如何使用文件自动创建新分支,然后使用拉取请求?

How can I automate creating a new branch using a file and then a pull request?

我有一个静态博客设置,每次我想要一个新的 post 我都必须提交并推送一个 .md 文件。这是一个组的一部分,所以我想知道是否有一种方法可以在每次将新的 .md 文件保存到 google 驱动器文件夹时自动执行提交和推送部分。

第一部分由 IFTTT 介绍,每次上传新文件时,都会在 github 上创建一个新问题,其中包含正文中文件的 link。

但是,我不知道如何创建现在将下载文件、创建新分支、提交文件并将其推送到该分支并最终设置拉取请求以供我批准的操作。

如果您知道任何其他自动化此过程的方法,我愿意接受建议!

谢谢!


编辑 1:

我不太确定如何完成此操作(包括在我编写 的地方生成一个随机数。这是我目前所拥有的:

name: Pull request on issue

on:
  issues:
  
jobs:
  create:
    runs-on: ubuntu-latest
    steps:
      - name: create branch
        uses: peterjgrainger/action-create-branch@v2.0.1
        with:
          # The branch to create
          branch: post-<random-number>
      - name: download file
        run: wget ${{ github.event.issue.body }} -O source/_posts/
      - name: commit and push new file
        run: |
          git config user.name "GitHub Actions Bot"
          git config user.email "<>"
          git add .
          git commit -m "New post"
          git push -u origin post-<random-number>
      - name: create pull request towards the main branch
        uses: peter-evans/create-pull-request@v3.10.1
        with:
          token: ${{ secrets.GH_TOKEN }}
          commit-message: Auto Pull Request
          title: New post pr
          body: Auto-created Pull Request
          branch: post-<random-number> # The branch where you commit
          base: master # Don't forget to specify the right base branch here

编辑2:

name: Pull request on issue

on:
  issues:
    inputs:
      secret:
        required: true
        description: "Github PAT"
  
jobs:
  create:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2.3.4
      - name: Generate random number
        id: random
        run: echo "::set-output name=value::$(echo $RANDOM)"
      - name: create branch
        uses: peterjgrainger/action-create-branch@v2.0.1
        with:
          # The branch to create
          branch: post-${{ steps.random.outputs.value }}
      - name: download file
        run: wget ${{ github.event.issue.body }} -O source/_posts/
      - name: commit and push new file
        run: |
          git config user.name "GitHub Actions Bot"
          git config user.email "<>"
          git add .
          git commit -m "New post"
          git push -u origin post-${{ steps.random.outputs.value }}
      - name: create pull request towards the main branch
        uses: peter-evans/create-pull-request@v3.10.1
        with:
          token: ${{ secrets.GH_TOKEN }}
          commit-message: Auto Pull Request
          title: New post pr
          body: Auto-created Pull Request
          branch: post-${{ steps.random.outputs.value }} # The branch where you commit
          base: master # Don't forget to specify the right base branch here

这就是我目前拥有的,感谢@GuiFalourd。

不幸的是,当我 运行 这个时,我得到以下错误:

Run peterjgrainger/action-create-branch@v2.0.1
Error: No token defined in the environment variables

它指的是哪个令牌?这是指你提到的私人行动吗?不过,存储库是 public。

再次感谢您的帮助!

更新您的工作流程以添加 随机数 可以使用输出变量来实现。我还认为您需要将 actions/checkout 操作添加到您的存储库才能访问下载的文件。

更新后的工作流程文件如下所示

name: Pull request on issue

on:
  issues:
  
jobs:
  create:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2.3.4
      - name: Generate random number
        id: random
        run: echo "::set-output name=value::$(echo $RANDOM)"
      - name: Example how to use the output
        run: echo "${{ steps.random.outputs.value }}"
      - name: create branch
        uses: peterjgrainger/action-create-branch@v2.0.1
        with:
          # The branch to create
          branch: post-${{ steps.random.outputs.value }}
      - name: download file
        run: wget ${{ github.event.issue.body }} -O source/_posts/
      - name: commit and push new file
        run: |
          git config user.name "GitHub Actions Bot"
          git config user.email "<>"
          git add .
          git commit -m "New post"
          git push -u origin post-${{ steps.random.outputs.value }}
      - name: create pull request towards the main branch
        uses: peter-evans/create-pull-request@v3.10.1
        with:
          token: ${{ secrets.GH_TOKEN }}
          commit-message: Auto Pull Request
          title: New post pr
          body: Auto-created Pull Request
          branch: post-${{ steps.random.outputs.value }} # The branch where you commit
          base: master # Don't forget to specify the right base branch here

您还可以创建一个 composite 操作来重现您正在使用的所有步骤,例如使用如下内容:

操作 action.yml 看起来像这样。

name: 'Action Name'
description: 'Runs a composite step action'
inputs:
  secret:
    required: true
    description: "Github PAT"

runs:
  using: "composite"
  steps:
      - name: Checkout
        uses: actions/checkout@v2.3.4
      - name: Generate random number
        id: random
        run: echo "::set-output name=value::$(echo $RANDOM)"
        shell: bash
      - name: Example how to use the output
        run: echo "${{ steps.random.outputs.value }}"
        shell: bash
      - name: create branch
        uses: peterjgrainger/action-create-branch@v2.0.1
        with:
          # The branch to create
          branch: post-${{ steps.random.outputs.value }}
      - name: download file
        run: wget ${{ github.event.issue.body }} -O source/_posts/
      - name: commit and push new file
        run: |
          git config user.name "GitHub Actions Bot"
          git config user.email "<>"
          git add .
          git commit -m "New post"
          git push -u origin post-${{ steps.random.outputs.value }}
        shell: bash
      - name: create pull request towards the main branch
        uses: peter-evans/create-pull-request@v3.10.1
        with:
          token: ${{ inputs.secret }}
          commit-message: Auto Pull Request
          title: New post pr
          body: Auto-created Pull Request
          branch: post-${{ steps.random.outputs.value }}
          base: master

如果您想将某些值变为动态值,请向操作添加一些其他 inputs(例如,您不能直接在操作中使用秘密)。

编辑

要在同一存储库中本地使用此操作文件(如果您不希望它是 public),您需要创建一个 .github/actions/<action-name> 文件夹,并添加此 action.yml 那里的文件。

然后,要在您的工作流作业中使用它,您需要将 workflow.yml 更新为此实现:

name: Pull request on issue

on:
  issues:
  
jobs:
  create:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2.3.4 # Necessary to access local action
      - name: Local Action Call
        uses: ./.github/actions/<action-name>
        with:
          secret: ${{ secrets.GH_TOKEN }}

正在为您选择的文件夹名称更新 <action-name>

我创建了一个类似的工作流示例(调用本地操作)here with this action,yml file


注意在Github Marketplace上也有很多动作执行git commit push操作