Projects Blog

Run a GitHub action after another action has finished

Last updated on

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?

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.

.github/workflows/first-action.yml
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:

.github/workflows/second-action.yml
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

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:

.github/workflows/second-action.yml
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! 🚀

Related posts