GitHub Actions
Continuous Integration with GitHub Actions
Workflow Docker Login
In this guide, you’ll learn how to extend your GitHub Actions pipeline to authenticate with Docker Hub (or any OCI registry) before building and pushing a Docker image. We assume you already have unit tests and code coverage set up; our focus here is adding a containerization job that logs into Docker.
1. Current Workflow and Dockerfile
1.1. GitHub Actions Workflow
The following workflow runs on pushes to main
or feature/*
branches, and can be triggered manually via workflow_dispatch
:
name: Solar System Workflow
on:
push:
branches:
- main
- 'feature/*'
workflow_dispatch:
env:
MONGO_URI: 'mongodb+srv://supercluster.d83jj.mongodb.net/superData'
MONGO_USERNAME: ${{ vars.MONGO_USERNAME }}
MONGO_PASSWORD: ${{ secrets.MONGO_PASSWORD }}
jobs:
unit-testing:
# … your unit testing steps
code-coverage:
# … your coverage steps
1.2. Dockerfile
Keep this Dockerfile
at the repository root to build your Node.js image:
FROM node:18-alpine3.17
WORKDIR /usr/app
COPY package*.json /usr/app/
RUN npm install
COPY . .
ENV MONGO_URI=uriPlaceholder
ENV MONGO_USERNAME=usernamePlaceholder
ENV MONGO_PASSWORD=passwordPlaceholder
EXPOSE 3000
CMD ["npm", "start"]
2. Add the Containerization Job
Insert a new job named containerization
after your existing steps. It will:
- Checkout the repository
- Authenticate with Docker Hub (or any registry)
jobs:
# … unit-testing and code-coverage as before
containerization:
name: Containerization
runs-on: ubuntu-latest
needs: [unit-testing, code-coverage]
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Docker Login
uses: docker/login-action@v2
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
Note
The docker/login-action
supports Docker Hub, GitHub Container Registry, AWS ECR, Google GCR, Azure ACR, and more. It performs docker login
in the workflow and handles logout post-job.
3. Configure Secrets and Variables
You’ll need:
- Secrets for sensitive data:
DOCKERHUB_PASSWORD
,MONGO_PASSWORD
- Variables for non-sensitive values:
DOCKERHUB_USERNAME
,MONGO_USERNAME
Type | Purpose | Example |
---|---|---|
Secret | Password or token | DOCKERHUB_PASSWORD |
Variable | Non-sensitive string | DOCKERHUB_USERNAME |
- In your repo, go to Settings → Secrets and variables.
- Under Actions secrets, add
DOCKERHUB_PASSWORD
. - Under Actions variables, add
DOCKERHUB_USERNAME
.
4. Observe Your Workflow Run
After committing and pushing these changes:
- Open the Actions tab in GitHub.
- Select your workflow; you’ll see builds triggered by your push.
- Click on a run to view job dependencies. Notice Containerization waits for unit-testing and code-coverage:
- Once earlier jobs pass, the Docker login step executes:
Your workflow is now authenticated to Docker Hub. In the next lesson, we’ll build, tag, and push the Docker image.
Links and References
Watch Video
Watch video content