Auto Pull Request Merge in GitHub Actions

Photo by Icons8 Team on Unsplash

Auto Pull Request Merge in GitHub Actions

ยท

2 min read

I write my blog almost daily, and I have to add new articles to it via Pull request. I wanted to schedule the pull request so that the article reaches readers at a proper time. Unfortunately, GitHub doesn't have a native solution to schedule the merge of the pull request. As a result, I searched Google and discovered a fantastic solution for scheduling the pull request merge in GitHub.

In order to schedule a merge, we need to work with the GitHub Actions built by Gregor Martynus to do the job.

Step 1: Create a workflow

Inside the root directory of your project, create a new folder .github/workflows. Inside the workflows folder create a YAML file merge-schedule.yml:

name: Merge Schedule

on:
  pull_request:
    types:
      - opened
      - edited
      - synchronize
  schedule:
    - cron: '0 1 * * *'

jobs:
  merge_schedule:
    runs-on: ubuntu-latest
    steps:
      - uses: gr2m/merge-schedule-action@v2
        with:
          # Merge method to use. Possible values are merge, squash or
          # rebase. Default is merge.
          merge_method: squash
          # Time zone to use. Default is UTC.
          time_zone: 'Asia/Kolkata'
          # Require all pull request statuses to be successful before
          # merging. Default is `false`.
          require_statuses_success: 'true'
          # Label to apply to the pull request if the merge fails. Default is
          # `automerge-fail`.
          automerge_fail_label: 'merge-schedule-failed'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

In the above YAML file, you can see the CRON job schedule. I am using it at 1 am IST every day. You can create your own schedule. Make sure you check out crontab.guru will help you to create a CRON schedule easily.

After creating the YAML file, commit it to GitHub.

Step 2: Schedule pull request

Add a line like this to the end of the pull request description in your pull requests.

/schedule 2022-09-22

The pull request merge will be completed at 1 a.m. IST on September 9, 2022.

You will now be able to easily schedule merge pull requests.

Thanks for reading

Follow me on Twitter

Thanks for reading!

ย