本文介绍了使用GitHub Actions自动向GitHub存储库发出拉取请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GitHub存储库中有一个文件,偶尔需要通过运行命令进行更新.

I have a file in a GitHub repository that needs updating occasionally by running a command.

作为 GitHub工作流程的一部分,我希望有一个bot运行命令,并查看它是否在存储库上创建差异,如果是,则自动向存储库发出拉取请求.

As part of a GitHub Workflows, I want to have a bot running a command, and seeing if it creates a diff on the repo, and if so, make a pull request to the repository automatically.

我怀疑 GitHub工作流程可以帮助我做就像GitHub现在允许人们运行任意容器(动作")一样,它们执行诸如在存储库中构建之类的工作.我在此处看到一些正式的自动化工作流程,这些工作流程可让您标记"和评论"问题,例如: https://github.com/actions/starter-workflows/tree/master/automation

I have a suspicion that the GitHub Workflows can help me do that as GitHub now lets people run arbitrary containers ("Actions") that do stuff like builds in a repository. I see some official automation workflows that let you "label" and "comment" issues etc here: https://github.com/actions/starter-workflows/tree/master/automation

如果我想运行任意命令并对存储库进行PR,我应该查看哪些GitHub Action,而不是重新发明自己的Action?任何指针都表示赞赏.

If I wanted to run an arbitrary command and make a PR to the repository, which GitHub Actions should I be looking at instead of reinventing my own Actions? Any pointers are appreciated.

推荐答案

我做了一个GitHub Action,我认为它将帮助您解决此用例. https://github.com/peter-evans/create-pull-request

I made a GitHub Action that I think will help you with this use case.https://github.com/peter-evans/create-pull-request

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

create-pull-request action needs to be run in conjunction with other actions or steps that modify or add files to your repository. The changes will be automatically committed to a new branch and a pull request created.

下面是设置大多数主要输入的示例.

Here is an example that sets most of the main inputs.

on:
  repository_dispatch:
    types: [create-pull-request]
name: Create Pull Request
jobs:
  createPullRequest:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Create report file
        run: date +%s > report.txt
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v3
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          commit-message: Add report file
          committer: Peter Evans <[email protected]>
          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

要使其像机器人一样,您可以定期触发工作流程.

To make it bot-like you can trigger the workflow periodically.

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

或者,您可以将工作流设置为通过webhook触发,如上例所示.

Alternatively, you can set the workflow to trigger via webhook, as in the example above.

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

要触发工作流程,请调用以下命令. [username]是GitHub用户名. [token]repo范围内的令牌. [repository]是工作流所在的存储库的名称.

To trigger the workflow call the following. [username] is a GitHub username. [token] is a repo scoped token. [repository] is the name of the repository the workflow resides in.

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"}'

有关更多示例,请在此处查看文档.

For further examples check out the documentation here.

这篇关于使用GitHub Actions自动向GitHub存储库发出拉取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 11:45