CI/CD Workflow with docker/build-push-action
This document outlines the CI/CD workflow using docker/build-push-action
. It is assumed that you have a working project setup that requires building and pushing Docker images as part of your CI/CD pipeline. If no CI/CD exists in the project, it is recommended to begin setting up a CI/CD pipeline using GitHub Actions.
Step 1: Set Up Your GitHub Actions Workflow
Create a new workflow file in your project repository at .github/workflows/ci-cd.yml
. This file will define the CI/CD process using the docker/build-push-action
.
name: CI/CD Workflow
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to Docker Registry
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and Push Docker Image
uses: docker/build-push-action@v2
with:
context: .
file: Dockerfile
push: true
tags: user/example:${{ github.sha }}
Step 2: Define Your Dockerfile
Ensure you have a Dockerfile
in the root of your repository. Below is a basic example:
# syntax=docker/dockerfile:1
FROM alpine
RUN echo "Hello world!"
The Dockerfile specifies that the image will be built from the alpine
base image, and it runs a simple command to echo “Hello world!”.
Step 3: Configure GitHub Secrets
For the Docker login action to work correctly, you need to configure your secrets in the GitHub repository settings. Navigate to Settings > Secrets and Variables > Actions
and add the following secrets:
DOCKER_USERNAME
: Your Docker Hub usernameDOCKER_PASSWORD
: Your Docker Hub password or personal access token
Step 4: Verify the Workflow
After pushing changes to the main
branch, the defined workflow will trigger automatically. Navigate to the “Actions” tab in your GitHub repository to monitor the progress of the workflow. You should see the steps of the CI/CD workflow being executed.
Step 5: Test Your Docker Image
Once the image is built and pushed successfully, you can pull it from Docker Hub to verify it works as expected.
docker pull user/example:<commit_hash>
docker run user/example:<commit_hash>
By default, GitHub Actions uses the commit SHA as the tag for the pushed image.
Additional Steps if CI/CD is Not Set Up
If the CI/CD pipeline is not yet set up, consider the following next steps:
Install Necessary Tools: Ensure you have Docker installed locally for development and testing.
Define CI/CD Requirements: Determine which actions need to be automated, such as testing, building Docker images, and deploying them.
Create a Sample Dockerfile: If you don’t have a Dockerfile yet, create a basic one as shown above.
Plan Your CI/CD Flow: Define the triggers for your workflow (e.g., on push, pull requests).
Utilize GitHub Actions: Leverage the
docker/build-push-action
and other actions available in the GitHub Marketplace to build and push images as part of your pipeline.Documentation: Ensure all your configurations and steps are documented for future reference and clarity within the team.
Following this workflow will facilitate an efficient CI/CD process using GitHub Actions and the docker/build-push-action.
(Source: Official documentation)