【问题标题】:GitHub action to run command and add commit if I type comment如果我输入注释,运行命令并添加提交的 GitHub 操作
【发布时间】:2020-12-13 06:01:36
【问题描述】:

我的目标是在 GitHub 中输入 /run-black 作为对拉取请求的评论,然后 GitHubActions 将在拉取请求的分支上运行 black . 并添加提交。

用例是有时偶然的贡献者向我的库提出一个小的拉取请求(例如修复错字),我希望能够只写一个像 /run-black 这样的评论来拥有 black 格式化程序在我合并之前运行他们的文件。

【问题讨论】:

    标签: github-actions


    【解决方案1】:

    使用操作Slash Command Dispatch。将名称为 PATrepo 范围 PAT 添加到您的机密中,并使用以下定义创建两个工作流。

    name: Slash Command Dispatch
    on:
      issue_comment:
        types: [created]
    jobs:
      slashCommandDispatch:
        runs-on: ubuntu-latest
        steps:
          - name: Slash Command Dispatch
            uses: peter-evans/slash-command-dispatch@v2
            with:
              token: ${{ secrets.PAT }}
              issue-type: pull-request
              commands: |
                run-black
    
    on:
      repository_dispatch:
        types: [run-black-command]
    jobs:
      runBlack:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v2
            with:
              repository: ${{github.event.client_payload.pull_request.head.repo.full_name}}
              ref: ${{github.event.client_payload.pull_request.head.ref}}
              token: ${{ secrets.PAT }}
          - name: Slash Command Dispatch
            run: black .
          - run: |
              git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
              git config --local user.name "github-actions[bot]"
              git commit -m "Run black" -a
              git push
    

    【讨论】:

    • 谢谢!我试过这个,但我得到了错误“1个文件已更改,1个插入(+),1个删除(-)致命:您当前不在分支上。要将历史记录推送到当前(分离的HEAD)状态,请使用git push origin HEAD:" 关于如何推送到拉取请求分支的任何建议?
    • @ignoring_gravity 尝试我的编辑,但它仅在从中提取的分支位于您的存储库中或 PR 允许您推送到他们的分支时才有效
    • 有没有办法使用应用令牌而不是 PAT 来执行此操作?
    猜你喜欢
    • 2022-12-29
    • 2022-06-14
    • 1970-01-01
    • 2022-01-12
    • 2021-06-08
    • 2011-12-31
    • 2020-11-20
    • 2020-10-05
    • 2013-01-18
    相关资源
    最近更新 更多