Having troubles with env()

I am trying to use env() in a .travis.yml config file and it does not work as I understand it can work from the docs: https://docs.travis-ci.com/user/conditions-v1#function-calls.

UPDATE: See this comment below for a more up-to-date example. Leaving original example in order to preserve the coherence of the comments.

An dummy example:

The config’s most relevant part:

env:
  global:
  - ENV_BRANCH: "env-branch"

stages:
- name: One
  if: branch = env(ENV_BRANCH)
- name: Two
  if: branch = "env-branch"

jobs:
  include:
  - stage: One
    install: skip
    script: echo "Stage One depends on env()"
  - stage: Two
    name: Job Zero
    install: skip
    script: echo "Stage Two does NOT depend on env()"
  - if: branch = env(ENV_BRANCH)
    name: Job One
    install: skip
    script: echo "Job One depends on env()"
  - if: branch = "env-branch"
    name: Job Two
    install: skip
    script: echo "Job Two does NOT depend on env()"
  • Stage One, which depends on env() does not get built.
  • Stage Two gets built partially only as its Job One which depends on env() does not get built.

UPDATE
As pointed out by @native-api, ENV_BRANCH gets parsed as _BRANCH and changing the env var to SOMEBRANCH fixes the problem of the missing job but not the missing stage.

UPDATE
Created a standalone repo to demonstrate the problem and experiment with the env() function call. The builds are at https://travis-ci.com/sbellem/travis-env-func-experiments.

Where is ENV_BRANCH envvar defined? I don’t see it defined anywhere.

Sorry, just updated the post. I changed the original script to make it simpler and forgot to change the env var.

As per https://docs.travis-ci.com/user/conditions-testing:

> travis-conditions parse "branch = env(ENV_BRANCH)"
[:eq, [:var, :branch], [:call, :env, [[:val, "_BRANCH"]]]]

This is the reason… ENV is parsed as a dummy env() call. Whether it should be like this is another question…

Thanks for checking this. If I change the env var name to SOMEBRANCH the missing job is now included but not the missing stage: https://travis-ci.org/sbellem/HoneyBadgerMPC/builds/626027377.

In the hope of making the example clearer and also to allow for future “experimentation” I moved the example to a standalone repository. The TravisCI builds are at https://travis-ci.com/sbellem/travis-env-func-experiments/.

As per the docs on Conditional Stages:

stages:
  - name: deploy
    # require the branch name to be master (note for PRs this is the base branch name)
    if: branch = master

So what seems to not work is the above approach, meaning when the condition is included in the block stages to specify the ordering. If the condition is specified in the stage definition block then it works. Consider this config:

env:
  global:
  - SOMEBRANCH: "dev"

stages:
- if: branch = env(SOMEBRANCH)
  name: One
- name: Two
  if: branch = "dev"
- name: Three
  if: branch = env(SOMEBRANCH)
- name: Hope
  if: branch = env(SOMEBRANCH)
- name: Four

jobs:
  include:
  - stage: One
    name: Job 0
    install: skip
    script: echo "Stage One depends on env()"
  - stage: Two
    name: Job Zero
    install: skip
    script: echo "Stage Two does NOT depend on env()"
  - if: branch = env(SOMEBRANCH)
    name: Job One
    install: skip
    script: echo "Job One depends on env()"
  - if: branch = "dev"
    name: Job Two
    install: skip
    script: echo "Job Two does NOT depend on env()"
  - stage: Three
    name: Hope
    install: skip
    script: echo "Stage Three depends on env()"
  - if: branch = env(SOMEBRANCH)
    stage: Four
    name: Persevere
    install: skip
    script: echo "Stage Four depends on env()"

Only stages Two and Four are built.