Streamline Your GitHub Workflows with Composite Actions

samnash

sam-nash

Posted on November 19, 2024

Streamline Your GitHub Workflows with Composite Actions

GitHub Actions has become a go-to tool for automating workflows, enabling developers to streamline everything from CI/CD to code quality checks. While it’s powerful out of the box, workflows can quickly become unwieldy as the number of repeated steps grows. Composite actions provide a solution to this problem, enabling you to package reusable steps into modular, maintainable components.

In this blog, we’ll explore what composite actions are, why they’re beneficial, and how to create one using a real-world example.

What Are Composite Actions?

Composite actions allow you to encapsulate multiple steps into a single reusable action. Instead of duplicating code across workflows, you can create a standalone block that performs specific tasks and use it across repositories. Composite actions live within your repository and are defined using YAML, making them easy to version, extend, and customize.

Benefits of using Composite Actions

Here are some reasons to embrace composite actions:

  1. Reusability: Share the same logic across multiple workflows and repositories.
  2. Maintainability: Update logic in one place, and all workflows using it benefit.
  3. Readability: Simplify complex workflows by abstracting repetitive logic.
  4. Modularity: Create building blocks that other developers or teams can easily use.

Example: Terraform Operations as a Composite Action

Let’s dive into a practical example. Suppose you have a GitHub Actions workflow that runs Terraform commands such as init, fmt, validate, and plan for managing infrastructure on Google Cloud. Instead of duplicating these steps across workflows, you can package them into a composite action.

  1. The Problem: Repetition in Workflows

Here’s a snippet of a typical Terraform workflow:

jobs:
terraform:
runs-on: ubuntu-latest
steps:
- name: Terraform Init
run: terraform init -backend-config="bucket=my-project-tfstate"
working-directory: terraform

  - name: Terraform Format
    run: terraform fmt
    working-directory: terraform

  - name: Terraform Validate
    run: terraform
Enter fullscreen mode Exit fullscreen mode

Each step is repeated every time Terraform commands are needed. If you manage multiple workflows or projects, this quickly becomes inefficient and error-prone.

  1. The Solution: A Composite Action

We’ll create a composite action to handle all Terraform operations.

Step 1: Set Up the Directory Structure

Composite actions are stored in the .github/actions/ directory. Create a folder for your action:

.github/actions/terraform/

Inside the folder, create a file named action.yml.

Step 2: Define the Composite Action

Here’s the action.yml file for the Terraform composite action:

name: Terraform Operations
description: Executes Terraform commands like init, fmt, validate, and plan
inputs:
working_directory:
description: Terraform working directory
required: true
gcp_project:
description: Target GCP project
required: true
apply_changes:
description: Whether to apply changes
required: false
default: false
runs:
using: "composite"
steps:
- name: Terraform Init
run: terraform init -backend-config="bucket=${{ inputs.gcp_project }}-tfstate"
working-directory: ${{ inputs.working_directory }}
shell: bash

- name: Terraform Format
  run: terraform fmt
  working-directory: ${{ inputs.working_directory }}
  shell: bash

- name: Terraform Validate
  run: terraform validate
  working-directory: ${{ inputs.working_directory }}
  shell: bash

- name: Terraform Plan
  run: terraform plan -var-file="${{ inputs.gcp_project }}.tfvars" -out=tfplan
  working-directory: ${{ inputs.working_directory }}
  shell: bash

- name: Terraform Apply
  if: ${{ inputs.apply_changes == 'true' }}
  run: terraform apply -auto-approve tfplan
  working-directory: ${{ inputs.working_directory }}
  shell: bash
Enter fullscreen mode Exit fullscreen mode

This composite action supports inputs like the working directory and target GCP project, making it adaptable to different projects.

  1. Using the Composite Action in a Workflow

Once your composite action is defined, you can use it in your workflows like this:

name: Terraform Workflow

on:
push:
branches:
- main

jobs:
terraform:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v2

  - name: Run Terraform Operations
    uses: ./.github/actions/terraform
    with:
      working_directory: terraform
      gcp_project: my-gcp-project
      apply_changes: false
Enter fullscreen mode Exit fullscreen mode

How This Simplifies Your Workflow

• Reduced Duplication: The Terraform logic is centralized in the composite action.
• Improved Readability: The main workflow is concise and easy to follow

💖 💪 🙅 🚩
samnash
sam-nash

Posted on November 19, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related