Is it possible to set conditional environment variables in travis?

跟風遠走 提交于 2019-12-08 03:24:16

问题


I am running on travis on 5 versions of nodeJS, .travis.yml is ....

language: node_js
node_js:
  - 5.0
  - 4.0
  - 0.12.7
  - 0.10.40
  - 0.10.36
before_install:
  - npm install -g grunt-cli
script:
  - npm run travis

I want to set a travis environment variable for the run on nodeJS 5.0 only

something like this ...

language: node_js
node_js:
  - 5.0
    - env: POST_TO_COVERALLS=true
  - 4.0
  - 0.12.7
  - 0.10.40
  - 0.10.36
before_install:
  - npm install -g grunt-cli
script:
  - npm run travis

but this is invalid ... anyone know how to do this ...

1 - Preferably, via .travis.yml

2 - If not, through the travis web application

I KNOW HOW TO DO THIS VIA CODE - But can it be done through travis?

Thanks all


回答1:


What about using the matrix to explicitly include the one "5.0" build where your environment variable is set to true (see documentation on explicitly including builds).

It would be something like the following

language: node_js
node_js:
  - 4.0
  - 0.12.7
  - 0.10.40
  - 0.10.36
env:
  POST_TO_COVERALLS=false
matrix:
    include:
       - node_js: 5.0
         env: POST_TO_COVERALLS=true
before_install:
  - npm install -g grunt-cli
script:
  - npm run travis



回答2:


Newly introduced Travis build stages combined with some YML inheritance and and some hacking around assigning the Bash variable can be very handy for this:

jobs:
  include:
    - stage: Tests
      script: # run tests
    - script: # run more tests
    - &deploy-stuff
      stage: Deploy
      if: branch != master
      env:
      - ENV=$(if [ "$SOMETHING" = "thing" ]; then echo ${TRAVIS_BRANCH//\//-}; else echo "staging"; fi)
      script: # do some things before deploy
      deploy:
      - provider: script
        skip_cleanup: true
        script: echo $ENV
        on:
          all_branches: true
          condition: $TRAVIS_BRANCH = "devel" || $SOMETHING = "thing"
    - <<: *deploy-stuff
      if: branch = master
      env:
      - ENV="production"
      deploy:
      - provider: script
        skip_cleanup: true
        script: echo $ENV
        on:
          branch: master


来源:https://stackoverflow.com/questions/33706166/is-it-possible-to-set-conditional-environment-variables-in-travis

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!