【问题标题】:Create pull request with github action使用 github 操作创建拉取请求
【发布时间】:2022-02-23 00:33:24
【问题描述】:

我正试图让它发挥这个作用,但在触发 peter-evans PR 之前,我也很困惑它之间缺少什么。

该场景非常简单,我喜欢在任何 feature/* 分支上进行推送,以自动创建 PR,但相反,我遇到了奇怪的场景,其中开发更改应用在 feature/* 分支之上。有人可以给我一些提示吗?

name: Pull Request Action
on:
  push:
    branches:
      - feature/*

jobs:
  create-pull-request:
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository code
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
          ref: develop
      
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v3.10.0
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          commit-message: Simple demo
          title: '[Example] Simple demo'
          body: >
            This PR is auto-generated by 
            [create-pull-request](https://github.com/peter-evans/create-pull-request).
          labels: feature, automated pr
          branch: feature/workflow-demo

【问题讨论】:

    标签: github github-actions pull-request


    【解决方案1】:

    只是将其发布为替代解决方案。如果您不想使用任何第 3 方操作,您可以使用 actions/github-script 实现此目的,它只需要更多编码。

    就目前而言,如果功能分支已经有一个打开的 PR,则该操作将出错。如果这是一个问题,您可以使用 github.rest.pulls.list 方法检查现有 PR,同时通过 head 和 base 过滤,因此它只会返回一个或不返回一个 PR。

    name: Pull Request Action
    on:
      push:
        branches:
          - feature/*
          
    jobs:
      create-pull-request:
        runs-on: ubuntu-latest
        steps:
          - name: Create Pull Request
            uses: actions/github-script@v6
            with:
              script: |
                const { repo, owner } = context.repo;
                const result = await github.rest.pulls.create({
                  title: '[Example] Simple demo',
                  owner,
                  repo,
                  head: '${{ github.ref_name }}',
                  base: 'develop',
                  body: [
                    'This PR is auto-generated by',
                    '[actions/github-script](https://github.com/actions/github-script).'
                  ].join('\n')
                });
                github.rest.issues.addLabels({
                  owner,
                  repo,
                  issue_number: result.data.number,
                  labels: ['feature', 'automated pr']
                });
    

    【讨论】:

      【解决方案2】:

      通读自述文件,Peter Evans 的操作不符合您的目标。但是你可以使用repo-sync's pull-request action:

      name: Pull Request Action
      on:
        push:
          branches:
            - feature/*
      
      jobs:
        create-pull-request:
          runs-on: ubuntu-latest
          steps:
            - name: Check out repository code
              uses: actions/checkout@v2
            - name: pull-request
              uses: repo-sync/pull-request@v2
              with:
                destination_branch: "develop"
                github_token: ${{ secrets.GITHUB_TOKEN }}
                pr_label: "feature, automated pr"
                pr_title: "[Example] Simple demo"
      

      【讨论】:

        【解决方案3】:

        您可能需要在此处指定基础分支:

        - name: Create Pull Request
            uses: peter-evans/create-pull-request@v3.10.1
            with:
              token: ${{ secrets.GH_TOKEN }}
              commit-message: Auto Pull Request
              title: Your desired title
              body: Auto-created Pull Request
              branch: ${{ github.ref }} # The branch where you commit
              base: develop # Don't forget to specify the right base branch here
        

        我有这个,它会在 PR 不存在时创建它。我记得一开始它并不能完全正确地工作,直到我指定了自己的 base 和 branch 值,这在文档中不是很清楚。

        【讨论】:

          猜你喜欢
          • 2022-08-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-25
          • 1970-01-01
          • 2020-10-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多