【问题标题】:How to trigger a Github Actions workflow on push to another branch?如何在推送到另一个分支时触发 Github Actions 工作流?
【发布时间】:2021-01-12 12:54:38
【问题描述】:

当我将一些代码推送到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 上监听推送事件)完成后,该文件应该运行,因为之前的文件将代码推送到 production 分支。我如何确保发生这种情况?

【问题讨论】:

标签: git workflow continuous-deployment github-actions


【解决方案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

【讨论】:

    猜你喜欢
    • 2023-01-11
    • 2022-01-26
    • 2022-11-02
    • 2022-01-10
    • 2021-10-14
    • 2021-02-10
    • 2020-05-09
    • 2021-02-14
    • 2020-01-25
    相关资源
    最近更新 更多