top of page

LetsDevOps: How to Trigger GitHub Actions Workflow from another Workflow

Introduction


In this article we will learn how to trigger workflow from another workflow. This will help in multiple use cases when you are required to trigger other workflow on same or different Repository.


Architecture




Use Case

In some case it is required to trigger workflow from another workflow.

Lets assume we have two workflow, Workflow1 and Workflow2 and from Workflow1 we suppose to trigger Workflow2.


Method for Trigger

With the help of repository_dispatch event we can trigger workflow from another workflow. By default, all repository_dispatch activity types trigger a workflow to run. You can use the types keyword to limit your workflow to run when a specific event_type value is sent in the repository_dispatch webhook payload.


on:
    repository_dispatch:
        types: [ workflow2 ]


Setup

In this process we will setup two workflow.


Workflow1 --> Triggering Workflow

Workflow2 --> Triggered Workflow


Workflow1 Setup



Configuration:

As part of the Configuration since we are going to trigger the Workflow2 we need to supply below details to workflow1 setup.

  • workflow2_name --> Name of the Workflow2 which needs to be trigger from workflow1

  • workflow2_github_account --> GitHub account of workflow2

  • workflow2_repo_github --> GitHub Repo of workflow2

  • pat_token --> GitHub PAT token of workflow2

  • Payload Details --> These are the value which can be passed to workflow2 while trigger. In sample demo I have used payload_Baseline_Number, payload_Baseline_Revision

  • workflow_trigger.py --> this Python script helps to send the REST API call to Trigger the workflow 2. Script can be found at sample GitHub.


# This is a basic workflow to help you get started with Actions

name: Workflow1

# Controls when the workflow will run
on:
  workflow_dispatch:
    inputs: 
      workflow2_name:
        description: 'Workflow2 Name for Triggering'
        required: true
        default: 'Workflow2'
      
      workflow2_github_account:
        description: 'GitHub Account Owner'
        required: true
        default: 'sumitraj0103'
      
      workflow2_repo_github:
        description: 'GitHub Repository Name'
        required: true
        default: 'Letsdevops'

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    env: 
      pat_token: ${{ secrets.Workflow2_PAT_TOKEN_GITHUB }}
      payload_Baseline_Number: "BSL_001"
      payload_Baseline_Revision: "zsfdgsdbgngffdwdx1dxvv2"

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:

      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v3

      - name: setup python
        uses: actions/setup-python@v2
        with:
         python-version: 3.8 #install the python needed
         
      - name: Install dependencies
        run: |
           cd "$GITHUB_WORKSPACE/Deployment-Scripts/"
           python -m pip install --upgrade pip
           pip install -r requirements.txt
          
      - name: Trigger the Workflow
        run: |
         cd "$GITHUB_WORKSPACE/Deployment-Scripts/"
         python $GITHUB_WORKSPACE/Deployment-Scripts/workflow_trigger.py ${{ env.pat_token }}  ${{ github.event.inputs.workflow2_github_account}}  ${{ github.event.inputs.workflow2_repo_github}} ${{ github.event.inputs.workflow2_name}} ${{ env.payload_Baseline_Number }} ${{ env.payload_Baseline_Revision }}

Workflow2 Setup

Here is the workflow2 Sample file.



Configuration:

As part of the Configuration to accept the trigger we need to make below configuration update in workflow2 file.


  1. repository_dispatch: types: [Workflow2] --> Repository dispatch helps to accept the trigger. Type value will be used by the workflow1. In this case we are using the workflow name is the repository dispatch. So it must be matching with workflow1 to accept the trigger.

  2. This is how we can read the Payload value which is sent by Workflow1.

    • baselineTag: ${{ github.event.client_payload.baselinetag }}

    • revision_number: ${{ github.event.client_payload.revision_number }}



# This is a basic workflow to help you get started with Actions

name: Workflow2

# Controls when the workflow will run
on:
 repository_dispatch:
    types: [Workflow2]
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    env:
       baselineTag: ${{ github.event.client_payload.baselinetag }}
       revision_number: ${{ github.event.client_payload.revision_number }}

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v3

      # Runs a single command using the runners shell
      - name: Run a one-line script
        run: echo Hello, world!

      # Runs a set of commands using the runners shell
      - name: Run a multi-line script
        run: |
          echo Add other actions to build,
          echo test, and deploy your project.
          echo $baselineTag
          echo $revision_number

Trigger the workflow

You can find the Sample Repo at


Once you Trigger workflow1, workflow2 will be automatically triggered.


Demo





bottom of page