更新另一个操作的工作流文件的GitHub Action

4
我在同一仓库中有两个GitHub Actions。我试图从一个Action更新另一个Action,但是当我尝试提交和推送更改时,出现以下错误:
``` ! [remote rejected] HEAD -> some-branch (refusing to allow a GitHub App to create or update workflow .github/workflows/the-other-action.yml without workflows permission) ```
这是我正在运行的GH Action的简化版本:
name: my-action

on:
  workflow_dispatch:
  schedule:
    - cron: "0 9 * * *"

jobs:
  components:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout the code
        uses: actions/checkout@v2

      - name: Update the other Action
        run: |
          # Do something to .github/workflows/the-other-action.yaml here

      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v3
        with:
            token: ${{ secrets.GITHUB_TOKEN }}
            branch: some-branch
            commit-message: Updated stuff

我正在尝试弄清如何授予工作流对GITHUB_TOKEN的权限,但不确定该如何操作?
(上下文:我每天运行此操作,以检查其他操作中使用的工具是否发布了新版本。如果是,则创建一个PR更新其他操作以使用较新的版本)

在这里,您需要使用一个具有工作流权限的PAT(个人访问令牌),而不是具有定义范围的GITHUB_TOKEN${{ secrets.GITHUB_TOKEN }}是否是您的PAT?如果是,那么存在问题,因为您无法使用以GITHUB_前缀命名的方式添加secrets。 - GuiFalourd
1
谢谢@GuiFalourd!GITHUB_TOKEN是默认令牌,而不是PAT。但我最终使用了PAT作为解决方法。知道默认令牌无法实现这一点很好。 - Joel
太好了!:D 如果这解决了您的问题,我可以给您的问题添加一个官方答案吗? - GuiFalourd
1
@GuiFalourd,当然可以! - Joel
2个回答

4
你需要在这里使用一个具有工作流权限的个人访问令牌,而不是具有定义范围的GITHUB_TOKEN。实际上,这在2022年9月8日发生了变化。

GitHub Actions: Use the GITHUB_TOKEN with workflow_dispatch and repository_dispatch

Customers will now be able to use the GITHUB_TOKEN with workflow_dispatch and repository_dispatch events to trigger workflows.

Prior to this change, events triggered by GITHUB_TOKEN would not create a new workflow run. This was done to prevent the accidental trigger of endless workflows.

This update makes an exception for workflow_dispatch and repository_dispatch events since they are explicit calls made by the customer and not likely to end up in a loop.

name: Create Workflow Dispatch

on:   workflow_dispatch:

jobs:   build:
    runs-on: ubuntu-latest
    steps:
      - name: Trigger Workflow
        uses: actions/github-script@v6
        with:
          script: |
            github.rest.actions.createWorkflowDispatch({
              owner: context.repo.owner,
              repo: context.repo.repo,
              workflow_id: 'test.yml',
              ref: 'main',
            })

For more details see
Triggering a workflow from a workflow.

所以 GITHUB_TOKEN 可能不起作用。
在OP的问题背景下,这里的主要任务是从另一个工作流中修改工作流文件。
我上面提到的最近的GitHub更改允许使用GITHUB_TOKEN触发工作流程,但它并没有明确说明该令牌是否可以直接用于推送更改到工作流文件。如果能够实现这个功能,将解决原始问题。
您仍然需要修改您的工作流程以提交和推送对工作流文件的更改。您尝试更新工作流文件的操作步骤可能如下所示:
- name: Update the other Action
  run: |
    # Do something to .github/workflows/the-other-action.yaml here
    git config --local user.email "action@github.com"
    git config --local user.name "GitHub Action"
    git add .github/workflows/the-other-action.yaml
    git commit -m "Update the other Action"
    git push

您需要使用具有推送更改所需权限的令牌,并且可以使用更新后的GITHUB_TOKEN测试它:

使用peter-evans/create-pull-request@v3操作,将是:

- name: Create Pull Request
  uses: peter-evans/create-pull-request@v3
  with:
    token: ${{ secrets.GITHUB_TOKEN }}
    branch: some-branch
    commit-message: Updated stuff

我不明白这个回答如何解决楼主的问题。楼主的问题是如何在工作流中修改工作流文件,对吗? - Moritz Schmitz v. Hülst
1
@MoritzSchmitzv.Hülst 你说得对。我已经编辑了答案,以更好地回答楼主的问题。 - VonC
哇,太谢谢了! - Moritz Schmitz v. Hülst

4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接