GitHub Actions: How to Create Pull Requests Automatically

Friday, October 11, 2019

Introducing one of the first GitHub Actions I wrote and published to the GitHub Marketplace. A generic action to automatically create a pull request for changes to your repository in the Actions workspace.

create-pull-request

Changes to a repository in the Actions workspace persist between steps in a workflow. The create-pull-request action is designed to be used in conjunction with other steps that modify or add files to your repository. The local changes will be automatically committed to a new branch and a pull request created.

Create Pull Request action will:

  1. Check for repository changes in the Actions workspace. This includes:
    • untracked (new) files
    • tracked (modified) files
    • commits made during the workflow that have not been pushed
  2. Commit all changes to a new branch, or update an existing pull request branch.
  3. Create a pull request to merge the new branch into the base—the branch checked out in the workflow.

There are many interesting use cases for this type of action, such as…

  • Process management bots
  • Synchronization with external data sources
  • Updating contract definitions (API, Swagger docs, etc.)
  • Code linting/formatting
  • Static analysis/fixes
  • Scheduled process management

Basic example workflow

Here is simple example that adds a dated report to a repository and raises a pull request.

on:
  schedule:
    - cron: '0 10 * * *'
name: create-pull-request workflow
jobs:
  createPullRequest:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Create report file
        run: date +%s > report.txt
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v5
        with:
          commit-message: Add report file
          title: '[Example] Add report file'
          body: >
            This PR is auto-generated by 
            [create-pull-request](https://github.com/peter-evans/create-pull-request).
          labels: report, automated pr

This is an example of what pull requests created with the action look like on GitHub.

Pull Request Example

Example workflow to automate periodic dependency updates

This pattern will work well for updating any kind of static content from an external source. The following example workflow executes once a week and will create a pull request for any new npm dependency updates. It works best in combination with a build workflow triggered on push and pull_request. A Personal Access Token (PAT) can be used in order for the creation of the pull request to trigger further workflows. See the documentation here for further details.

name: Update Dependencies
on:
  schedule:
    - cron:  '0 10 * * 1'
jobs:
  update-dep:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: '16'
      - name: Update dependencies
        run: |
          npx -p npm-check-updates ncu -u
          npm install
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v5
        with:
            token: ${{ secrets.PAT }}
            commit-message: Update dependencies
            title: Update dependencies
            body: |
              - Dependency updates
  
              Auto-generated by [create-pull-request][1]
  
              [1]: https://github.com/peter-evans/create-pull-request
            branch: update-dependencies

The above workflow works best in combination with a build workflow triggered on push and pull_request.

name: CI
on:
  push:
    branches: [master]
  pull_request:
    branches: [master]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 16
      - run: npm ci
      - run: npm run test
      - run: npm run build

Further examples and documentation

For further examples of how create-pull-request action can be used, please see the list of examples in the documentation.

See the create-pull-request action repository for the latest version and full usage details.