Automatically builds & merges a PR and deletes PR branches if they satisfies configurable approval criteria.
- Configure this workflow to trigger PR build if the PR satisfies criteria:
- Mandatory: Approval count is greater than equal to DEFAULT_APPROVAL_COUNT_ENV variable
- Mandatory: There is no review with changes_requested status
- Optional: PR is tagged with a certain label PR_LABEL_ENV
- Optional: List of codeowners (same as in CODEOWNER file in .github) CODE_OWNERS_ENV, build will only be triggered if there are no pending CODEOWNERS review on the PR
- The workflow gets triggered in the following scenarios:
- When PR review is submitted 1. When PR is labeled 1. Note: build will only be triggered if PR satisfies the above criteria and is independent of the event that triggered the workflow in the first place
- Currently, build through Github Actions is not supported
- Build can be triggered if using GitHub pull request builder plugin
- The action can trigger build using trigger phrase
- Configure the trigger phrase using BUILD_COMMENT_ENV
- You need to provide GIT token and user name to action so that it has access to the GIT APIs
- Create token with read & comment permission on issues, follow here
- Add token as secret using this guide
- In the sample worflow the secret name is bot_token
- Trigger PR build sample workflow:
name: PR Auto-build
on:
pull_request:
types: [labeled]
pull_request_review:
types: [submitted]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Trigger pr-autobuild action for building PR
uses: nikhilaii93/pr-autobuild@master
env:
GITHUB_TOKEN: ${{ secrets.bot_token }}
GITHUB_NAME: nikhilaii93
DEFAULT_APPROVAL_COUNT_ENV: 1
BUILD_COMMENT_ENV: "OK to test"
PR_LABEL_ENV: RELEASE_TEST
CODEOWNERS: abc@example.com,defteam@example.com
- PR is merged when it's mergeable according to branch protection rules
- The following are still verified (in case branch protection rules are not set)
- Mandatory: Approval count is greater than equal to DEFAULT_APPROVAL_COUNT_ENV variable
- Mandatory: There is no review iwth changes_requested status
- Optional: PR is tagged with a certain label PR_LABEL_ENV
- Optional: List of codeowners (same as in CODEOWNER file in .github) CODE_OWNERS_ENV, build will only be triggered if there are no pending CODEOWNERS review on the PR
- Optional: Build successful event(eg. through Jenkins)
- This workflow gets triggered through a external event which is a POST call:
curl -s -X POST -u nikhilaii93:$TOKEN -H "Content-Type: application/json" -H "Accept: application/vnd.github.everest-preview+json" "https://api.github.com/repos/$GITHUB_REOSITORY/dispatches" -d '{"event_type": "pr-build-success $PR_NUM"}'
- Replace $TOKEN with actual GITHUB access token
- Replace $GITHUB_REPOSITORY with owner/repo
- Replace $PR_NUM with actual PR number
- The above event can be triggered through Jenkins on build success
- PR Name in GitHub pull request builder plugin can be obtained through ghprbPullId environment variable
- The PR is merge method used is based on the following logic:
- If base or head branch is named release then default merge is used always
- If DEFAULT_MERGE_ENV is set to merge or not set at all then 'default merge' is used for other PRs as well
- If DEFAULT_MERGE_ENV is set to squash then squash merge is used (if base or head is not named release)
- This flow can also delete the branch after merge
- It deletes if delete is enabled using DELETE_BRANCH_ENV has been set to true
- It only delete when the associated PR has been merged
- Trigger PR Merge sample workflow:
name: PR Auto-merge
on: repository_dispatch
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Trigger pr-autobuild action
uses: nikhilaii93/pr-autobuild@master
env:
GITHUB_TOKEN: ${{ secrets.bot_token }}
GITHUB_NAME: nikhilaii93
DEFAULT_APPROVAL_COUNT_ENV: 1
BUILD_COMMENT_ENV: "OK to test"
PR_LABEL_ENV: RELEASE
DEFAULT_MERGE: squash
DELETE_BRANCH_ENV: true
ISC © 2019 Nikhil Verma nikhilverma.ajm@gmail.com