如何使用文件和拉取请求自动创建新分支



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

IFTTT涵盖了第一部分,每次上传新文件时,都会在github上创建一个新问题,其中包含正文中文件的链接。

然而,我不知道如何创建操作,该操作现在将下载文件,创建一个新分支,提交并将文件推送到该分支,最后设置一个请求以供我批准。

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

谢谢!


第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

它指的是哪种代币?这是指你提到的私人行为吗?不过,存储库是公开的。

再次感谢您的帮助!

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

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

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 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

编辑

要在同一存储库中本地使用此操作文件(如果您不希望它是公共的(,您需要创建一个.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>

我用这个操作创建了一个类似的工作流示例(调用本地操作(,yml文件


请注意,Github Marketplace上也有许多操作来执行git commit push操作。

最新更新