Boost Efficiency: The Ultimate Guide to Reusable Workflows in GitHub Actions for Docker Image Build and Push
In the ever-evolving landscape of DevOps, automation is key to streamlining workflows, enhancing collaboration, and ensuring efficiency. GitHub Actions provides a powerful platform for automating tasks, and one of its notable features is the ability to create reusable workflows. This blog will guide you through the process of creating a reusable workflow in GitHub Actions and demonstrate its application in a Docker build and push scenario.
Reusable workflows in GitHub Actions offer several advantages:
Code Reusability: Define a workflow once and reuse it across multiple repositories, reducing redundancy and ensuring consistency.
Centralized Maintenance: Make updates or improvements in a single workflow file, and those changes reflect across all repositories using it.
Consistent CI/CD Processes: Enforce standardized CI/CD practices by sharing the same workflow definition across projects.
Time and Effort Savings: Save time by avoiding the need to recreate similar workflows for different repositories.
Let’s start by creating a reusable workflow for a common scenario: building a Docker image and pushing it to a container registry. We’ll use GitHub Actions, Docker, and a hypothetical container registry for this example.
In your GitHub repository, create a directory named .github/workflows
if it doesn’t already exist.
Inside the .github/workflows
directory, create a file named docker-build-push.yml
. This YAML file will define our reusable workflow.
Open docker-build-push.yml
in your preferred code editor and start by specifying the name and trigger for the workflow:
name: Docker Build and Push
on:
push:
branches:
- main
This workflow will be triggered on each push to the main branch.
Define reusable jobs for building and pushing the Docker image:
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
– name: Set up Docker
uses: docker/setup-docker@v2
- name: Build and Push Docker Image
env:
IMAGE_NAME: ${{ github.event_name }}-image
run: |
docker build -t $IMAGE_NAME .
docker push $IMAGE_NAME
In this example, the job build-and-push
checks out the repository, sets up Docker, and builds/pushes the Docker image. The IMAGE_NAME
environment variable is set based on the GitHub event name.
Save and commit the docker-build-push.yml
file to your repository.
Now that we have a reusable workflow, let’s see how to use it in another repository.
In a different GitHub repository, navigate to the .github/workflows
directory (create it if it doesn’t exist).
Create a new YAML file, e.g., use-docker-build-push.yml
, to define how the reusable workflow should be used:
name: Use Docker Build and Push
on:
push:
branches:
– main
jobs:
build-and-push:
uses: your-username/your-repo-name@main
Replace your-username
and your-repo-name
with the actual GitHub username and repository name where your reusable workflow is defined.
Save and commit the use-docker-build-push.yml
file to this repository.
Push a change to the main branch. This will trigger the reusable workflow defined in the other repository.
In real-world scenarios, you might want to pass dynamic values (like image names) to your reusable workflow. Let’s modify our example to accept the Docker image name as a variable.
Update the docker-build-push.yml
file in the original repository:
name: Docker Build and Push
on:
push:
branches:
- main
jobs:
build-and-push:
runs-on: ubuntu-latest
inputs:
image-name:
description: ‘Docker image name’
required: true
steps:
– name: Checkout Repository
uses: actions/checkout@v2
– name: Set up Docker
uses: docker/setup-docker@v2
- name: Build and Push Docker Image
env:
IMAGE_NAME: ${{ inputs.image-name }}
run: |
docker build -t $IMAGE_NAME .
docker push $IMAGE_NAME
In the same repository, update the use-docker-build-push.yml
file:
name: Use Docker Build and Push
on:
push:
branches:
– main
jobs:
build-and-push:
uses: your-username/your-repo-name@main
with:
image-name: 'your-dynamic-image-name'
Replace 'your-dynamic-image-name'
with the actual image name you want to use.
Save and commit the changes.
Push a change to the main branch in the second repository, and the reusable workflow will use the provided image name.
Creating reusable workflows in GitHub Actions significantly enhances automation capabilities, fostering consistency and reducing redundancy across projects. In this blog, we walked through creating a reusable workflow for Docker build and push, and demonstrated how to use it in different repositories while passing dynamic variables. Leveraging this approach can streamline your CI/CD processes and contribute to a more efficient and collaborative development environment.
External Resources:
Visit BootLabs’ website to learn more: https://www.bootlabstech.com/
Leave a Comment