CI/CD with GitHub Actions
Deployment with Docker and Python
In this post, we will setup CI/CD with GitHub actions to build and push images to Docker Hub.
What is CI/CD
CI/CD combines the practices of continuous integration and continuous delivery. CI/CD automates much or all of the manual human intervention traditionally needed to get new code from a commit into production such as build, test, and deploy, as well as infrastructure provisioning. With a CI/CD pipeline, developers can make changes to code that are then automatically tested and pushed out for delivery and deployment.
What is GitHub Actions
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.
Docker Hub
Docker Hub is a hosted repository service provided by Docker for finding and sharing container images with your team. Key features include: Private Repositories: Push and pull container images. Automated Builds: Automatically build container images from GitHub and Bitbucket and push them to Docker Hub.
Setup CI/CD with GitHub actions
Docker Hub
To get started, login to Docker Hub and create a new repository. In my case, the repository is called “demo”.
Creating the GitHub secrets
To build and push images to our Docker Hub account, we need to tell GitHub how to do this. We do that by adding secrets for our docker username, docker password and the name of the Docker Hub repository.
In your GitHub account, go to settings and click on secrets. Add the following secrets.
Note , the DOCKERHUB_REPO secret is in my case set to magsther/demo
GitHub Actions Workflow
Go to GitHub Actions.
Click on New Workflow. GitHub gives you a suggestion of workflows that you can use, based on the type of files that are inside the repository.
I picked the Python application workflow and used that as a template. I also added the configuration needed for to build and push my Docker image to Docker Hub. The workflow can be found here.
Pay attention to the on
property, which describes when the automation is going to start.
on: push: branches: [ "main" ] pull_request: branches: [ "main" ]
If you now make a change and push the code into the repository, the actions will start and build the code.
When the build is done, you should see that all steps are completed.
If you now switch to your Docker Hub account, you will also see that you have a new build showing up.
Congratulations , you have just created a pipeline using GitHub action to automatically build and push a docker image to Docker Hub.