/pr-autobuild

Automatically builds a PR if it's satisfies approval criteria

Primary LanguageShell

pr-autobuild

Automatically builds & merges a PR and deletes PR branches if they satisfies configurable approval criteria.

GitHub Actions status

made-with-bash

Worflows supported

PR Build Trigger

  1. Configure this workflow to trigger PR build if the PR satisfies criteria:
    1. Mandatory: Approval count is greater than equal to DEFAULT_APPROVAL_COUNT_ENV variable
    2. Mandatory: There is no review with changes_requested status
    3. Optional: PR is tagged with a certain label PR_LABEL_ENV
  2. 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
  3. The workflow gets triggered in the following scenarios:
  4. 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
  5. Currently, build through Github Actions is not supported
  6. Build can be triggered if using GitHub pull request builder plugin
    1. The action can trigger build using trigger phrase
    2. Configure the trigger phrase using BUILD_COMMENT_ENV
  7. You need to provide GIT token and user name to action so that it has access to the GIT APIs
    1. Create token with read & comment permission on issues, follow here
    2. Add token as secret using this guide
    3. In the sample worflow the secret name is bot_token
  8. 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 Merge & Delete branch

  1. PR is merged when it's mergeable according to branch protection rules
  2. The following are still verified (in case branch protection rules are not set)
    1. Mandatory: Approval count is greater than equal to DEFAULT_APPROVAL_COUNT_ENV variable
    2. Mandatory: There is no review iwth changes_requested status
    3. Optional: PR is tagged with a certain label PR_LABEL_ENV
  3. 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
  4. Optional: Build successful event(eg. through Jenkins)
  5. 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"}'
  6. Replace $TOKEN with actual GITHUB access token
  7. Replace $GITHUB_REPOSITORY with owner/repo
  8. Replace $PR_NUM with actual PR number
  9. The above event can be triggered through Jenkins on build success
  10. PR Name in GitHub pull request builder plugin can be obtained through ghprbPullId environment variable
  11. The PR is merge method used is based on the following logic:
    1. If base or head branch is named release then default merge is used always
    2. If DEFAULT_MERGE_ENV is set to merge or not set at all then 'default merge' is used for other PRs as well
    3. If DEFAULT_MERGE_ENV is set to squash then squash merge is used (if base or head is not named release)
  12. This flow can also delete the branch after merge
    1. It deletes if delete is enabled using DELETE_BRANCH_ENV has been set to true
    2. It only delete when the associated PR has been merged
  13. 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

License

ISC © 2019 Nikhil Verma nikhilverma.ajm@gmail.com