Workflow_run completed event triggered by failed workflow

I have a workflow triggered as follows:

name: Production deploy
on:
  workflow_run:
    workflows: ["Stage deploy"]
    types:
      - completed

I expect that after the stage deploy (which also runs API tests) completes successfully, then the production deploy would start. However, I just watched a failed stage deploy (which skipped its API tests due to the failed deploy) also trigger a production deploy. Is there a different “types” value for workflow_run that causes it not to fire if the workflow failed, rather than completed successfully?

5 Likes

@cuporter,

Currently, there are only the two types requested and completed for the workflow_run event.

As a workaround, you can use the expression “github.event.workflow_run.conclusion” to get the result of the previous workflow run, and use the job 's if conditional to skip all the job in the current workflow run when the previous workflow run is not success.

name: Production deploy
on:
  workflow_run:
    workflows: ["Stage deploy"]
    types:
      - completed

jobs:
  job1:
    if: ${{ github.event.workflow_run.conclusion == 'success' }}

  job2:
    if: ${{ github.event.workflow_run.conclusion == 'success' }}

    . . .

  jobN:
    if: ${{ github.event.workflow_run.conclusion == 'success' }}

Of course, you also can directly report a feature request that add more types for the workflow_run event (such as success and failure) here.
That will allow you to directly interact with the appropriate engineering team, and make it more convenient for the engineering team to collect and categorize your suggestions.

8 Likes

how do you use github.event.workflow_run.conclusion == 'success' when you have multiples workflows?

name: Production deploy
on:
  workflow_run:
    workflows: ["CI", "Linters"]
    types:
      - completed
1 Like

@edudepetris I assume that both CI and Linters both trigger separate workflow_run events, i.e. you only see a single conclusion, from one of the parent workflows. You may want to verify this with e.g.

name: Production deploy
on:
  workflow_run:
    workflows: ["CI", "Linters"]
    types:
      - completed
jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - name: Print github context
        env:
          GITHUB_CONTEXT: ${{ toJSON(github) }}
        run: echo "$GITHUB_CONTEXT"

If I’m right, then it might not be possible to wait for both parent workflows to succeed and then run the production deployment, unless you let Linters trigger CI and then let CI trigger Production deploy…

@brightran your suggestion works great, however, it is not documented in the webhook events docs: https://docs.github.com/en/free-pro-team@latest/developers/webhooks-and-events/webhook-events-and-payloads#workflow_run

I wonder if this property is authoritative, or will be removed in the future?

1 Like

How was this ever considered useful or fit for production use in it’s current form?
Triggering workflows on completion regardless of status is so naïve, it has no applicability in the real world

The workaround, aside from being ugly and error prone, still results in the workflow being triggered so you end up with hundreds of skipped runs when you look at the UI

10 Likes

this was frustrating me enough that I went ahead and created a new Action to deal with the shortcomings of workflow_run, namely:

The workflow_run event occurs when a workflow run is requested or completed, and allows you to execute a workflow based on the finished result of another workflow.

example
on:
workflow_run:
  workflows: [ test ]
  types: 
    - completed

However by itself, this doesn’t quite work as expected.

  1. The completed type, does not indicate success, for that you’d have to include the following in each job of your workflow:

    if: ${{ github.event.workflow_run.conclusion == 'success' }}
    
  2. If you’re depending on more than one workflow, then ANY of them completing, will trigger the event

    example
    name: deploy
    
    on:
    workflow_run:
      workflows: [ test, lint, compile ]
      types: 
        - completed
    

    if your test workflow fails, but lint completed successfully, github.event.workflow_run.conclusion == 'success' will still be true

  3. Your workflow will trigger as many times as you have workflow dependencies

    in the previous example, our deploy workflow, will run 3 times!

All this makes the workflow_run event fundamentally broken for any advanced usage, this Action aims to remedy that:

2 Likes

workflow_run is so useless, I was SHOCKED to see the workflow triggered regardless of failure or success.

1 Like

An alternative to conditionals on conclusion.