2

I have 2 conditions under which I want to run my workflows in GH actions on a go project

  • run unit tests on every PR (I have already taken care of this with the following workflow.yaml

name: unit-tests

on: [ pull_request ]

jobs:
  unit-tests:
    runs-on: Linux

    steps:
      - name: checkout project
        uses: actions/checkout@v2

      - name: dynamically retrieve go version
        uses: arnested/go-version-action@v1
        id: go-version

      - name: setup go ${{ steps.go-version.outputs.minimal }}
        uses: actions/setup-go@v2
        with: 
          go-version: ${{ steps.go-version.outputs.minimal }}

      - name: run unit tests
        run: go test -race -cover -v ./... -run Unit

My second requirement is the following:

  • run unit tests AND proceed to creating a tag + release if the tests succeed and the target branch is master

My question is the following: Since the on clause is unique per workflow, is there a way I can leverage the above workflow (without the need to write a more or less clone) that can be integrated in the second use case?

pkaramol
  • 16,451
  • 43
  • 149
  • 324

1 Answers1

2

First, reusing workflows is available since Oct. 2021

That means you can make a workflow on master branch, which would "call" (reuse) your second (existing) workflow, itself with its on: directive

The call would look like:

jobs:
  call-workflow-passing-data:
    uses: octo-org/example-repo/.github/workflows/reusable-workflow.yml@main
    with:
      username: mona
    secrets:
      envPAT: ${{ secrets.envPAT }}
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250