Home > database >  GitHub Actions Reuse Workflow Definitions
GitHub Actions Reuse Workflow Definitions

Time:11-28

I have a project where I have two GitHub actions yml file where the first file is called build.yml and it contains instructions to compile, build and test the project. It is as simple as this:

name: build my-project

on:
  push:
    paths-ignore:
      - 'images/**'
      - README.md
    branches:
      - master
  pull_request:
    branches:
      - master
  release:
    types: [ created ]

env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: cache ivy2
        uses: actions/cache@v1
        with:
          path: ~/.ivy2/cache
          key: ${{ runner.os }}-sbt-ivy-cache-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }}

      - name: sbt Test
        run: sbt clean test

I now have another yml file that contains the instructions to do a release based on annotated tags. It is like this:

name: release my-project

on:
  push:
    # Sequence of patterns matched against refs/tags
    tags:
      - 'v[0-9] .[0-9] .[0-9] -[a-zA-Z]*'

env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
  build:
    uses: ./.github/workflows/build.yml

  publish:
    runs-on: ubuntu-latest
    needs: test # See build.yml file where the test job is defined
    # If there is a tag and if that tag comes from master branch
    if: startsWith(github.ref, 'refs/tags/v')
    steps:
      - name: checkout
        uses: actions/checkout@v3

      - name: capture changelog
        id: changelog
        uses: metcalfc/[email protected]
        with:
          myToken: ${{ secrets.GITHUB_TOKEN }}

      - name: sbt ci-publish-github
        run: sbt publish

      - name: ci-release-github
        id: create-release
        uses: actions/create-release@latest
        with:
          allowUpdates: true
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          body: |
            ## What's Changed
            ${{ steps.changelog.outputs.changelog }}
          draft: false
          prerelease: false

I just created an annotated tag which then resulted in an error like this:

Invalid workflow file: .github/workflows/publish.yml#L14
error parsing called workflow "./.github/workflows/build.yml": workflow is not reusable as it is missing a `on.workflow_call` trigger

So basically what I want is, when I push an annotated tag, I want to first run the test job from build.yml and then once that succeeds, I would like to run the publish job. Any suggestions on how to get this straight?

CodePudding user response:

So basically what I want is, when I push an annotated tag, I want to first run the test job from build.yml and then once that succeeds, I would like to run the publish job. Any suggestions on how to get this straight?

You almost got it right with your implementation. You just need a few modifications:

  • The build job needs to depends on the publish job:
name: release my-project

on:
  push:
    # Sequence of patterns matched against refs/tags
    tags:
      - 'v[0-9] .[0-9] .[0-9] -[a-zA-Z]*'

jobs:
  publish:
     [ ... ]

  build:
    needs:
      - publish
    uses: ./.github/workflows/build.yml
  • The build needs the workflow_call trigger (as stated by the error message - Reference):
on:
  workflow_call:
  push:
    [ ... ]

Note: You could even share the tag value from the previous workflow, sending it as input to the second one by using:

on:
  workflow_call:
    inputs:
      tag:
        required: true
        type: string

Calling the reusable workflow that way from the main workflow:

  build:
    needs:
      - publish
    uses: ./.github/workflows/build.yml
    with:
      tag: 'MY TAG'

CodePudding user response:

I was able to fix it by adding the following in my publish.yml:

jobs:
  tests:
    uses: ./.github/workflows/build.yml

  publish:
    runs-on: ubuntu-latest
    needs: [tests] # See build.yml file where the test job is defined

In my build.yml, I had to add the following:

on:
  push:
    paths-ignore:
      - 'images/**'
      - README.md
    branches:
      - master
  pull_request:
    branches:
      - master
  release:
    types: [ created ]
  workflow_call:

Notice that workflow_call: entry that needs to be added explicitly.

  • Related