Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github Actions - trigger another action after one action is completed

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?

like image 509
CSSer Avatar asked Jul 06 '20 06:07

CSSer


People also ask

How do I call one workflow from another workflow in GitHub Actions?

You call a reusable workflow by using the uses keyword. Unlike when you are using actions within a workflow, you call reusable workflows directly within a job, and not from within job steps.

Do GitHub Actions jobs run sequentially?

To run jobs sequentially, you can define dependencies on other jobs using the jobs. <job_id>. needs keyword. Each job runs in a runner environment specified by runs-on .


1 Answers

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.

like image 98
jonashackt Avatar answered Sep 27 '22 20:09

jonashackt