GitHub : Composite Actions

1 minute read

Often when using Github Actions you end up building more or less the same workflow,
especially with microservices sharing the same framework.
Since we do not want to repeat ourselves(DRY), Github came up with the notion of composite actions.

Setup

The first step is to create a seperate repository (private in my case) where you will store all your composite actions.
Inside this repository you will have 1 folder per composite action with an action.yml or action.yaml file.

composite-actions
├── composite-1
│   └── action.yml
├── composite-2
│   └── action.yml
└── README.md

2 directories, 3 files

The action.yml contains inputs like a regular action and a list of the steps you want to re-use across your various workflows.
The kv using: "composite" is mandatory.

name: "Build composite-1"
description: "Build composite-1..."
inputs:
  working-directory:
    description: ''
    required: true


runs:
  using: "composite"
  steps: 

    - run: cd ${{ inputs.working-directory }}
      shell: bash

    - run: ls -l
      shell: bash

At this moment you are ready to use this composite action in one of your microservices workflow, by adding those 2 steps:

...
...
      - name: Clone composite actions
        uses: actions/checkout@v2
        with:
          repository: [org]/composite-actions
          token: ${{ secrets.PAT }}
          path: ./.github/actions/composite-actions

      - name: call composite-1
        uses: ./.github/actions/composite-actions/composite-1
        with:
          working-directory: my-working-dir
...
...

Here we are using a personal access token(PAT) since we have our composite actions in a private repository.
If you would be using a public repository to reference your composite actions, the first step would become obsolete.
And you would directly point to it, e.g: uses: [org]/composite-actions/composite-1

In some cases you might also want to reference a specific version/tag:
[org]/composite-actions/composite-1@0.1.0

Final thoughts

Composite actions are very handy to avoid repetitions hence mistakes, but have a few drawbacks.
One of them being that all composite action steps show as 1 step in your main microservice workflow.
Also Github is coming with the concept of Reusable Workflows (in beta, time of writing) - which will probably gain more momentum.

References

Updated: