J Fabian Meier
J Fabian Meier

Reputation: 35805

github action to create a branch and pull request

What would be a good way to do the following in github actions:

  1. Create a new branch.
  2. Run some shell command on the branch (like some Maven command to change the POM).
  3. Create a pull request.

Should I use raw git commands? Or the github cli? Or other actions?

What would you suggest?

Upvotes: 2

Views: 7094

Answers (2)

Scott Robey
Scott Robey

Reputation: 1701

I was looking for a solution that didn't require using actions from a third-party and figured out that 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 }}

Upvotes: 12

vsr
vsr

Reputation: 1127

I've used Create Pull Request GHA (https://github.com/peter-evans/create-pull-request) and it is quite easy to use. Within the action itself you can even specify the branch name. In the next step, you can also run shell commands. Example as below:

  - name: Create Pull Request
    id: cpr
    uses: peter-evans/create-pull-request@v3
  - name: Check outputs
    run: |
      echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
      echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"

Upvotes: 0

Related Questions