GitHub Actions Certification
Continuous Integration with GitHub Actions
Workflow Docker Login
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:
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: …
code-coverage: …
2. Dockerfile for the Application
Ensure your repository includes a Dockerfile
like this:
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"]
3. Add the Containerization Job
We’ll create a new job named containerization
that depends on the previous jobs. This job will:
- Check out the repository.
- Authenticate to Docker Hub.
- Build and push the Docker image.
jobs:
unit-testing: …
code-coverage: …
containerization:
name: Containerization
needs: [unit-testing, code-coverage]
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Build Docker Image
run: |
docker build -t my-app:${{ github.sha }} .
- name: Push Docker Image
run: |
docker push my-app:${{ github.sha }}
Note
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:
Name | Type | Location |
---|---|---|
DOCKERHUB_USERNAME | Variable | Settings → Secrets and variables → Actions → Variables |
DOCKERHUB_PASSWORD | Secret | Settings → Secrets and variables → Actions → Secrets |
- Go to Settings > Secrets and variables > Actions.
- Under Repository variables, click New repository variable and add
DOCKERHUB_USERNAME
. - Under Repository secrets, click New repository secret and add
DOCKERHUB_PASSWORD
.
Warning
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:
git add .github/workflows/ci.yml
git commit -m "chore: add Docker login and image push"
git push
6. Verify the Workflow Run
- Navigate to the Actions tab in your repository.
- Select the latest run of your workflow.
- Confirm that:
- The
containerization
job starts only afterunit-testing
andcode-coverage
. - The Docker Hub login step completes without printing your password.
- The
Congratulations! You have successfully set up Docker Hub login within your GitHub Actions pipeline, enabling automatic building and publishing of your container images.
Links and References
Watch Video
Watch video content