Back to Blog
1 min read

Building GitHub Composite Actions

Composite actions in GitHub Actions allow you to create reusable action components that combine multiple steps. They are simpler than JavaScript or Docker actions while providing powerful customization.

Creating Composite Actions

# .github/actions/setup-dotnet-project/action.yml
name: 'Setup .NET Project'
description: 'Setup .NET environment with caching'

inputs:
  dotnet-version:
    description: '.NET version'
    required: false
    default: '7.0.x'
  project-path:
    description: 'Path to project'
    required: false
    default: '.'

outputs:
  cache-hit:
    description: 'Cache hit status'
    value: ${{ steps.cache.outputs.cache-hit }}

runs:
  using: 'composite'
  steps:
    - name: Setup .NET
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: ${{ inputs.dotnet-version }}

    - name: Cache NuGet packages
      id: cache
      uses: actions/cache@v3
      with:
        path: ~/.nuget/packages
        key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj') }}

    - name: Restore dependencies
      shell: bash
      working-directory: ${{ inputs.project-path }}
      run: dotnet restore

Using Composite Actions

name: Build

on: push

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Setup project
        uses: ./.github/actions/setup-dotnet-project
        with:
          dotnet-version: '7.0.x'

      - name: Build
        run: dotnet build --configuration Release

Composite actions bridge the gap between simple step sequences and full custom actions.

Michael John Peña

Michael John Peña

Senior Data Engineer based in Sydney. Writing about data, cloud, and technology.