cschleiden
Published on

Effective Workflow GitHub CLI extension

Authors

GitHub Actions supports a feature for reuse called "Reusable workflows". It allows you to include workflow jobs defined in a shared workflow file.

One drawback is that you don't get to see the whole workflow file for a workflow run anymore. The UI still shows you the calling workflow, but you don't see the called workflows at their respective versions. For example, when this simple workflow:

name: Call a reusable workflow

on:
  workflow_dispatch:

jobs:
  call-workflow:
    uses: cschleiden/cschleiden-called/.github/workflows/called.yml@main
    with:
      name: 'monalisa'

  call-workflow2:
    uses: cschleiden/cschleiden-called/.github/workflows/called.yml@main
    with:
      name: 'cschleiden'

  echo:
    runs-on: ubuntu-latest
    steps:
    - run: echo 'world'

is run, it uses workflows from another repo: cschleiden/cschleiden-called. But when you look at the workflow file for the run in the UI:

workflow file for the run

you don't see the called workflows. You also don't know exactly which version was used. While it's relatively easy in this example since the called workflows are referenced from default branch:

  call-workflow2:
    uses: cschleiden/cschleiden-called/.github/workflows/called.yml@main

in other scenarios they might be referenced from other branches, tags, or SHAs.

One way to figure out is to use the REST API which does include the information which called workflows were included:

  "referenced_workflows": [
    {
      "path": "cschleiden/cschleiden-called/.github/workflows/called.yml@main",
      "sha": "21f5e61d865b5e06afff7fa8e05251908a98e955",
      "ref": "refs/heads/main"
    },
    {
      "path": "cschleiden/cschleiden-called/.github/workflows/called2.yml@main",
      "sha": "21f5e61d865b5e06afff7fa8e05251908a98e955",
      "ref": "refs/heads/main"
    }
  ],

Since getting the full picture from the API takes some manual work, I put together a quick GitHub CLI extension to show the effective workflow, that is the calling workflow and every workflow in the reusable workflow callchain: https://github.com/cschleiden/gh-effective-workflow

It's easy to install, once you have the GitHub CLI installed, just run:

$ gh extension install https://github.com/cschleiden/gh-effective-workflow

and then you can use

$ gh effective-workflow view <run-id>

to see the calling workflow, every called workflow at the SHA that was used for the run and from where it was called. So as an example for the run mentioned before, the output would be:

Example output of the extension