88

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 ?

0

3 Answers 3

145

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.

Sign up to request clarification or add additional context in comments.

What is about multiple labels. For example if my PR hast two labels eg. bug and security. Does this approach will work?
@Markus this would trigger the workflow for every labelling event, so would work when you have multiple labels too.
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 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, '<label_name>'). You will also need to make sure you specify "labeled", "opened", "synchronize" and "reopened" in pull_request.types
@StephenYoung You should include that as an answer.
43

Per this comment, the condition to check is:

   if: contains(github.event.pull_request.labels.*.name, '<label_name>')

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.
It depends on your trigger. If you don't have a PR labeled trigger in your workflow then it wouldn't work. You need this: on: pull_request: types: [ labeled ] @vishwarajanand
@vishwarajanand I think you're only partially correct. If you push up another commit, Github will recognize the new labels.
You both seem correct, it seems due to the default pull_request activity type. docs.github.com/en/actions/using-workflows/… suggests default activity type is opened, synchronize, or reopened and not labelled.
10

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

Comments

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.