Reputation: 15018
As discussed here, in GitHub Actions there is a nice way of referencing job
s in other job
s using need
keyword, e.g.
name: Share data between jobs
on: [push]
jobs:
job_1:
name: Add 3 and 7
runs-on: ubuntu-latest
steps:
# Steps
job_2:
name: Multiply by 9
needs: job_1
# The rest of the job
The question that I could not find an answer to in the documentation is: Is there a way to reference/share job
s in other workflows? (i.e. separate yml
file).
My project consists of few separate workflows and each of them needs to perform the same initial step
s. I am trying to avoid copy-pasting the same steps across different workflow
s.
Upvotes: 12
Views: 8426
Reputation: 81
It sounds like you'd like to reuse a single job among multiple workflows to keep your work DRY/SPOT (single point of truth) so you don't have to repeat yourself and introduce errors. This can be done by Reusing Workflows.
There is essentially a "caller" workflow, and that calls (runs) the "called" workflow with optional inputs and outputs.
The following .yml file 'uses' the relative path of the called workflow (but can reference other repos with versions etc. - see documentation).
name: Caller for a reusable workflow
on:
push:
branches:
- main
jobs:
example_job:
uses: ./.github/workflows/reusable-workflow-example-called.yml
with:
BUILD_ENV: "Dev"
secrets:
TESTING: ${{ secrets.TESTING }}
And the above workflow calls the workflow below, and optionally passes in variables and secrets.
name: Called reusable workflow
on:
workflow_call:
inputs:
BUILD_ENV:
required: true
type: string
secrets:
TESTING:
required: true
jobs:
called-workflow:
name: Blippedy
runs-on: ubuntu-latest
steps:
- id: do-a-thing
run: |
echo "${BUILD_ENV} is ${TESTING}!"
echo $BUILD_ENV
echo "${{ inputs.BUILD_ENV }} ${{ secrets.TESTING }}"
I use this to run a the same job (Postman tests via newman
) across multiple workflows configured for different environments.
Alternatively, if you're looking for using a single step across jobs/workflows, check out Composite Actions.
Upvotes: 6
Reputation: 14836
At this time, I do not think specifying dependencies between workflows is possible. It is discussed in this GitHub community forum:
How do I specify job dependency running in another workflow?
What you can do is to use the same workflow file and then use conditions to trigger or not a specific job.
If you want to run a job only when there is a push to master branch you can do it like this:
deploy: if: github.event_name == 'push' && github.ref == 'refs/heads/master'
Upvotes: 8