· Travis Rodgers · Programming · 3 min read
How To Call a GitHub Actions Workflow From Another Workflow
So you’re creating a workflow in GitHub actions and you want to call another workflow from it.
Let’s say you have a GitHub action workflow that deploys your code to your website. We’ll call the file main.yml
.
You also have another GitHub action workflow that gets the most popular posts from Google Analytics and updates a file in your repo that your website will use for its “trending posts.” We’ll call this file popular.yml
.
But after you get the most popular posts from Google Analytics, you want to then deploy those changes to your site.
So instead of rewriting all the deploy YAML again in that workflow, you just want to “re-call” main.yml from popular.yml so that it will deploy the changes.
Easy.
Let’s say popular.yml looks like this:
name: Update Popular Pages
on:
schedule:
- cron: "0 */12 * * *"
jobs:
build:
# Here runs your popular posts automation and updates the file.
And after this build job, you want to call the other GitHub Action workflow (main.yml) from this one.
First, we’ll add a new job title called deploy:
name: Update Popular Pages
on:
schedule:
- cron: "0 */12 * * *"
jobs:
build:
# Here runs your popular posts automation and updates the file.
deploy:
Then, we want to make sure that build is finished successfully before starting the deploy job, so we’ll need to use needs
like so, with our build job being referenced:
name: Update Popular Pages
on:
schedule:
- cron: "0 */12 * * *"
jobs:
build:
# Here runs your popular posts automation and updates the file.
deploy:
needs: build
Next, we can reference the other workflow (main.yml) using uses
name: Update Popular Pages
on:
schedule:
- cron: "0 */12 * * *"
jobs:
build:
# Here runs your popular posts automation and updates the file.
deploy:
needs: build
uses: ./.github/workflows/main.yml # whatever your file name is
And then finally, since we are calling the main.yml workflow (which is using Actions secrets) from this popular.yml workflow, we need to either pass our particular secrets to it, or we need to make available the Actions secrets that we’ve saved in this particular repo (inherit). More information on passing secrets can be found here. Here we want to make them all available, so we’ll add:
name: Update Popular Pages
on:
schedule:
- cron: "0 */12 * * *"
jobs:
build:
# Here runs your popular posts automation and updates the file.
deploy:
needs: build
uses: ./.github/workflows/WF-1.yml # whatever your file name is
secrets: inherit
And that’s it.
Well, almost.
The final key step is that we need to add workflow_call
as a trigger.
Most forget this step, but you can’t call a workflow from another workflow without this specification. So, let’s add it to our main.yml (the workflow we want to call).
name: Website Deploy
on:
push:
branches: [main]
workflow_call: # <---- ADD THIS
jobs:
build:
# .....deploy config
And that’s it!
In popular.yml
, the Build job will run first. When that is successfully completed, the Deploy job will run, referencing the other workflow and thus deploying the changes.