Amedeo
SYMBIOSYS OF
PURPOSE & STYLE
Follow us

Search

ekusiadadus
  -  Programming   -  GitHub   -  Auto Create and Auto Merge using GitHub Actions by Cron
Auto Create and Auto Merge using GitHub Actions by Cron

How to Auto Create and Auto Merge using GitHub Actions

I wrote GitHub Actions yaml code, which enables creating a pull request automatically, and merging the pull request automatically.

name: Auto merge dev2stage

on:
  schedule:
    - cron: "0 15 * * *"

jobs:
  auto-merge-dev2stage:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
        with:
          ref: dev
      - name: 【定期実行】Create Pull Request dev2stage
        id: create-pull-request
        env:
          GH_TOKEN: ${{ secrets.AUTO_DEP_TOKEN }}
        run: |
          PULL_REQUEST_URI=$(gh pr create -B stage -t 【定期実行】dev2stage -l 'auto merge' -b "")
          echo "::set-output name=PULL_REQUEST_URI::$PULL_REQUEST_URI"
      - name: 【定期実行】Merge Pull Request dev2stage
        env:
          GH_TOKEN: ${{ secrets.AUTO_DEP_TOKEN }}
        run: |
          gh pr merge ${{steps.create-pull-request.outputs.PULL_REQUEST_URI}} --merge

Cron

Using cron, the Action runs on specific time on the day.

on:
  schedule:
    - cron: "0 15 * * *"

In this case this Action runs on 15:00(WST).

Creating a Pull Request

      - name: 【定期実行】Create Pull Request dev2stage
        id: create-pull-request
        env:
          GH_TOKEN: ${{ secrets.AUTO_DEP_TOKEN }}
        run: |
          PULL_REQUEST_URI=$(gh pr create -B stage -t 【定期実行】dev2stage -l 'auto merge' -b "")
          echo "::set-output name=PULL_REQUEST_URI::$PULL_REQUEST_URI"

With GitHub CLI、a pull request can be submitted.
`gh pr create` command create a pull request with some options.

-B: the base branch
t: the title of the pull request.
-l: label of the pull request.
-b: body of the pull request

In some cases, error occurs.

  1. Already a pull request exists from the branch to base branch
  2. Some conflicts exist
  3. No diffs

Auto Merge

      - name: 【定期実行】Merge Pull Request dev2stage
        env:
          GH_TOKEN: ${{ secrets.AUTO_DEP_TOKEN }}
        run: |
          gh pr merge ${{steps.create-pull-request.outputs.PULL_REQUEST_URI}} --merge

With GitHub CLI

Leave a Comment