Setting Up a CI Pipeline in GitHub Actions for Building and Pushing Docker Images to Hub.Docker.com

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:

  1. A GitHub account
  2. A Docker Hub account
  3. 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:

  1. Open your GitHub repository in a browser.
  2. Navigate to the “Settings” tab.
  3. Click on “Secrets” in the left sidebar.
  4. Click on “New repository secret.”
  5. Enter DOCKER_USERNAME in the “Name” field and your Docker Hub username in the “Value” field.
  6. Click on “Add secret” to save the secret.
  7. 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:

  1. In your GitHub repository, navigate to the “Settings” tab.
  2. Click on “Branches” in the left sidebar.
  3. Under “Branch protection rules,” click on “Add rule.”
  4. Specify the branch you want to protect, such as main or master.
  5. Enable the following options:
    • “Require pull request reviews before merging”
    • “Include administrators”
  6. 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:

  1. Checks out the repository code.
  2. Logs in to Docker Hub using the Docker Hub username and access token from secrets.
  3. Builds a Docker image and tags it with the specified name.
  4. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights