In this tutorial, we’ll enhance our GitHub Actions workflow to build, test, and publish a Docker image to Docker Hub. You’ll learn how to use Docker Buildx for multi-platform builds, run live container tests, and manage credentials securely for a robust CI/CD pipeline.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
1. Build and Test the Docker Image
First, we compile the image locally without pushing it, then spin up a container to validate the/live endpoint.
Tagging images with the Git SHA ensures traceability. Use
${{ github.sha }} for an immutable reference.2. Push the Docker Image to Docker Hub
Once tests pass, rebuild (with cache reuse) and push the image in a dedicated job.Ensure your
DOCKERHUB_TOKEN is stored as a GitHub repository secret to prevent credential leaks.3. Inspecting the Workflow Logs
During the push step, the logs will indicate cache reuse and the push command:build-push-action automatically leverages layers from the testing job to speed up the push.
4. Verifying on Docker Hub
After completion, navigate to your Docker Hub repository. You should find a new tag matching the commit SHA, for example:Job Summary
| Job Name | Purpose | Key Steps |
|---|---|---|
| build-and-test | Build image & run live tests | Checkout, Buildx setup, build, run container |
| push-image | Publish image to Docker Hub | Checkout, Buildx setup, login, build & push |
Links and References
- GitHub Actions Documentation
- Docker Buildx Action
- Docker Login Action
- Docker Build-Push Action
- Docker Hub