Automatically schedule a git merge using GitHub Actions

All of the articles in this blog are published by creating a pull request with the article, and then merging that pull request into the main branch.

Then, using Netlify's continuous deployment tools, the site deploys with the new change.

However, one of the difficulties of blogging every day is there will inevitably be days where I'm too busy to write. There will also be days where I will have enough free time (and concentration) to write 3 or 4 posts at once!

I could create the pull requests for each article, and then merge them on the associated day, but most probably I'll forget.

What I need is a process that will automatically merge the pull request on a specified date.

Fortunately, GitHub actions has the answer.

Merge Schedule Action

Using the action gr2m/merge-schedule-action@v1, I've been able to automate the process.

All I needed to do was create a folder called .github\workflows in the root of my project directory, then add the following to a file I've named 'merge-schedule.yml':

name: Merge Article Schedule
on:
  pull_request:
    types:
      - opened
      - edited
  schedule:
    # 9AM UTC
    - cron: 0 9 * * *

jobs:
  merge_schedule:
    runs-on: ubuntu-latest
    steps:
      - uses: gr2m/merge-schedule-action@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

This will run the action every day, at 9AM UTC.

Specify the date

This action will look at all the pull requests, and merge those that are ready to be merged.

But how does it know which are ready to be merged?

To specify this, you need to add a line to the end of the pull request description, in this format:

/schedule 2021-03-31

If you're running the job more than once a day, you may also wish to specify a time:

/schedule 2021-03-31T10:30:00.000Z

Additional options

The script above is how I currently have it set up on this blog.

If you need to change the merge method, or you wish to use a different time zone, you can do so:

name: Merge Article Schedule
on:
  pull_request:
    types:
      - opened
      - edited
  schedule:
    # 9AM PDT
    - cron: 0 9 * * *

jobs:
  merge_schedule:
    runs-on: ubuntu-latest
    steps:
      - uses: gr2m/merge-schedule-action@v1
        with:
          # Use squash instead of merge. Possible values are merge, squash or rebase.
          merge_method: squash          # Use LA time zone (PDT) instead of UTC
          time_zone: "America/Los_Angeles"        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Get it on GitHub

You can find the repository here: g2rm/merge-schedule-action