Have actions *not* run a job if a label is set

All,

I think I’m an idiot as I can’t seem to figure this out. Namely, a repo I work with can have both trivial and non-trivial PRs. We have our developers label the trivial ones with a label “0 diff trivial”. What I’d like is to have those PRs not run a rather expensive CI step (takes 30 minutes to build) as changing a README file shouldn’t need that.

So following some other threads here I have something a la:

name: Build Tests

on:
  pull_request:
    types: [opened, synchronize, reopened, labeled, unlabeled]

jobs:
  build_cheap:
    name: Build Cheap
    runs-on: ubuntu-latest
    container: foo
    steps:
      - name: Cancel Previous Runs
        uses: styfle/cancel-workflow-action@0.4.0
        with:
          access_token: ${{ github.token }}
      - name: Checkout
        uses: actions/checkout@v2
...
  build_expensive:
    name: Build expensive
    if: "!contains(github.event.pull_request.labels.*.name, 'trivial')"
    runs-on: ubuntu-latest
    container: foo
    steps:
      - name: Cancel Previous Runs
        uses: styfle/cancel-workflow-action@0.4.0
        with:
          access_token: ${{ github.token }}
      - name: Checkout GCM
        uses: actions/checkout@v2

My guess is my problem is with not knowing GitHub Actions if syntax. I thought:

    if: "!contains(github.event.pull_request.labels.*.name, 'trivial')"

was the right way to say “Run this job step only if the labels do not have trivial”, but tests seem to show this causes an expensive test. :confused:

(Note: I might have other issues as well. Since I care about labels, I added to the types of the on: pull_request. I also tried adding the “Cancel Previous Runs” action since I figured we sometimes have to push to our PRs. But I look and it’s not cancelling so…)

Sigh. I might have figured this out. I actually decided to read the documentation carefully and:

contains( search, item )

Returns true if search contains item . If search is an array, this function returns true if the item is an element in the array. If search is a string, this function returns true if the item is a substring of search . This function is not case sensitive. Casts values to a string.

After staring at the GitHub API docs, labels is an array, which means contains seems to be looking for a “full string” (as it were) not just something in the string. Thus:

    if: "!contains(github.event.pull_request.labels.*.name, '0 diff trivial')"

seems more correct.

2 Likes