The Ultimate Guide to Reusable Workflows

Home The Ultimate Guide to Reusable Workflows
Reusable Workflows By: John Abhilash / November 30, 2023

 Boost Efficiency: The Ultimate Guide to Reusable Workflows in GitHub Actions for Docker Image Build and Push

  1. 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.

    Why Reusable Workflows?

  2.  

    Reusable workflows in GitHub Actions offer several advantages:

    1. Code Reusability: Define a workflow once and reuse it across multiple repositories, reducing redundancy and ensuring consistency.

    2. Centralized Maintenance: Make updates or improvements in a single workflow file, and those changes reflect across all repositories using it.

    3. Consistent CI/CD Processes: Enforce standardized CI/CD practices by sharing the same workflow definition across projects.

    4. Time and Effort Savings: Save time by avoiding the need to recreate similar workflows for different repositories.

    Getting Started with Reusable Workflows

    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.

    Step 1: Create a Reusable Workflow

    1. In your GitHub repository, create a directory named .github/workflows if it doesn’t already exist.

    2. Inside the .github/workflows directory, create a file named docker-build-push.yml. This YAML file will define our reusable workflow.

    3. Open docker-build-push.yml in your preferred code editor and start by specifying the name and trigger for the workflow:

      yaml
      name: Docker Build and Push
      on:
      push:
      branches:
      - main

      This workflow will be triggered on each push to the main branch.

    4. Define reusable jobs for building and pushing the Docker image:

      yaml

      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.

    5. Save and commit the docker-build-push.yml file to your repository.

    Step 2: Using the Reusable Workflow

    Now that we have a reusable workflow, let’s see how to use it in another repository.

    1. In a different GitHub repository, navigate to the .github/workflows directory (create it if it doesn’t exist).

    2. Create a new YAML file, e.g., use-docker-build-push.yml, to define how the reusable workflow should be used:

      yaml

      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.

    3. Save and commit the use-docker-build-push.yml file to this repository.

    4. Push a change to the main branch. This will trigger the reusable workflow defined in the other repository.

    Passing Variables to Reusable Workflows

    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.

    Step 3: Passing Image Name as a Variable

    1. Update the docker-build-push.yml file in the original repository:

      yaml

      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

    2. In the same repository, update the use-docker-build-push.yml file:

      yaml

      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.

    3. Save and commit the changes.

    4. 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.

  3.  

  4. External Resources:

  5. Visit BootLabs’ website to learn more: https://www.bootlabstech.com/

     

  6.  

Previous post
OWASP ZAP Integration with GitLab CI/CD
Next Post
Prometheus Operator Deployment on Kubernetes

Leave a Comment