Continuous Deployment with Docker🐳 and Github Actions🐣
--
What
Deploying Docker Images is boring. Every time you want to push your image you have to build it, give it the proper tag, authenticate with your repository, push it, and then notify your other services that a new version is available.
People shouldn’t do this work; it's designed to be automated. This will go over how to automate your Docker build process.
For the purpose of this tutorial, we will be working with Docker Hub, however, with small modifications, these will work with both GCR, ACR, or ECR.
Why
- Saves developers time
- Eliminates human error
- Makes pipeline to production more secure
How
Pre-requisites
- A Github Repository
- A Docker Hub Repository
You can automate deployments with Github Actions.
Create a workflow file
Github Actions are located within the .github/workflows
directory of a Github Project. For the deployment workflow we will be building, I am going to use .github/workflows/deploy-docker-image.yaml
Building the Workflow
Name
All workflows need a name, this comes in the form of the name parameter.
name: AUTO DEPLOY CONTAINER
On
The On section defines when the action will run, and when your container will get deployed.
There are two main ways to do this, depending on how you want to organize your project. The choices are between deploying the container whenever there is a push to the main branch, or whenever there is a new release.
On push to the main branch:
on:
push:
branches:
- main
On release:
on:
release:
types: [published]
Jobs
The jobs define the work a container will have to do
jobs:
deploy:
runs-on: ubuntu-latest
This defines the name of the job, and where it will run. This job will be called deploy and will run on an ubuntu machine.
Steps
The steps define the work the job will have to do.
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Install Docker
uses: docker/setup-buildx-action@v1
- name: Login to Docker
run: docker login --username=yourhubusername --password=***
- name: Build Image
env:
IMAGE_TAG: ${{ github.sha }}
run: docker build -t yourhubusername/imagename:$IMAGE_TAG .
- name: Push Image
env:
IMAGE_TAG: ${{ github.sha }}
run: docker push yourhubusername/imagename:$IMAGE_TAG
🥳🥳🥳 You have a working Continuous Deployment Github Action
But it can be better
Improvements
- You can make it secure by using Github Secrets
- You can make it more adaptable to environmental variables
Secrets
Creation
Github Secrets can be created by going to
https://github.com/username/repository/settings/secrets/actions
Note: replace username/repository with your username and repository.
You should create a secret for your password, I named mine DOCKER_HUB_PASSWORD
Using Secrets
Secrets can be used in many ways, however, I have had the most success by defining them as global environment variables, and using them as those.
Creating global environment variables
env:
DOCKER_HUB_PASSWORD: ${{ secrets.DOCKER_HUB_PASSWORD }}
USERNAME: yourhubusername
REPOSITORY_NAME: yourrepositoryname
Using global environment variables
The new steps look like this:
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Install Docker
uses: docker/setup-buildx-action@v1
- name: Login to Docker
run: docker login --username=yourhubusername --password=$DOCKER_HUB_PASSWORD
- name: Build Image
run: docker build -t $USERNAME/$REPOSITORY_NAME:$IMAGE_TAG .
- name: Push Image
run: docker push $USERNAME/$REPOSITORY_NAME:$IMAGE_TAG
Bonus
Create a .dockerignore
file to ignore the .github
folder
All code in this tutorial is available in template form here: https://github.com/theswerd/docker-githubactions