DEV Community

Moaaz Adel
Moaaz Adel

Posted on

๐Ÿš€ Github Actions: A simple way to trigger workflow from another workflow

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

New choose "Developer settings"

Image description

Click on "Generate new token"

Image description

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

Image description

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

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

Image description

Now choose "New Repository secret"

Image description

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

Image description

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


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

Finally, let's try it :D

Navigate to repo_01 > Actions > workflow_01 > run workflow

Image description

And Tada! we made it

Workflow_02 is now being triggered by workflow_01

workflow_01

Image description

Workflow_02

Image description

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
LetsDevOps

Top comments (0)