git - 如何使用文件然后 pull 请求自动创建新分支?

标签 git github-actions blogs ifttt

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

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

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

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

谢谢!


编辑1:

我不太确定如何完成此操作(包括在我编写 的位置生成一个随机数。这是迄今为止我所得到的:

name: Pull request on issue

on:
  issues:
  
jobs:
  create:
    runs-on: ubuntu-latest
    steps:
      - name: create branch
        uses: peterjgrainger/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="68090b1c010706450b1a0d091c0d450a1a09060b00281e5a46584659" rel="noreferrer noopener nofollow">[email protected]</a>
        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/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7d4c5d2d6c3d29ac7c2dbdb9ac5d2c6c2d2c4c3f7c1849986879986" rel="noreferrer noopener nofollow">[email protected]</a>
        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/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d3e35383e363228291d2b6f736e7369" rel="noreferrer noopener nofollow">[email protected]</a>
      - name: Generate random number
        id: random
        run: echo "::set-output name=value::$(echo $RANDOM)"
      - name: create branch
        uses: peterjgrainger/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9cfdffe8f5f3f2b1ffeef9fde8f9b1feeefdf2fff4dceaaeb2acb2ad" rel="noreferrer noopener nofollow">[email protected]</a>
        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/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d5b6a7b0b4a1b0f8a5a0b9b9f8a7b0a4a0b0a6a195a3e6fbe4e5fbe4" rel="noreferrer noopener nofollow">[email protected]</a>
        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/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c2a3a1b6abadacefa1b0a7a3b6a7efa0b0a3aca1aa82b4f0ecf2ecf3" rel="noreferrer noopener nofollow">[email protected]</a>
Error: No token defined in the environment variables

它指的是哪个 token ?这是指你所说的私有(private)诉讼吗?不过,该存储库是公开的。

再次感谢您的帮助!

最佳答案

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

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

name: Pull request on issue

on:
  issues:
  
jobs:
  create:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="66050e03050d09131226105448554852" rel="noreferrer noopener nofollow">[email protected]</a>
      - 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/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2b3b1a6bbbdbcffb1a0b7b3a6b7ffb0a0b3bcb1ba92a4e0fce2fce3" rel="noreferrer noopener nofollow">[email protected]</a>
        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/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="432031262237266e33362f2f6e312632362630370335706d72736d72" rel="noreferrer noopener nofollow">[email protected]</a>
        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/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ae9e2efe9e1e5fffecafcb8a4b9a4be" rel="noreferrer noopener nofollow">[email protected]</a>
      - 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/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e2f2d3a272120632d3c2b2f3a2b632c3c2f202d260e387c607e607f" rel="noreferrer noopener nofollow">[email protected]</a>
        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/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ae9f8efebfeefa7faffe6e6a7f8effbffeff9fecafcb9a4bbbaa4bb" rel="noreferrer noopener nofollow">[email protected]</a>
        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如果您想将某些值动态化(例如,您不能直接在操作中使用 secret ),请添加到操作。

编辑

要在同一存储库中本地使用此操作文件(如果您不希望它公开),您将需要创建一个 .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/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f5c575a5c54504a4b7f490d110c110b" rel="noreferrer noopener nofollow">[email protected]</a> # Necessary to access local action
      - name: Local Action Call
        uses: ./.github/actions/<action-name>
        with:
          secret: ${{ secrets.GH_TOKEN }}

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

我创建了一个具有类似内容的工作流程示例(调用本地操作)herethis action,yml file


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

关于git - 如何使用文件然后 pull 请求自动创建新分支?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70036757/

相关文章:

git - 如何使用 Android Studio 将 Android 项目推送到 github 中现有的私有(private)空存储库?

python - Django==2.1.4 - 博客

git - 将 Git 与 Visual Source Safe 6.0 一起使用

git - 为什么在这种情况下我在 Git 中的本地更改会被 checkout 覆盖?

github - 如果 github 操作失败则阻止合并

github - GitHub Actions 中的嵌套模板(从另一个 yaml 文件调用 yaml 文件)

javascript - 建立博客 : what's standard?

java - 如何在我的 Web 应用程序中创建博客文章?我应该使用什么数据库?

git - .gitignore 中多个文件扩展名的简写

github - 跨作业重用 github 操作的一部分