Skip to content

Deploy to Prod

Deploy to Prod #16

Workflow file for this run

name: Deploy to Prod
on:
workflow_dispatch:
inputs:
api_version:
description: 'api version for docker image'
required: true
type: string
web_version:
description: 'web version for docker image'
required: true
type: string
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Install SSH client
run: sudo apt-get install -y openssh-client
- name: Shutdown running containers
env:
SSH_KEY: ${{ secrets.SSH_KEY }}
HOST: ${{ secrets.HOST }}
USERNAME: ${{ secrets.USERNAME }}
run: |
echo ---= Deploying API version v${{ github.event.inputs.api_version }} =---
echo ---= Deploying Web version v${{ github.event.inputs.web_version }} =---
# Create a private key file from the SSH_KEY secret
# set permissions to 600 which means only the owner can read and write
echo "$SSH_KEY" > private_key
chmod 600 private_key
ssh -i private_key -o StrictHostKeyChecking=no $USERNAME@$HOST << 'EOF'
cd /home/thor/apps/bookstore-app
docker compose -f docker-compose-prod.yml down
if [ -f docker-compose-prod.yml ]; then
rm docker-compose-prod.yml
fi
exit
EOF
- name: Copy docker-compose-prod.yml to server
env:
SSH_KEY: ${{ secrets.SSH_KEY }}
HOST: ${{ secrets.HOST }}
USERNAME: ${{ secrets.USERNAME }}
run: |
# Passing the api_version and web_version to the docker-compose-prod.yml file
sed -i "s/{{api_version}}/${{ github.event.inputs.api_version }}/g" docker-compose-prod.yml
sed -i "s/{{web_version}}/${{ github.event.inputs.web_version }}/g" docker-compose-prod.yml
# Copy the docker-compose-prod.yml & manage.py file to the server
scp -i private_key -o StrictHostKeyChecking=no docker-compose-prod.yml $USERNAME@$HOST:/home/thor/apps/bookstore-app/
scp -i private_key -o StrictHostKeyChecking=no ./app/manage.py $USERNAME@$HOST:/home/thor/apps/bookstore-app/
- name: Start up containers
env:
SSH_KEY: ${{ secrets.SSH_KEY }}
HOST: ${{ secrets.HOST }}
USERNAME: ${{ secrets.USERNAME }}
run: |
ssh -i private_key -o StrictHostKeyChecking=no $USERNAME@$HOST << 'EOF'
cd /home/thor/apps/bookstore-app
docker compose -f docker-compose-prod.yml up -d
exit
EOF