Custom conditions for conditional builds?

Referring to this page: Conditions - Travis CI

Travis allows you to use these built in attributes to be used when defining your conditions for conditional builds/stages/jobs. I have an attribute that is not covered in the provided list: I’d like to use Git labels to determine if a set of stages should run or not. So if a pull request has a *release" label on it, then it will run additional stages, otherwise they will be skipped.

I’ve written a script that will either output true or false if the release label is included or not, and I was hoping to use this to help decide if a stage should run or not. Is this possible to do with the .travis.yaml?

If your ‘label’ is a “git tag” you can set the “tags” to true to make it only applicable to tagged branches, for example like this:

- provider: script
  edge: true
  script: >-
          bash deployConfig/deploy.sh
  skip_cleanup: true
  on:
    all_branches: true
    tags: true

Hey Hubert, thanks for the reply! I meant the actual labels that you can assign to a PR from Github, not Git tags. Is there any way using just these labels to create conditional stages?

Basically I want to know if something like this is possible:

env:
  - IS_RELEASE = false

before_install:
  - git clone git@github.com:foo/bar.git foobar
  - foobar/check_labels_for_release_tag.sh  ${PR_NUMBER} ${GIT_TOKEN}
 # this script should set the IS_RELEASE above to either "true" or "false". It will use the Git API to check the PR to see if a label with the name "release" is on it or not.

jobs:
  include:
    - stage: Build
      name: "Build"
      script: foobar/build.sh

    - stage: Test
      name: "Test"
      script: foobar/test.sh

    # the following stage only runs if the IS_RELEASE variable is set to true
    - stage: Release
      name: "Release"
      script: foobar/release.sh
      if: IS_RELEASE = true

Not possible as a job condition. But possible to run a command early in the job and terminate the job early with travis_terminate if the condition is not met.

You’ll need to do a Github API request to retrieve labels.

Would you mind elaborating on what might look like? I do have a script that grabs the Git labels but I’m not sure how to use the output of that to actually terminate. Say I wanted to run the build and tests stages, then skip the release stage, where would I call travis_terminate?

The other issue is, if we terminate, does that mean the remaining stages will fail? In that case we would not be able to merge the pull request being tests, which I don’t want. What I’m trying to achieve here is different stages running based on the git labels present on the PR.

Call travis_terminate <exit code> in the same shell process depending on the result of the check. E.g.:

before_install:
- if ! get_git_labels | grep -qxF release; then travis_terminate 0; fi

Note that this terminates the job immediately, without any default finishing steps like cache saving. So it’s chiefly useful only for cases like this, where you want to quit ASAP and don’t need anything else in the job to run.

My method of fetching github label using Github CLI:

First set GH_TOKEN or GH_ENTERPRISE_TOKEN (and GH_HOST in this case). Then,

gh pr view $TRAVIS_PULL_REQUEST --json labels --jq '.labels[].name'

This can then be used in conditional logic.