Search code examples
githubcontinuous-integrationdevopscontinuous-deploymentgithub-actions

GitHub Actions: sharing/referencing jobs between workflows


As discussed here, in GitHub Actions there is a nice way of referencing jobs in other jobs 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 jobs 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 steps. I am trying to avoid copy-pasting the same steps across different workflows.


Solution

  • 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'