Skip to main content
In this guide, you’ll learn how to integrate Docker Hub authentication into your GitHub Actions CI/CD pipeline. By the end, your workflow will automatically build and push a Docker image once unit tests and code coverage checks have passed.

Prerequisites

  • A GitHub repository containing your application code and a Dockerfile.
  • Unit tests and code coverage steps already configured in your workflow.
  • Docker Hub account with repository access.

1. Existing Workflow Overview

Below is an example workflow that runs unit tests and measures code coverage on every push to main or any feature/* branch:

2. Dockerfile for the Application

Ensure your repository includes a Dockerfile like this:

3. Add the Containerization Job

We’ll create a new job named containerization that depends on the previous jobs. This job will:
  1. Check out the repository.
  2. Authenticate to Docker Hub.
  3. Build and push the Docker image.
Replace my-app with your Docker Hub repository name (e.g., username/solar-system).
You can also tag with :latest or semantic versions.

4. Store Credentials as Variables and Secrets

To prevent exposing your Docker Hub credentials in the workflow, add them via the GitHub UI:
  1. Go to Settings > Secrets and variables > Actions.
  2. Under Repository variables, click New repository variable and add DOCKERHUB_USERNAME.
  3. Under Repository secrets, click New repository secret and add DOCKERHUB_PASSWORD.
Add a new secret in GitHub repository settings
Manage Actions secrets and variables in GitHub repository settings
Add a new Actions variable for Docker Hub username
Overview of Actions secrets and variables management
Never hardcode sensitive credentials in your workflow files. Always use Secrets for passwords and Variables for non-sensitive values.

5. Commit and Push

After updating .github/workflows/ci.yml (or your workflow filename), commit your changes and push to the repository:

6. Verify the Workflow Run

  1. Navigate to the Actions tab in your repository.
  2. Select the latest run of your workflow.
  3. Confirm that:
    • The containerization job starts only after unit-testing and code-coverage.
    • The Docker Hub login step completes without printing your password.
List of workflow runs in GitHub Actions
Successful containerization job with Docker Hub login step
Congratulations! You have successfully set up Docker Hub login within your GitHub Actions pipeline, enabling automatic building and publishing of your container images.

Watch Video