There are cases where we want to run a GitHub action just after another separated and independent action finishes.
In the official GitHub Actions documentation, we can find a lot of events that trigger workflows. One of these events perfectly aligns with our needs: workflow_run.
How to do it?"undefined" anchor link
Let’s suppose we have an action with name First action in the file first-action.yml.
This action can be manually triggered from the repository’s Actions tab and is scheduled to run every Monday at 17:00.
name: First action
on: workflow_dispatch: schedule: - cron: '0 17 * * 1'
jobs: first: runs-on: ubuntu-latest steps: - run: echo "First action successfully finished!"After the execution of First action, we aim to run Second action, specified in the file second-action.yml.
For achieving this, we have to add, within the field on, a workflow_run field with the following configuration:
-
workflows: Specifies the name of the workflow (or workflows) that will trigger the current workflow. In this case, it will be triggered when the workflow namedFirst actionhas finished. -
types: limits your workflow to run only for specific activity types. This workflow will be triggered only when the specified workflow completes their execution. The available activity types arecompleted,requestedandin_progress.
name: Second action
on: workflow_dispatch: workflow_run: workflows: ['First action'] types: [completed]
jobs: second: runs-on: ubuntu-latest steps: - run: echo "Second action successfully finished!"Now, Second action will run every time First action finishes.
This might be sufficient in many cases, but sometimes we need to be more precise and dispatch the workflow only if the First action succeeds.
Running a GitHub workflow after another succeeds"undefined" anchor link
For running a job based on the conclusion of another workflow -in this case, success- we can add a conditional statement with the github.event.workflow_run.conclusion property:
name: Second action
on: workflow_dispatch: workflow_run: workflows: ['First action'] types: [completed]
jobs: second: runs-on: ubuntu-latest if: ${{ github.event.workflow_run.conclusion == 'success' }} steps: - run: echo "Second action successfully finished!"And this time, Second action will run every time First action succeeds.
I hope you found this article useful.
Happy coding! 🚀