59

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 ?

Samuel
  • 645
  • 1
  • 7
  • 11

3 Answers3

99

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.

Madhu Bhat
  • 13,559
  • 2
  • 38
  • 54
  • 4
    What is about multiple labels. For example if my PR hast two labels eg. `bug` and `security`. Does this approach will work? – Markus Dec 21 '20 at 21:56
  • 1
    @Markus this would trigger the workflow for every labelling event, so would work when you have multiple labels too. – Madhu Bhat Dec 22 '20 at 05:39
  • 10
    Too bad, we can only run on labeling event. I wish it can continues run the action as long as the label is still assigned to the PR> – Bozhao Mar 25 '21 at 22:07
  • 62
    @Bozhao You can do this by checking the `github.event.pull_request.labels` field. Here's a simplified example: `if: contains(github.event.pull_request.labels.*.name, '')`. You will also need to make sure you specify "labeled", "opened", "synchronize" and "reopened" in `pull_request.types` – Stephen Young Sep 09 '21 at 12:11
  • 4
    @StephenYoung You should include that as an answer. – Saiansh Singh Dec 29 '21 at 20:53
  • @StephenYoung how do you do it if you want to have a conditional where you don't want something to run if one or specific labels are present? For instance, I don't want to run a PR event workflow if 'maintenance' or 'WIP' are labels on the PR – Randy Jan 26 '22 at 16:36
  • Can we use similar label conditions on push? - From what I can see, labels are only available on the Pull Requests events, but wondered if anyone has found otherwise? – fuzzi Feb 22 '22 at 21:29
  • @Randy you can add a '!' before the expression: if: "!contains(github.event.pull_request.labels.*.name, 'hotfix')" – user1418336 Feb 28 '23 at 23:17
16

Per this comment, the condition to check is:

   if: contains(github.event.pull_request.labels.*.name, '<label_name>')
noah
  • 21,289
  • 17
  • 64
  • 88
  • 1
    If you add this on a `pull_request` workflow, it doesn't pick up the labels added after the PR was raised. In that case, one needs to close and re-open the PR or just create a new PR. – vishwarajanand Feb 12 '23 at 17:01
3

I didn't need to have the labeled type to make it work, just as this answer I've manage to have it working with this condition:

name: ExampleWorkflow
on:  
  pull_request:
    branches:
      - main
jobs:
  ExampleJob:
    if: contains(github.event.pull_request.labels.*.name, 'example-label')

This way, the workflow ExampleWorkflow will be triggered for any pull request with target branch main and the job ExampleJob will be skipped if the PR doesn't have the label example-label

Federico Jordan
  • 186
  • 1
  • 6