How do I create a dynamic environment variable?

Hello,

I have a node.js iOS app which uses TravisCI to make its builds.
I have a file environment.txt with the following content
PROD=1.0.1
STAGE=2.0.1

How can I define a global variable “abc” inside travis.yml which first checks for branch its building from and if the branch name is PROD, assigns abc=1.0.1 and if the branch is staging, assigns abc=2.0.1

I have tried sourcing it from a script but that is currently not working.

Thanks
Abhineet

You can inspect the branch name with TRAVIS_BRANCH.

Then, you can use bash indirection to get the value of the environment variable whose name is indicated by the value of TRAVIS_BRANCH:

$ PROD=1.0.1
$ STAGE=2.0.1
$ TRAVIS_BRANCH="PROD"
$ abc=${!TRAVIS_BRANCH}
$ echo ${abc}
1.0.1

Once I have found out the value of abc, how can I declare this environment variable globally so that it can be used when the app starts the build?

For instance: my env variables are declared like this (how can I declare abc here with dynamic value)

env:
matrix:

  • LABEL=google APP_PID=com.google.press MOBILE_PREFIX=GoogleTube
    global:
  • APP_NAME=“GooglePress”
  • ‘DEVELOPER_NAME=“iPhone Distribution: travis (CE7D32W2E2)”’
  • MATCH_KEYCHAIN_NAME=“ios-build.keychain”
  • S3_BUCKET=google-mobile-downloads

how can I declare this environment variable globally so that it can be used when the app starts the build?

export it like a regular shell variable. For example, this is part of script: in the Unbound library.

    elif [ "$TEST_IOS" = "yes" ]; then
      export AUTOTOOLS_BUILD="$(./config.guess)"
      export PKG_CONFIG_PATH="$IOS_PREFIX/lib/pkgconfig"
      source ./contrib/ios/setenv_ios.sh
      ./contrib/ios/install_tools.sh
      ./contrib/ios/install_openssl.sh
      ./contrib/ios/install_expat.sh
      ./configure \
        --build="$AUTOTOOLS_BUILD" --host="$AUTOTOOLS_HOST" \
        --prefix="$IOS_PREFIX" \
        --with-ssl="$IOS_PREFIX" --disable-gost \
        --with-libexpat="$IOS_PREFIX";
      make -j 2
      make install