How do I specify job dependency running in another workflow?

I’ve this workflow:

name: Testing
on: [push, pull_request]

jobs:
  testing:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: Setup Environment (Using NodeJS 12.x)
        uses: actions/setup-node@v1
        with:
          node-version: 12.x

      - name: install dependencies
        run: npm install

      - name: Run unit tests and collect coverage
        run: npm run test:unit

Now, I would like to create another workflow (deploy.yml) to deploy my application on Netlify.

How can specify that this deploy depends of the successfully run of “testing” job ?

“testing” and “deploy” are in different workflows files.

name: Deploy
on:
  push:
    branches:
      - master
jobs:
deploy:
# ... ??? ...

Thanks.

8 Likes

You cant.

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:

jobs:
   deploy:
       if: github.event_name == 'push' && github.ref == 'refs/heads/master'

Another option for deployments is to create a tag when you are ready to deploy and then having a seperate workflow that is listening to the push tag event and trigger your deploy.

9 Likes

Will this be a feature that will be added in the future?

27 Likes

I think it should be supported!

3 Likes

I would love to have such a feature. Perhaps it is simple (for GitHub) to implement, by adding another “on” event trigger?

on:
  workflow_complete:
workflow: Test
    types: [success]

jobs:
  ...

or

on:
  workflow_success:
    workflow: Test

Seems like a must have for “test then deploy” flow, where it makes sense to separate the two workflows instead of toying around with conditions and have everything as one pile of YAML mess.

14 Likes

This solution would cause a problem if you are using matrix. It will run for every matrix item basically.

This really needs to be solved. I can’t believe this doesn’t work!

It’s a pretty common requirement.

  1. I need to run tests in a matrix.

 2. I also want to deploy only on master, but only if all tests pass.

Right now this is basically impossible.

8 Likes

When will this be supported?

4 Likes

I have try this code,

on:
  workflow_success:
    workflow: Test

but it does not work , and show me

`workflow_success` is not a valid event name

Same here, I’m facing the same problem. On every master push I need to test different JDK versions and then I publish a package snapshot, but i don’t know how to solve it.

3 Likes

Instead of creating a new workflow, you might just create a dependent job. See the documentation for jobs.<job_id>.needs.

1 Like

Yes, you can put it all into different jobs in one workflow, but breaking up your large files into logically separated smaller files is a pretty universal programming best practice, and that’s extra true when you have a lot of different things branching off from one spot (like your tests passing or whatever). If they want to define a workflow as all of those branches together, that’s fine, but that means we need to be able to split out jobs into different files. Otherwise, we’re forced to either write code in some scripting language that builds into the yaml code for our workflow or create some sort of funky trigger event, both of which are incredibly hacky.

1 Like

This is possible using the GitHub Workflow runs API. Assuming Testing workflow fails if the testing step fails, in the Deploy workflow, you get the conclusion of the latest Testing workflow run using some cURL and jq foo, and then exit if the output is not success. See this project, where publish depends on ci.