如何在推送到另一个分支时触发 Github Actions 工作流程?

How to trigger a Github Actions workflow on push to another branch?

当我将一些代码推送到 master 时,一个工作流文件 运行s。此文件构建工件并将代码推送到另一个分支 production.

production 发生任何推送时,另一个工作流文件(如下所示)设置为 运行。

name: Deploy

on:
  push:
    branches:
      - production

jobs:

# Do something

  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@master

但是这个工作流文件永远不会 运行。我希望当工作流文件完成后,监听 master 上的推送事件,这个文件应该 运行 因为前一个文件将代码推送到 production 分支。我如何确保发生这种情况?

您需要使用个人访问令牌 (PAT) 来推送工作流程中的代码,而不是默认的 GITHUB_TOKEN:

Note: You cannot trigger new workflow runs using the GITHUB_TOKEN

For example, if a workflow run pushes code using the repository's GITHUB_TOKEN, a new workflow will not run even when the repository contains a workflow configured to run when push events occur.

If you would like to trigger a workflow from a workflow run, you can trigger the event using a personal access token. You'll need to create a personal access token and store it as a secret.

https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows#triggering-new-workflows-using-a-personal-access-token

name: Push to master

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    # the checkout action persists the passed credentials by default
    # subsequent git commands will pick them up automatically
    - uses: actions/checkout@v2
      with:
        token: ${{secrets.PAT}}
    - run: |
        # do something
        git push