Github Actions - trigger another action after one action is completed

GithubGithub Actions

Github Problem Overview


I have one action (a yaml file) for deploying a docker image to Google Cloud Run.

I would like to receive Slack or Email messages informing the build and push results.

How could the message action be triggered after build action is completed?

Is it possible to get the result of the build action?

Github Solutions


Solution 1 - Github

There are 2 options of doing this:

  1. Use a second job inside the same workflow.yml together with the needs keyword
  2. Create a separate notify.yml workflow that uses the workflow_run event as a trigger

1. Same workflow, separate job with needs keyword

In your workflow.yml file you simply define two jobs like this (leveraging the needs: build configuration in the second job):

name: CI build and notify

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    
    - name: Deploy Docker image to Google Cloud Run
      run: ...

  notify:
    needs: build
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Notify Slack and send eMail
        run: ...

As the docs state, the second notify job will only start if the first build job succeeded:

> Identifies any jobs that must complete successfully before this job > will run.

Here's a screenshot of how this approach can look like practically from my own project (I have a second publish-snapshot job instead of your notify job - but the concept stays the same):

build-job-triggers-publish-job

There's also a way to always let the notify job run, even if the build job failed. You have to enhance the needs with a if: always() configuration then.


2. Separate workflow, using the workflow_run event as a trigger

Using the workflow_run event as a trigger we end up having 2 separate GitHub Actions workflow yaml files:

build.yml

name: CI build

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Deploy Docker image to Google Cloud Run
      run: ...

notify.yml

name: CI notify

# Only trigger, when the build workflow succeeded
on:
  workflow_run:
    workflows: ["CI build"]
    types:
      - completed

jobs:
  notify:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Notify Slack and send eMail
        run: ...

A crucial point here is that the name: CI build definition of the first yaml file must exactly match the workflow_run: workflows: ["CI build"] definition in the second yaml file. Another point is that this approach needs to be done on the default branch (which is mostly main or master) as the docs state:

> Note: This event will only trigger a workflow run if the workflow file > is on the default branch.

Here's also a full example project using the 1st option if you're interested.

Solution 2 - Github

First, you are mixing terms here. According to GitHub Actions documentation a single YAML file is called a workflow (not an action) and consists of jobs. Jobs contain a sequence of steps (including actions) that are executed one after another. A particular workflow execution is called a run. Having that in mind lets go the questions.

> How could the message workflow be triggered after build workflow is completed?

You can use GitHub API to trigger a webhook event called repository_dispatch (only for the base branch) or workflow_dispatch. This can be easily done using a dedicated Repository Dispach action in your build workflow.

> Is it possible to get the result of the build workflow?

Yes, the result of the workflow run can be obtained using given GitHub API

But if you only want to send the build result notification of the currently executed workflow you don't need to create a separate workflow and trigger it from the parent. You can use dedicated Slack actions or e-mail actions.

Solution 3 - Github

you can try in step 2 the following directive:

needs: step-1-job-name 

just after job name

Solution 4 - Github

> How could the message action be triggered after build action is completed?

This should now (August 2020) be possible with "GitHub Actions improvements for fork and pull request workflows"

> Another frequently-requested feature for Actions is a way to trigger one workflow based on the completion of another workflow.
For example, you may want to take the results of a CI workflow and run some further analysis.

> The new workflow_run event enables you to trigger a new workflow when one or more workflows are requested or completed.
Runs triggered by the workflow_run event always use the default branch for the repository, and have access to a read/write token as well as secrets.
As an example, as a maintainer you could set up a workflow that takes the artifacts generated by the pull request workflow, do some analysis, and post comments back to the pull request.
This event is also available as a web hook.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCSSerView Question on Stackoverflow
Solution 1 - GithubjonashacktView Answer on Stackoverflow
Solution 2 - GithubMarcin KÅ‚opotekView Answer on Stackoverflow
Solution 3 - GithubsvirinsView Answer on Stackoverflow
Solution 4 - GithubVonCView Answer on Stackoverflow