使用GitHub Actions自动向GitHub仓库发起拉取请求

13

我在GitHub仓库中有一个文件,需要通过运行命令不时进行更新。

作为GitHub Workflows的一部分,我想要一个机器人来运行一个命令,查看是否在repo上创建了差异,如果有,则自动向仓库提交拉取请求。

我怀疑GitHub Workflows可以帮助我做到这一点,因为GitHub现在允许人们运行任意容器(“Actions”)来执行诸如仓库中的构建之类的操作。 我在这里看到了一些官方自动化工作流程,可以让您对问题进行“标记”和“评论”等操作:https://github.com/actions/starter-workflows/tree/master/automation

如果我想运行任意命令并向仓库提交PR,那么我应该查看哪个GitHub Actions,而不是重新发明我的Actions? 任何指针都将不胜感激。

1个回答

16

我创建了一个 GitHub Action,我认为它将有助于您解决这个用例。 https://github.com/peter-evans/create-pull-request

create-pull-request 动作需要与其他修改或添加文件到您的存储库的动作或步骤一起运行。更改将自动提交到新分支并创建拉取请求。

以下是设置大多数主要输入的示例。

on:
  repository_dispatch:
    types: [create-pull-request]
name: Create Pull Request
jobs:
  createPullRequest:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Create report file
        run: date +%s > report.txt
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v5
        with:
          commit-message: Add report file
          committer: Peter Evans <peter-evans@users.noreply.github.com>
          body: |
            New report
            - Contains *today's* date
            - Auto-generated by [create-pull-request][1]

            [1]: https://github.com/peter-evans/create-pull-request
          title: '[Example] Add report file'
          labels: report, automated pr
          assignees: peter-evans
          reviewers: peter-evans
          milestone: 1
          branch: example-patches

为了使其类似于机器人,您可以定期触发工作流程。

on:
 schedule:
   - cron: '*/5 * * * *'

或者,您可以像上面的示例一样通过Webhook触发工作流程。

on:
  repository_dispatch:
    types: [create-pull-request]

要触发工作流,请调用以下内容。其中[username]是GitHub用户名,[token]是一个repo范围的令牌,[repository]是工作流所在的存储库的名称。
curl -XPOST -u "[username]:[token]" -H "Accept: application/vnd.github.everest-preview+json" -H "Content-Type: application/json" https://api.github.com/repos/[username]/[repository]/dispatches --data '{"event_type": "create-pull-request"}'

如需更多示例,请查看此处的文档


链接不可用 @peterevans - may
1
@may 已修复链接。谢谢。 - peterevans

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