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

时间:2020-09-26 10:55:25

标签: git workflow continuous-deployment github-actions

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

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

name: Deploy

on:
  push:
    branches:
      - production

jobs:

# Do something

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

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

1 个答案:

答案 0 :(得分:1)

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

注意:您无法使用GITHUB_TOKEN

触发新的工作流程运行

例如,如果工作流运行使用存储库的GITHUB_TOKEN推送代码,则即使存储库包含配置为在发生push事件时运行的工作流,新的工作流也不会运行。

如果要从工作流程运行中触发工作流程,则可以使用个人访问令牌触发事件。您需要创建一个个人访问令牌并将其存储为秘密。

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