githubgithub-actions

GitHub Actions - trigger an action on multiple conditions


I have an release action for deploying new releases by conditions (run only when tag pushed):

name: Release

on:
  push:
    tags:
      - 'v[0-9].[0-9]+.[0-9]+'

# ...

and have some other actions, which runs on push. For example:

name: Test

on: [push]

# ...

I would like to add condition to Release action 👉🏻 run only tag was pushed and all other actions successfully completed. For example:

on:
  push:
    tags:
      - 'v[0-9].[0-9]+.[0-9]+'
  # here need "AND" condition
  workflow_run: 
    workflows: [ "Lint", "Test", "Integration test" ]
    types:
      - completed

# ...

How to combine conditions for trigged in the on value?


Solution

  • This is not exactly what you're hoping for, based on your question, but this is how I solve the problem of having my release workflow depend on tests: use reusable workflows, with release declaring needs: tests.

    Release workflow:

    name: Release
    
    on:
      push:
        tags:
          - 'v[0-9].[0-9]+.[0-9]+'
    
    jobs:
      tests:
        uses: ./.github/workflows/tests.yml
        secrets: inherit
    
      release:
        runs-on: [...]
        needs: tests
        steps:
        [...]
    

    And to make this work, I make my tests workflow callable with on: workflow_call:

    name: Tests
    
    on:
      - pull_request
      - push
      - workflow_call
    
    jobs:
      [...]
    

    The result is that my release workflow runs the tests job which invoked my Tests workflow, and if that passes, it runs the release job.

    The downside of this approach is that on a tag push, Tests gets run twice: once on the push (because it has on: push) and again invoked by Release. I'm OK with that, it's up to you to figure out if you are.