Skip to content

technote-space/create-pr-action

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Repository files navigation

Create PR Action

CI Status codecov CodeFactor License: MIT

Read this in other languages: English, 日本語.

This is a GitHub Actions that executes an arbitrary command and commits the changes to the new pull request.
It also has a management function that resolves conflicts and closes pull requests that are no longer needed.

Table of Contents

Details

Installation

e.g. Update npm packages

e.g. .github/workflows/update-npm-packages.yml

on:
 schedule:
   - cron: 0 0 * * *
 pull_request:
   types: [opened, synchronize, reopened, closed]

name: Update packages
jobs:
 release:
   name: Update npm packages
   runs-on: ubuntu-latest
   steps:
     - name: Update npm packages
       uses: technote-space/create-pr-action@v2
       with:
         EXECUTE_COMMANDS: |
           npx npm-check-updates -u --packageFile package.json
           yarn install
           yarn upgrade
           yarn audit
         COMMIT_MESSAGE: 'chore: update npm dependencies'
         COMMIT_NAME: 'GitHub Actions'
         COMMIT_EMAIL: 'example@example.com'
         PR_BRANCH_NAME: 'chore-npm-update-${PR_ID}'
         PR_TITLE: 'chore: update npm dependencies'

e.g. Update composer packages

e.g. .github/workflows/update-composer-packages.yml

on:
 schedule:
   - cron: 0 0 * * *
 pull_request:
   types: [opened, synchronize, reopened, closed]

name: Update packages
jobs:
 release:
   name: Update composer packages
   runs-on: ubuntu-latest
   steps:
     - name: Update composer packages
       uses: technote-space/create-pr-action@v2
       with:
         EXECUTE_COMMANDS: |
           rm -f "composer.lock"
           < "composer.json" jq -r '.require | to_entries[] | select(.value | startswith("^")) | select(.key | contains("/")) | .key' | tr '\n' ' ' | xargs -r php -d memory_limit=2G "$(command -v composer)" require --no-interaction --prefer-dist --no-suggest
           < "composer.json" jq -r '."require-dev" | to_entries[] | select(.value | startswith("^")) | select(.key | contains("/")) | .key' | tr '\n' ' ' | xargs -r php -d memory_limit=2G "$(command -v composer)" require --dev --no-interaction --prefer-dist --no-suggest
         COMMIT_MESSAGE: 'chore: update composer dependencies'
         COMMIT_NAME: 'GitHub Actions'
         COMMIT_EMAIL: 'example@example.com'
         PR_BRANCH_NAME: 'chore-composer-update-${PR_ID}'
         PR_TITLE: 'chore: update composer dependencies'

e.g. Mixed

e.g. .github/workflows/update-packages.yml

on:
 schedule:
   - cron: 0 0 * * *
 pull_request:
   types: [opened, synchronize, reopened, closed]

name: Update packages
jobs:
 release:
   name: Update packages
   runs-on: ubuntu-latest
   steps:
     - name: Update packages
       uses: technote-space/create-pr-action@v2
       with:
         EXECUTE_COMMANDS: |
           npx npm-check-updates -u --packageFile package.json
           yarn install
           yarn upgrade
           yarn audit
           rm -f "composer.lock"
           < "composer.json" jq -r '.require | to_entries[] | select(.value | startswith("^")) | select(.key | contains("/")) | .key' | tr '\n' ' ' | xargs -r php -d memory_limit=2G "$(command -v composer)" require --no-interaction --prefer-dist --no-suggest
           < "composer.json" jq -r '."require-dev" | to_entries[] | select(.value | startswith("^")) | select(.key | contains("/")) | .key' | tr '\n' ' ' | xargs -r php -d memory_limit=2G "$(command -v composer)" require --dev --no-interaction --prefer-dist --no-suggest
         COMMIT_MESSAGE: 'chore: update dependencies'
         COMMIT_NAME: 'GitHub Actions'
         COMMIT_EMAIL: 'example@example.com'
         PR_BRANCH_NAME: 'chore-update-${PR_ID}'
         PR_TITLE: 'chore: update dependencies'

More details of target event

Screenshots

Run commands

run command

Created Pull Request

pull request

Options

name description default required e.g.
GLOBAL_INSTALL_PACKAGES Packages to be global installed imagemin-cli
EXECUTE_COMMANDS Commands to be executed
COMMIT_MESSAGE Commit message
COMMIT_NAME Git commit name ${github.actor}
COMMIT_EMAIL Git commit email ${github.actor}@users.noreply.github.com
PR_BRANCH_PREFIX PullRequest branch prefix create-pr-action/ true imagemin/
PR_BRANCH_NAME PullRequest branch name
Several variables are available (variables1)
true imagemin-${PR_ID}
PR_TITLE PullRequest title
Several variables are available (variables1)
true chore: minify images
PR_BODY PullRequest body
Several variables are available (variables2)
true
CHECK_DEFAULT_BRANCH Whether to check default branch true false
ONLY_DEFAULT_BRANCH Whether not to check other than default branch pull_request: false
else: true
true
GITHUB_TOKEN アクセストークン ${{github.token}} true ${{secrets.ACCESS_TOKEN}}

Action event details

Target event

eventName action
pull_request opened, synchronize, reopened, labeled, unlabeled
pull_request closed
schedule, repository_dispatch, workflow_dispatch *
  • The following activity types must be explicitly specified (detail)
    • labeled, unlabeled, closed

Variables

Variables1

name description
PR_NUMBER pull_request.number (e.g. 11)
PR_NUMBER_REF #${pull_request.number} (e.g. #11)
PR_ID pull_request.id (e.g. 21031067)
PR_HEAD_REF pull_request.head.ref (e.g. change)
PR_BASE_REF pull_request.base.ref (e.g. main)
PR_TITLE pull_request.title (e.g. Update the README with new information.)
PATCH_VERSION new patch version (e.g. v1.2.4)
MINOR_VERSION new minor version (e.g. v1.3.0)
MAJOR_VERSION new major version (e.g. v2.0.0)

Variables2

name description
PR_LINK Link to PR
COMMANDS_OUTPUT Results of command
FILES_SUMMARY e.g. Changed 2 files
FILES Changed file list

Addition

GITHUB_TOKEN

The GITHUB_TOKEN that is provided as a part of GitHub Actions doesn't have authorization to create any successive events.
So it won't spawn actions which triggered by push.
This can be a problem if you have branch protection configured.

If you want to trigger actions, use a personal access token instead.

  1. Generate a personal access token with the public_repo or repo scope.
    (repo is required for private repositories).
  2. Save as ACCESS_TOKEN
  3. Add input to use ACCESS_TOKEN instead of GITHUB_TOKEN.
    e.g. .github/workflows/update-packages.yml
    on:
      schedule:
        - cron: 0 0 * * *
      pull_request:
        types: [opened, synchronize, reopened, closed]
    
    name: Update packages
    jobs:
      release:
        name: Update npm packages
        runs-on: ubuntu-latest
        steps:
          - name: Update npm packages
            uses: technote-space/create-pr-action@v2
            with:
              GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
              EXECUTE_COMMANDS: |
                npx npm-check-updates -u --packageFile package.json
                yarn install
                yarn upgrade
                yarn audit
              COMMIT_MESSAGE: 'chore: update npm dependencies'
              COMMIT_NAME: 'GitHub Actions'
              COMMIT_EMAIL: 'example@example.com'
              PR_BRANCH_NAME: 'chore-npm-update-${PR_ID}'
              PR_TITLE: 'chore: update npm dependencies'

Author

GitHub (Technote)
Blog