Run Github Actions when pull requests have a specific label

天涯浪子 提交于 2021-01-02 15:30:32

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/62325286/run-github-actions-when-pull-requests-have-a-specific-label

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!