13

I have created a Reusable Workflow using a workflow_call trigger, but I need to run additional steps based on its outcome.

Example:

jobs:
  released:
    steps:
      - name: Build
        uses: my-org/some-repo/.github/workflows/build.yml@main

      - name: Upload source maps
        run: something

The reusable Build step builds my JS app and produces source maps. Now I need to upload these source maps to somewhere as a separate step that should only run inside this Released job.

Doing the above results in the following error:

Error : .github#L1
reusable workflows should be referenced at the top-level `jobs.*.uses' key, not within steps

It only allows running my reusable workflow inside a job, not inside a step. But by doing that I can no longer access the source maps.

My question: How do I reuse the steps from the Build workflow and access its output inside the Released job?

Duncan Lukkenaer
  • 12,050
  • 13
  • 64
  • 97
  • if I understand correctly, might it help to split your things into multiple jobs and have the jobs communicate via outputs? https://stackoverflow.com/a/61236803/1080523 – rethab Nov 17 '21 at 11:59
  • @rethab If I understand correctly, outputs only work for string values. In this case I need to access the generated source map **files**. – Duncan Lukkenaer Nov 17 '21 at 12:58
  • Could the build job upload the source maps as build artifacts (using artifact-upload) and then the release job would download them again and upload them whereever else it wants to upload them? – rethab Nov 17 '21 at 13:07
  • 1
    You could create a local action using the `composite` type on this repository in the `.github/actions/` directory, and access it in your workflow job using in a step with `uses: ./.github/actions/`. This action could have an output variable that you could access in your other steps. – GuiFalourd Nov 17 '21 at 13:19
  • 2
    I've made an example: [the workflow](https://github.com/GuillaumeFalourd/poc-github-actions/blob/main/.github/workflows/36-local-action.yml), the [local (composite) action](https://github.com/GuillaumeFalourd/poc-github-actions/blob/main/.github/actions/local-action/action.yaml) with an output, the [workflow run](https://github.com/GuillaumeFalourd/poc-github-actions/runs/4238670862?check_suite_focus=true). Let me know if it resolves your problem. – GuiFalourd Nov 17 '21 at 13:49

1 Answers1

9

You can share these output files between jobs using artifacts.

Use the upload-artifact to upload the build files from the Build workflow and download-artifact to download them in the Released workflow.

Build workflow

name: Build

on:
  workflow_call:
    secrets:
      SOME_SECRET:
        required: true

jobs:
  build:
    steps:
      # Your build steps here

      - name: Create build-output artifact
        uses: actions/upload-artifact@master
        with:
          name: build-output
          path: build/

Released workflow

name: Released

on:
  push:
    branches:
      - main

jobs:
  build:
    uses: my-org/some-repo/.github/workflows/build.yml@main
    secrets:
      SOME_SECRET: ${{ secrets.SOME_SECRET }}

  released:
    needs: build

    steps:
      - name: Download build-output artifact
        uses: actions/download-artifact@master
        with:
          name: build-output
          path: build/

      # The "build" directory is now available inside this job

      - name: Upload source maps
        run: something

Bonus tip: Note that the "my-org/some-repo/.github/workflows/build.yml@main" string is case-sensitive. I wasted some time figuring out that that was the cause of the error below.

Error : .github#L1
error parsing called workflow "my-org/some-repo/.github/workflows/build.yml@main": workflow was not found. See https://docs.github.com/en/actions/learn-github-actions/reusing-workflows#access-to-reusable-workflows for more information.

Duncan Lukkenaer
  • 12,050
  • 13
  • 64
  • 97