CI/CD with Github Actions
In terms of CI, GitHub Actions, which is the CI/CD framework provided by GitHub, allows you to automate the build, test, and validation processes for your software projects. With GitHub Actions, you can define workflows that execute various tasks, such as compiling code, running tests, performing code analysis, and generating build artifacts. It supports a wide range of programming languages and provides flexibility in customizing and configuring your CI pipeline.
Regarding CD, GitHub Actions also facilitates continuous deployment by integrating with various deployment strategies and environments. It allows you to automate the deployment of your application to different platforms and hosting services, such as cloud providers or dedicated servers. You can define deployment workflows that automatically trigger when specific conditions are met, ensuring a seamless and automated release process.
Let’s use workflows to have automatic CI/CD Pipelines in our Github Repository for free.
Github Workflows
This Github Feature will help us to create automatically Docker containers whenever the repository received a new push.
In your repository, you will see the button Actions, then New workflow.
You can select the type of workflow most suitable for your project or just use my recommendation from the following steps as the .yml file.
Building Docker images on Push
name: CI/CD Pipeline
on:
push:
branches:
- main
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
Pushing Containers to ghcr
You will need to include the following code chunk below the build, to push the created container to Github Container Repository:
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.CICD_DASH_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: ghcr.io/jalcocert/slider_crank:${{ github.sha }}
Remember that you will need the secret CICD_DASH_TOKEN so that Github can act on your behalf for this action. This can be obtained from Github Settings -> Developer Settings -> Personal Access Tokens
Pushing Docker Containers to DockerHub
If you prefer to have your containers in DockerHub, you will need the following code included in your .yml file:
- uses: actions/checkout@v1
- name: Build & Push Image to DockerHub
run: |
echo "${{ secrets.CICD_DASH_TOKEN_DOCKERHUB }}" | docker login -u "your_DockerHubUser" --password-stdin
docker image build -t reisikei/slider_crank:latest .
docker push reisikei/slider_crank:latest
In this case, you will need the secret CICD_DASH_TOKEN_DOCKERHUB so that Github can act on your behalf for this action, you can create your PAT from the Settings in DockerHub.