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:
- A repository with an action defined inside it (repo_01).
- Another repository that needs the action to be triggered (repo_02).
- Personal Access Token (PAT) that will be added at "Secrets".
- 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.
New choose "Developer settings"
Click on "Generate new token"
Provide a descriptive note (or leave it empty) and select the desired scopes of the PAT:
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.
From the left navigation panel, click on "Secrets" then choose "Actions"
Now choose "New Repository secret"
Type "ACTIONS_KEY" as the secret name and enter the "PAT" in the "Secret" field.
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
And Tada! we made it
Workflow_02 is now being triggered by workflow_01
workflow_01
Workflow_02
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:
Top comments (0)