In the previous blog, i wrote about how to deploy your backend to the cloud with dokku. deployment was manual where you had to push your code manually to your server so Dokku could build and run it.
In this blog i'll be using Github Actions to automate that.
What is Continuous Deployment (CD)?
Continuous Deployment is a software engineering practice used to make the software development life cycle (SDLC) easier and more productive, by automating the deployment cycle using various techniques and tools.
Github Actions
Github actions is an automation platform, provided by github to automate tasks like testing, building and deploying code directly from your github repo.
The github action workflow should be a YAML file defined inside your_project/.github/workflows
folder, that's where github will look for your actions.
Let's Automate it
The follwing is the content of my workflow used to automate the deployment of my app, Let's break it down.
-
name:
a user friendly name you give to your action to understand what workflow is running. -
on:
github workflows are be triggered based on events, such as a pull or push request. in this case we want to deploy the app on everypush
to themaster
branch. -
jobs:
A job is the most important thing here, it's a set of steps, that can be either a shell script or an action that will be run. In this workflow we have 2 steps, the "Cloning repo" and "Push to dokku" steps. before that we have thedeploy
job, it's actually a customizable name you can name it whatever you want. -
📌 Cloning repo: to make your code accessible to the workflow, github provides an action template actions/checkout@v3. the template has a set of configurations. like the
ref
which defines the branch you want to access andsparse-checkout
to checkout a specific folder/files. -
🚚 Push to dokku: the step that handles the deployment of the code. dokku provides the template dokku/github-action@master to deploy your code. It requires a set of configurations, including
git_remote_url
andssh_private_key
. For security you need to hide your keys, you can define your secrets in the settings of your repo, go to repo settings > Secrets and variables > Actions, add your secrets, and they will be accessible inside the workflow under thesecrets
object.