okaryo.log

Automatically Adding Labels to Pull Requests Targeting the main Branch with GitHub Actions | okaryo.log

Automatically Adding Labels to Pull Requests Targeting the main Branch with GitHub Actions

    #GitHubActions#GitHub

Introduction

I use the main branch as a release branch and check out development branches from the develop branch. When releasing, I create a Pull Request from the develop branch to the main branch and merge it.

I used to manually add a release label to this Pull Request every time, but I wanted to automate it, so I set up a workflow for this purpose. By the way, the label on the Pull Request is used for automatically generating release notes.

Workflow

Here’s the workflow I set up.

name: Label Release PR

on:
  pull_request:
    types: opened
    branches:
      - main

jobs:
  label-release:
    if: ${{ github.base_ref == 'main' && github.head_ref == 'develop' }}
    runs-on: ubuntu-latest
    steps:
      - name: Apply release label
        uses: actions-ecosystem/action-add-labels@v1
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          labels: release

The workflow is triggered when a Pull Request is created for the main branch. In my case, the trigger is only on Pull Request creation, but if you want to trigger it when changes are made to the Pull Request, you can specify types: [ opened, synchronize ].

on:
  pull_request:
    types: opened
    branches:
      - main

Here, github.base_ref and github.head_ref are used to control the job so that it only runs when a Pull Request is created from the develop branch to the main branch.

if: ${{ github.base_ref == 'main' && github.head_ref == 'develop' }}

Then, the workflow adds a label to the Pull Request. The action used here is actions-ecosystem/action-add-labels. You can specify the label you want to add with labels.

- name: Apply release label
  uses: actions-ecosystem/action-add-labels@v1
  with:
    labels: release

If you want to add multiple labels, you can specify them as follows.

with:
  labels: |
    release
    feature

Conclusion

This time, I only added a label to release-related Pull Requests, but it could also be possible to add labels like feature or bug based on the prefix in the Pull Request title, such as feat: or fix:.

By automating these routine tasks that require human judgment or operation, which can be prone to inconsistency or error, we can reduce mistakes and make our work easier.


Related Posts
Related Posts
Promotion

This site uses Google Analytics.