Introduction:
As software development and deployment practices continue to evolve, continuous integration (CI) and continuous deployment (CD) have become essential for efficient and reliable software delivery. In this blog post, we will guide you through setting up a CI pipeline using GitHub Actions to build and push Docker images to hub.docker.com. We will cover the creation of secrets, configuring your GitHub repository settings, and generating an access token in Docker Hub. Let’s dive in!
Prerequisites:
Before we begin Setting up the CI pipeline in GitHub Actions for building and pushing Docker images to Hub.Docker.com, make sure you have the following:
- A GitHub account
- A Docker Hub account
- A GitHub repository for your project (for this example, we’ll use https://github.com/learnsmartcoding/dotnet-with-docker)
Step 1:
Creating Secrets in GitHub: GitHub provides a secure way to store sensitive information using secrets. We’ll create two secrets: DOCKER_USERNAME
and DOCKERHUB_TOKEN
. Follow these steps:
- Open your GitHub repository in a browser.
- Navigate to the “Settings” tab.
- Click on “Secrets” in the left sidebar.
- Click on “New repository secret.”
- Enter
DOCKER_USERNAME
in the “Name” field and your Docker Hub username in the “Value” field. - Click on “Add secret” to save the secret.
- Repeat the above steps to create the
DOCKERHUB_TOKEN
secret, using your Docker Hub access token as the value.
Step 2:
Configuring GitHub Repository Settings: To trigger the CI pipeline on each commit or pull request, we need to configure the repository settings. Follow these steps:
- In your GitHub repository, navigate to the “Settings” tab.
- Click on “Branches” in the left sidebar.
- Under “Branch protection rules,” click on “Add rule.”
- Specify the branch you want to protect, such as
main
ormaster
. - Enable the following options:
- “Require pull request reviews before merging”
- “Include administrators”
- Save the branch protection rule.
Step 3:
Defining GitHub Actions Workflow: We will now define a GitHub Actions workflow that builds and pushes Docker images to hub.docker.com. Create a file named .github/workflows/ci.yml
in your repository and add the following content:
# This is a basic workflow to help you get started with Actions
name: CI-build-and-push-image-for-dotnetapp
# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [ main ]
paths-ignore:
- README.md
- .vscode/**
- .gitignore
pull_request:
branches: [ main ]
paths-ignore:
- README.md
- .vscode/**
- .gitignore
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
-
name: Set up QEMU
uses: docker/setup-qemu-action@v1
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
-
name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
uses: docker/build-push-action@v2
with:
context: .
file: ./LearnSmartCoding.API/Dockerfile
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/dockerfordotnetapi:latest, ${{ secrets.DOCKERHUB_USERNAME }}/dockerfordotnetapi:${{ github.run_number }}
This workflow consists of a single job that executes on each push to the main
branch. It performs the following steps:
- Checks out the repository code.
- Logs in to Docker Hub using the Docker Hub username and access token from secrets.
- Builds a Docker image and tags it with the specified name.
- Pushes the Docker image to Docker Hub.
Step 4: Committing and Pushing the Workflow: Once you have added the workflow file, commit and push it to your GitHub repository. The CI pipeline will automatically trigger for each push to the main branch.
Conclusion:
In conclusion, by following this step-by-step guide, you can successfully set up a CI pipeline in GitHub Actions for building and pushing Docker images to Hub.Docker.com. This CI pipeline allows for efficient and automated containerization, ensuring the smooth deployment of your applications. By Setting up CI pipeline in GitHub Actions for building and pushing Docker images to Hub.Docker.com, you can streamline your development process, improve collaboration, and enhance the reliability of your Docker image deployments. Embrace the power of CI/CD and Docker to unlock the full potential of your software development workflow.