🚀 Github Actions: A simple way to trigger workflow from another workflow
Tags: devops, github, testing, codequality
Published: 2022-10-03 13:51:21

GitHub Actions is a neat and tidy way of automating the workflow in your development environment.

Actions let you define and run simple commands in your development environment using a single interface.

You can write your script as a simple bash file, but defining it as a GitHub action will make it even easier to run (and maintain).

In this article, we will learn how to trigger a workflow from another workflow in a simple and easy way.

**

## Prerequisites:

**
To trigger a workflow from another workflow we need the following:

1. A repository with an action defined inside it (**repo_01**).
2. Another repository that needs the action to be triggered (**repo_02**).
3. Personal Access Token (PAT) that will be added at "Secrets".
4. Simple Command to authorize the other workflow's PAT to call the repository's PAT to initiate the workflow, which will call the **repo_01**'s action to execute the command.

Now after we knowing what we need to make it works, let's make it works :D


Generating PAT:

To generate Personal Access Token for a user account we need to navigate to user profile tab > and in dropdown menu select "Settings" option > Personal access tokens.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u22j0tj7iz8zvce94e6u.png)

New choose "Developer settings"


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/txojunubn5y0ncavmgs2.png)




Click on "Generate new token"


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kk34yugctcrlmlgn096u.png)


Provide a descriptive note (or leave it empty) and select the desired scopes of the PAT:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/03at784cag7jk8su3upy.png)


> **Note**: PAT is secret and should not be shared; this is to prevent unauthorized access to your account and other repos.


We need to add the PAT to the secrets section of the **repo_01** repositories permissions.

Navigate to "Settings" tab from the top navigation menu.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwbc9p7gus6frvnqrvgd.png)

From the left navigation panel, click on "Secrets" then choose "Actions"


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4tb0ueqk3ny0opxpba9z.png)


Now choose "New Repository secret"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mc2eiejwbyjem50te1gy.png)


Type "ACTIONS_KEY" as the secret name and enter the "PAT" in the "Secret" field.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10gp8f4i5bk0n40c2hj2.png)



## **Creating workflows:**

Now we will create the workflow for the two repos.

First navigate to "Actions" of the first repo and click on "New Workflow".

You can choose between "Set up a workflow yourself" or using one of the suggested workflows depending on your need.

Do the same steps at the **repo_02** repository to create the second workflow.

Last but not least, we need to configure the two workflows to interact with each other by calling one from the other.

Use the following script at repo_01 (The trigger flow).

> Replace userName with the Girhub userName and repoName with the actual Repository name name and the related url to the repository.

```	

name: workflow_01
on:
  workflow_dispatch:
     inputs:
      workflow_02:
        description: 'ًWorkflow 2 which will be triggered'
        required: true
        default: 'workflow_02'
        
      workflow2_github_account:
        description: 'GitHub Account Owner'
        required: true
        default: ':userName'
        
      workflow2_repo_github:
        description: 'repo-name'
        required: true
        default: ':repoName' 
           
jobs:
  build:
    runs-on: ubuntu-latest
      
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - name: Test
        run: |
          curl -X POST https://api.github.com/repos/:userName/:repoName/dispatches \
          -H 'Accept: application/vnd.github.everest-preview+json' \
          -u ${{ secrets.ACTIONS_KEY }} \
          --data '{"event_type": "Trigger Workflow", "client_payload": { "repository": "'"$GITHUB_REPOSITORY"'" }}'
      - uses: actions/checkout@v3
```	

And at repo_02, add the follwoing code to the actions file:

```
name: workflow_02
on: [repository_dispatch]
jobs:
  cypress-run:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Triggering Wokflow 2"
      - name: Checkout
        uses: actions/checkout@v2
```

Finally, let's try it :D

Navigate to repo_01 > Actions > workflow_01 > run workflow


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nfu20gbbru73cmzj8g74.png)


And Tada! we made it

**Workflow_02** is now being triggered by **workflow_01**


**workflow_01**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mcfczkkpoc3cvpmc4wbt.png)


**Workflow_02**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfh9avvp8rk5cfqm4d11.png)

That's all folks! 🎉


To learn more about github triggeres and events:
https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows

References:

[GitHub](https://github.com)
[LetsDevOps](https://www.letsdevops.net/post/letsdevops-trigger-workflow-from-another-workflow-using-github-actions)