How to make one workflow dependent on the other?

I am operating on the following two workflows -

I would want to make pr_new.yaml dependent on commit.yaml. What should I be looking for?

Here’s my repository (along with the desired branch): https://github.com/sayakpaul/wine/tree/experiment-1/.

Hi @sayakpaul,

It’s not supported to set pull_request event depending on push event.

Typically there are two methods to set ‘dependence’:

  1. Use two jobs in one workflow, the job2 needs job1.
jobs:
  job1:
  job2:
    needs: job1

Doc here: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idneeds

  1. Use event ‘workflow_run’, it can be used as a trigger when another workflow is requested or completed.
on:
  workflow_run:
    workflows: ["Run Tests"]
    branches: [main]
    types: 
      - completed
      - requested

Doc here: https://docs.github.com/en/actions/reference/events-that-trigger-workflows#workflow_run

The limitation is you cannot get the pull request number in both methods( ${{ github.event.number }}). Instead, you can get all pull request of the repo via rest api, parse the pull request whose head ref is your branch to get the pull request number.

Thanks

Thanks, @weide-zhou. The problem I am facing is that the pull_request trigger is fetching the backdated values from run.txt which gets updated during the commit. If you see this PR the two run URLs differ from each other:

The commit message one is the latest while the commented one is the older one.

Any ways to solve this other than making the workflows independent?

Hi @sayakpaul,

So the problem is how to make sure the 2nd workflow get the latest run.txt url.
Since it’s not supported to depend pull request event on push event, you can use two jobs as a workaround(not ideal but should work).

name: test
on: [push]

jobs:
  commit:
    runs-on: ubuntu-latest
    outputs:
      string: ${{ steps.changestring.outputs.string}}
    steps:
      - uses: actions/checkout@v2
      - name: generate new url and change to run.txt
         run: | 
              ....... # Your steps to generate new url and store to run.txt
      - name: set as output
        id: changestring
        run: |
          echo "::set-output name=string::$(<run.txt)"

  PR-Comment:
    runs-on: ubuntu-latest
    needs: [commit]
    steps:
      - name: add PR issue comment
        run: |
          PRnumber=$(curl -s -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/${{github.repository}}/pulls?head=${{github.repository_owner}}:${{github.ref}} | jq -r '.[] | .number')
          echo "::set-env name=body::${{needs.commit.outputs.string}}"
          echo "::set-env name=number::$(echo $PRnumber)"
      - name: PR Comment
        uses: actions/github-script@v3
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          script: |
            github.issues.createComment({
              issue_number: ${{ env.number}},
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: '${{ env.body }}'
            })

Now you get the PR number and new URL, you can use them to add PR comment.

It works on my side, please check my workflow for your reference.


Note: This works when one pull request from head branch, if you have mutiple PRs, you can get a list of pull request number, and use a loop to add PR comment.

Thanks

Edited: fixed env code, and provide a sample workflow.

1 Like

Thank you very much for the detailed explanation. I tried adapting your workflow (see here) but the body is not getting set and I am not sure why. Am I missing out on something?

Hi @sayakpaul,

I notice the body is empty in your ‘PR-Comment’ job (value of ${{needs.commit.outputs.string}}), this is due to you didn’t set it in the dependent job ‘commit’. Please fix it and give a try. :grinning:

jobs:
  commit:
    runs-on: ubuntu-latest
    outputs:
      string: ${{ steps.changestring.outputs.string}}    # set the job output in commit job then it can be got from PR-Comment job.

Thanks

@weide-zhou tried that but still no luck. Workflow file here. Workflow output here.

Hi @sayakpaul,

The set-ouput command in your job commit doesn’t work correctly, it seems the two commands messed.

Please remove cat command, make sure the output value is correctly set. (your error should be due the run.txt format, since if i directly create run.txt on github web, the cat and set-output commands work well).

      - name: Store the run URL
        id: changestring
        run: |
          echo "::set-output name=string::$(cat run.txt)"


Thanks.

1 Like

I see. I just tried your fix. It works seamlessly now :slight_smile:
Thank you!