Automate merging release branches into your main branch with GitHub Actions

 5 months ago
source link: https://www.jessesquires.com/blog/2022/03/26/gh-action-merge-release-to-main/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Automate merging release branches into your main branch with GitHub Actions

26 March 2022

A typical release process for Git workflows involves creating a release branch, performing various tests on that branch, and applying any necessary fixes or changes to that branch. Once stable and ready to release, you create a build from the release branch, create a git tag, and finally merge the release branch changes back into your main branch.

It isn’t difficult to keep your branches in sync, but it is tedious. And sometimes, you need the fixes from your release branch back on your main branch immediately. Furthermore, if you want to try to avoid git conflicts, it is best to merge your release branch into your main branch frequently throughout your release process rather than waiting until the very end. This makes merging your release branch into your main branch a great task to automate. We can create a workflow using GitHub Actions to do this for us.

The following workflow will create a pull request from release/* branches that targets the repository’s default branch. It runs every time there is a push or merge to the release branch, which means you’ll get any fixes or changes from release/* back into main almost immediately. And because it opens a pull request, it gives your team a chance to double-check and review the changes. It does not handle conflicts (obviously), so if there are conflicts between release/* and main, you’ll have to resolve those manually — but that’s what you would have to do without this automation anyway. However, in many scenarios, this should save you a lot of time.

name: Merge Release Into Main

      - 'release/*'

    name: Create PR Release to Main
    runs-on: ubuntu-latest
      - name: git checkout
        uses: actions/[email protected]
          token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}

      # https://github.com/marketplace/actions/github-pull-request-action
      - name: create pull request
        id: open-pr
        uses: repo-sync/[email protected]
          github_token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
          destination_branch: ${{ github.event.repository.default_branch }}
          pr_title: "[Automated] Merge ${{ github.ref_name }} into ${{ github.event.repository.default_branch }}"
          pr_body: "Automated Pull Request"
          pr_reviewer: "jessesquires"
          pr_assignee: "jessesquires"

      # https://github.com/marketplace/actions/enable-pull-request-automerge
      - name: enable automerge
        if: steps.open-pr.outputs.pr_number != ''
        uses: peter-evans/[email protected]
          token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
          pull-request-number: ${{ steps.open-pr.outputs.pr_number }}
          merge-method: merge

You can customize this to fit your needs. For example, if you name your release branches differently than release/*, you’ll need to make that change. You will also need to configure the title, description, reviewers, and assignees for the pull request. Also note that you’ll need to configure a personal access token for everything to work properly.

The workflow uses two third-party actions, GitHub Pull Request Action and Enable Pull Request Automerge. The second one is particularly nice to have, because it will setup the pull request to automerge. This means all your team needs to do is approve the pull request, and after all CI status checks pass it will merge automatically.

I hope this helps improve your release process! If you enjoyed this post, check out my other posts about GitHub Actions. You can also find my collection of GitHub Actions workflows on GitHub here.

About Joyk

Aggregate valuable and interesting links.
Joyk means Joy of geeK