【问题标题】:Making pull requests to a GitHub repository automatically with GitHub Actions使用 GitHub Actions 自动向 GitHub 存储库发出拉取请求
【发布时间】:2019-12-21 12:50:56
【问题描述】:

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

作为GitHub Workflows 的一部分,我想让一个机器人运行一个命令,并查看它是否在存储库上创建了差异,如果是,则自动向存储库发出拉取请求。

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

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

【问题讨论】:

    标签: github github-actions


    【解决方案1】:

    我创建了一个 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@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 <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"}'
    

    更多示例请查看documentation here

    【讨论】:

      猜你喜欢
      • 2022-10-24
      • 2021-09-25
      • 1970-01-01
      • 2012-02-17
      • 2020-07-19
      • 2020-12-10
      • 1970-01-01
      • 2020-08-10
      • 2018-11-08
      相关资源
      最近更新 更多