How to trigger Github Workflow after a check suite finishes?

为君一笑 提交于 2020-12-13 18:06:48

问题


I want to trigger a workflow only if a particular workflow finishes... does anyone know how to do that?

Some context:

  • I have a workflow Tests and another called Build-feature.
  • I run my Tests workflow on every PR to branch feature.
  • If something is pushed/merged to branch feature then i want to run workflow Tests and only if that succeeds then i want to run Build-feature workflow.

Also there's the event check_suite that it's supposed to trigger a workflow: https://help.github.com/en/actions/automating-your-workflow-with-github-actions/events-that-trigger-workflows#check-suite-event-check_suite

I tried this example:

on:
  check_suite:
    types: [rerequested, completed]

But my workflow never triggers, any ideas why? or any other idea on how can i achieve the above?.


回答1:


Updated answer:

Turns out that you can achieve that using the on: status event, but you need to manually trigger the status with a token that is not the one from the Github Actions.

You need to add something like this in order to trigger the status event after a workflow finishes:

      - name: Trigger status event
        run: >-
          curl -L -X POST 
          -H "Content-Type: application/json"
          -H "Authorization: token ${{ secrets.GITHUB_PAT}}"
          -d '{
            "state": "success",
            "target_url": "https://you-can-add-a.url/",
            "description": "All tests passed", "context": "your-custom/status-event"
          }'
          "https://api.github.com/repos/${GITHUB_REPOSITORY}/statuses/${GITHUB_SHA}"

And in your other workflow you can just add an if condition in case you have more than one status event that can trigger the workflow, something like this:

on: status

...

jobs:

...

  if: github.event.context == 'your-custom/status-event'

And that's it... That's how you can chain workflows.



Old answer:

Well, after asking at the github.community forum i got an answer.

Events raised from the Actions app do not trigger workflows. This restriction is currently in place in order to prevent a circular workflow execution.

Related links:

  • https://github.community/t5/GitHub-Actions/on-check-suite-never-gets-called/td-p/38955
  • https://github.community/t5/GitHub-Actions/on-check-suite-never-triggers-why/m-p/42891/highlight/false#M5162

Edit: The same applies to check events.




回答2:


Usually you would use steps. The "build" step is only executed if the previous "Test" step was successful.

If you really need to model it via jobs, than you can use needs.

Example:

jobs:
  job1:
  job2:
    needs: job1
  job3:
    needs: [job1, job2]

The check_suite event is only triggered by (external) integrations, like Netlify builds. Note: the event is also only triggered, if you workflow is already merged to your default branch (master).



来源:https://stackoverflow.com/questions/59595803/how-to-trigger-github-workflow-after-a-check-suite-finishes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!