Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

yaml - 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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...