# Custom conditions for conditional builds?

**URL:** <https://travis-ci.community/t/custom-conditions-for-conditional-builds/11873>\
**Category:** Travis CI Discussions & Feedback\
**Tags:** github-integration\
**Created:** [July 19, 2021, 7:23pm UTC](https://travis-ci.community/t/custom-conditions-for-conditional-builds/11873 "2021-07-19T19:23:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SakeebHossain](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/sakeebhossain/32/6890_2.png) [@SakeebHossain](https://travis-ci.community/u/SakeebHossain)\
**Post date:** [July 19, 2021, 7:23pm UTC](https://travis-ci.community/t/custom-conditions-for-conditional-builds/11873/1 "2021-07-19T19:23:38Z")

</div>

Referring to this page: [Conditions - Travis CI](https://docs.travis-ci.com/user/conditions-v1#integration)

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?

---

<div class="post-metadata">

**Author:** ![HubertWagnerVE](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/hubertwagnerve/32/5831_2.png) [@HubertWagnerVE](https://travis-ci.community/u/HubertWagnerVE)\
**Post date:** [July 20, 2021, 1:42am UTC](https://travis-ci.community/t/custom-conditions-for-conditional-builds/11873/2 "2021-07-20T01:42:26Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![SakeebHossain](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/sakeebhossain/32/6890_2.png) [@SakeebHossain](https://travis-ci.community/u/SakeebHossain)\
**Post date:** [July 20, 2021, 3:38pm UTC](https://travis-ci.community/t/custom-conditions-for-conditional-builds/11873/3 "2021-07-20T15:38:52Z")

</div>

![Untitled](https://us1.discourse-cdn.com/flex015/uploads/travis_ci/original/2X/5/5acf6f437183dc828b97158361812760e266b132.png)

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?

---

<div class="post-metadata">

**Author:** ![SakeebHossain](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/sakeebhossain/32/6890_2.png) [@SakeebHossain](https://travis-ci.community/u/SakeebHossain)\
**Post date:** [July 20, 2021, 7:00pm UTC](https://travis-ci.community/t/custom-conditions-for-conditional-builds/11873/4 "2021-07-20T19:00:54Z")

</div>

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

```auto
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

```

---

<div class="post-metadata">

**Author:** ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)\
**Post date:** [July 21, 2021, 9:32am UTC](https://travis-ci.community/t/custom-conditions-for-conditional-builds/11873/5 "2021-07-21T09:32:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SakeebHossain](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/sakeebhossain/32/6890_2.png) [@SakeebHossain](https://travis-ci.community/u/SakeebHossain)\
**Post date:** [July 21, 2021, 1:46pm UTC](https://travis-ci.community/t/custom-conditions-for-conditional-builds/11873/6 "2021-07-21T13:46:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)\
**Post date:** [July 21, 2021, 5:50pm UTC](https://travis-ci.community/t/custom-conditions-for-conditional-builds/11873/7 "2021-07-21T17:50:42Z")

</div>

> [@SakeebHossain](#):
>
> Would you mind elaborating on what might look like?

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![mbrotos](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/mbrotos/32/8128_2.png) [@mbrotos](https://travis-ci.community/u/mbrotos)\
**Post date:** [July 20, 2022, 4:06pm UTC](https://travis-ci.community/t/custom-conditions-for-conditional-builds/11873/8 "2022-07-20T16:06:27Z")

</div>

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.
