Create pull request with github action

Viewed 10391

I'm trying to make it work this action, but I'm confused also whats it's missing in between, before triggering the peter-evans PR.

The scenario is pretty simple, I like on push, on any feature/* branch, to create automatically PR, but instead I'm getting weird scenario, where develop changes are applied on top of the feature/* branch. Can someone give me hints on this?

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

Just posting this as an alternative solution. If you don't want to use any 3rd party actions you can achieve this with actions/github-script, it will just require a bit more coding.

As this stands, the action will error if there is already an open PR for the feature branch. If this is an issue you could check of an existing PR with the github.rest.pulls.list method, filtering by both head and base so it will only return one or no PRs.

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']
            });

Reading through the readme, the action by Peter Evans doesn't fit what you're trying to achieve. But you can use 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"

I know this question is a year old now and asking about the create-pull-request action, but for those that would rather not use third-party actions, Github actions now support Github command line natively, if you use Github hosted runners. See: Using Github CLI in Workflows

This makes it super easy to create a pull request using the gh pr create command

Something like this:

  steps
    - name: create pull request
      run: gh pr create -B base_branch -H branch_to_merge --title 'Merge branch_to_merge into base_branch' --body 'Created by Github action'
      env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

You might need to specify the base branch there:

- 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

I have this and it creates the PR when it does not exist. I remember it didn't work exactly right at the beginning until I specified myself base and branch values, which are not very clear in the docs.

Related