Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Github Actions when pull requests have a specific label

After reading the documentation of the Events that trigger workflows, I wonder if it's possible to run a workflow with a given label name, like RFR or WIP.

I know we can run a workflow when the pull request is labeled, but there is nothing more for a specifc label name :

on:
  pull_request:
    types: [labeled]

Has anyone done this before ?

like image 360
Samuel Avatar asked Jun 11 '20 13:06

Samuel


People also ask

What is label on GitHub actions?

Label Actions is a GitHub bot that performs certain actions when issues, pull requests or discussions are labeled or unlabeled.

Can GitHub secrets be scoped to the organization level?

Secrets can be stored within GitHub at three different levels: the organization, a single repository, or a repository environment. The level at which the secret should be stored depends on its scope and intended use.


1 Answers

You can achieve running a workflow on labeling a Pull Request using a conditional expression like

if: ${{ github.event.label.name == 'label_name' }}

So if you have your GitHub action config as below

name: CI

on:
  pull_request:
    types: [ labeled ]

jobs:
  build:
    if: ${{ github.event.label.name == 'bug' }}
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Run a one-line script
      run: echo Hello, world!

It would trigger the workflow whenever a PR is labeled and run the job only if the label is bug and would skip if the label is anything else. You can also use github.event.action == 'labeled' as an extra check but that is not required if you have only types: [ labeled ] for the pull_request as shown in the config above.

Note: Just for your information, the github event has the following info (removed the irrelevant data for brevity) regarding the label in case of labelling a PR

"event": {
    "action": "labeled",
    "label": {
      "color": "d73a4a",
      "default": true,
      "description": "Something isn't working",
      "id": 1519136641,
      "name": "bug",
      "node_id": "abcd",
      "url": "https://api.github.com/repos/owner/repo/labels/bug"
    }
}

GitHub actions documentation regarding conditional expressions is here.

like image 94
Madhu Bhat Avatar answered Oct 06 '22 08:10

Madhu Bhat