visit
What do you do? Bite your fingers? (Uh…) Scratch your head? Say it’s not possible? (Okay, now, c’mon!)
GA probably means the god of Abram. Who knows? No?
“GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline. You can create workflows that build and test every pull request to your repository, or deploy merged pull requests to production.“ - docs.github.com
mkdir .github/workflows/
Create a workflow file and name it python-deploy.yml
. Copy and paste the following codes into the newly created workflow:
name: Pull Changes & Deploy
on:
push:
branches:
- dev
jobs:
pull_changes:
runs-on: ubuntu-latest
steps:
- name: Pull New Changes
uses: appleboy/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
port: ${{ secrets.SSH_PORT }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
script: |
cd /path/to/project_dir
git pull origin dev
deploy_backend:
runs-on: ubuntu-latest
steps:
- name: Deploy Backend
uses: appleboy/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
port: ${{ secrets.SSH_PORT }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
script: |
cd /path/to/project_dir
# ---------------------------------
# INSERT COMMAND(S) HERE
# ---------------------------------
name: is what you’d want to call this workflow.
on: is a trigger you want to listen for to tell this workflow when to run. In our case, when there is a new push to the “dev” branch.
jobs: These are jobs that you need the workflow to run.
pull_changes: is the name of the job that would be responsible for pulling new changes into our application.
deploy_backend: is the name of the job that would be responsible for deploying our application.
runs-on: the docker image you want this workflow to run on. I would recommend ubuntu-latest as it is the preferred container to test your code on, and most likely- what your code would run on, in production.
steps: these are the list of steps you want the workflow to run.
name: the name of the step.
uses appleboy/[email protected]: this action will allow you to execute remote ssh commands into the environment (dev, staging, pre-prod, prod) you want to. Read more
with: these are required configurations required by the action.
host: is the IP address of the server you wish to SSH into.
port: is the port number of the server you wish to SSH. The default is 22.
username: is the user of the server you want to SSH. The default is ubuntu.
key: is the private key of the server or pem file for the ec2 instance.
script: these are a list of commands you wish for the action to execute. Replace /path/to/project_dir
with the directory where your application code is. Replace dev
to the branch you wish to pull changes from.
Do not forget to add the secrets: SSH_HOST, SSH_PORT, SSH_USERNAME and SSH_KEY to your repository secrets.
In the section where you see: "INSERT COMMAND(S) HERE", kindly replace the entire comment with the commands you want to execute.
# using docker - option: 1
# --------------------------------
# run database migrations, etc
sudo docker-compose run <compose_service_name> python manage.py makemigrations
sudo docker-compose run <compose_service_name> python manage.py migrate
# using gunicorn - option 2
# -----------------------------------
source venv/bin/activate
python manage.py makemigrations
python manage.py migrate
systemctl restart gunicorn # add other services like celery
Replace <compose_service_name>
with the name of the service.
# using docker - option 1
# --------------------------------------------------------------------------
# run database migrations, etc
sudo docker-compose run <compose_service_name> alembic upgrade head
# without docker - option 2
# -------------------------------
# run database migrations, etc
# alembic upgrade head
# -------------------------------
Replace <compose_service_name>
with the name of the service.
# using docker - option 1
# -----------------------------
# run database migrations, etc
sudo docker-compose run <compose_service_name> alembic upgrade head
# without docker - option 2
# -------------------------------
# run database migrations, etc
# alembic upgrade head
# -------------------------------
Replace <compose_service_name>
with the name of the service.
Remember that you are adding the following: SSH_HOST, SSH_PORT, SSH_USERNAME and SSH_KEY.
Click on either of the job “Details”. If you add the repository secrets, and updated the python-deploy.yml
workflow file to fit your needs. You should see that your application has been redeployed successfully.
You trusted in me; and followed me from the start to the very end. I am truly honoured.