GitHub Actions restricts one workflow trigger another workflow in order to prevent users from accidentally creating recursive workflow. I spent some time on figuring how to do it.

Preface

According to GitHub: Users have to use Personal Access Token on the workflow which wants to trigger another GitHub Actions workflow.

When you use the repository's GITHUB_TOKEN to perform tasks on behalf of the GitHub Actions app, events triggered by the GITHUB_TOKEN will not create a new workflow run. This prevents you from accidentally creating recursive workflow runs. For example, if a workflow pushes code using the repository's GITHUB_TOKEN, a new workflow will not run even if that workflow in the same repository is configured to be executed when push events occur."

It looks easily, but there are lots of traps while doing so actually. I tried many different ways to achieve this and finally made it work.

There are other approaches like:

  • using another bot user with new personal access token
  • using GitHub App access token

But this post will only focus on using your own Personal Access Token.

Solution

  • Create a GitHub Personal Access Token with repo permissons at https://github.com/settings/tokens
  • Copy that access token and paste it in the Secrets - Settings page of the target repo with the name MY_GITHUB_TOKEN
  • In the yml file of the workflow you want to trigger another workflow, you have to write these lines in the CHECKOUT step:
    - name: Check out repo
      uses: actions/checkout@v2
      with:
        token: ${{ secrets.MY_GITHUB_TOKEN }}
        persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal access token
        fetch-depth: 0 # otherwise, you will failed to push refs to dest repo
  • In the yml file of the workflow you want to trigger another workflow, you have to write these lines in the PUSH step:
    - name: Push changes
      uses: ad-m/github-push-action@master
      with:
        github_token: ${{ secrets.MY_GITHUB_TOKEN }}
        branch: 'source'    # default using 'master', change it if you want to push to another branch

Result & Example

I am using one GitHub Actions workflow with on:schedule to auto-generate weekly post for my blog.
Meanwhile, I have another workflow which will auto-build my blog when there is new commit being pushed. I want it work finely after a new weekly post being pushed. Here's the yml file of that "auto-generate weekly post" workflow: https://github.com/M157q/fkz.github.io/blob/source/.github/workflows/generate_weekly_post.yml

References


Share


Donation

如果覺得這篇文章對你有幫助, 除了留言讓我知道外, 或許也可以考慮請我喝杯咖啡, 不論金額多寡我都會非常感激且能鼓勵我繼續寫出對你有幫助的文章。

If this blog post happens to be helpful to you, besides of leaving a reply, you may consider buy me a cup of coffee to support me. It would help me write more articles helpful to you in the future and I would really appreciate it.


Related Posts