How to trigger a github action only when a tag is pushed and after a workflow is completed

Viewed 1577

I want to trigger a release github action only when these two conditions are true:

  • I push a new tag
  • The test github action (unit tests) was successfully run

This is what I tried:

name: Semantic Release

on:
    push:
        tags:
            - 'v*'
    workflow_run:
        workflows:
            - "test"   # basically i have another test action to run unit tests
        types:
            - completed
jobs:
    release:
        name: "Release on Pypi"
        runs-on: ubuntu-latest
        concurrency: release

        steps:
            -   uses: actions/checkout@v2
                with:
                    fetch-depth: 0

            -   name: Python Semantic Release
                uses: relekang/python-semantic-release@master
                with:
                    github_token: ${{ secrets.GITHUB_TOKEN }}
                    repository_username: __token__
                    repository_password: ${{ secrets.PYPI_TOKEN }}

I set my test action to trigger when I push to master, which is working fine. However, the release action is always triggered after the test action even when I do not push a new tag.

I want the release action to trigger only when I push a new tag and wait for the test action to complete successfully. Why is my example is not working in this case?

1 Answers

Triggers are treated as 2 separate triggers - they combine as OR not AND. So in your case, it's either a tag push or workflow_run completed.

There is no way to filter out workflow_run trigger by tag I'm afraid.

How I would solve this is to trigger your Semantic release directly from your Test workflow using workflow dispatch event:

- name: Invoke workflow in another repo with inputs
  uses: benc-uk/workflow-dispatch@v1
  with:
    workflow: Semantic Release
    repo: yourcompany/yourrepo

You have to make your Semantic Release to be triggered by workflow_dispatch first to make it work:

on:
    workflow_dispatch
Related