简体   繁体   中英

How to override .travis.yml environment variables from ruby script using travis client

I'm using the travis client to write a ruby script to interact with my TravisCI builds. I have a working.travis.yml file with a series of encrypted env vars. I'm trying to trigger the build from my script with a new env_var that overwrites one of the existing encrypted env vars in the travis.yml but I am unable to override the.yml configuration.

When making API call using CURL, I can successfully override the env var.

body='{
  "request": {
    "branch":"master",
    "config": {
      "merge_mode": "deep_merge",
      "env": {
        "SOME_ENV_VAR_DEFINED_IN_YML": '"$some_new_value_for_the_old_key"'
      }
    }
  }
}'
curl -s -X POST \
   -H "Content-Type: application/json" \
   -H "Accept: application/json" \
   -H "Travis-API-Version: 3" \
   -H "Authorization: token MYSECRETTOKEN" \
   -d "$body" \
   https://api.travis-ci.com/repo/MYORG%2FMYREPO/requests

This is what I'm trying to do with the client in the script but not able to.

I've tried getting the repo and setting some env vars thusly:

my_repo = Travis::Pro::Repository.find("MYREPO")
my_repo.env_vars.upsert("SOME_ENV_VAR_DEFINED_IN_YML", "some_new_value", public: true)

Nada. It sets the env var on the repo that I can see in the travis ui but doesn't override the.yml config.

I've also tried setting the config object on the build that I want.

build_that_i_am_targeting.config["global_env"][index_of_env_var]="SOME_ENV_VAR_DEFINED_IN_YML=some_new_value"

Also nada - when setting off the build it resets the global env vars to the previous version.

Link to what I thought was relevant documentation: https://github.com/travis-ci/travis.rb#build-environment-variables

I was considering using the client session (just below the above in the docummentation) but I feel like there is something simpler that I'm missing. Any suggestions/ideas much appreciated! Thanks!

I know this is very late, but for future reference. I'm not totally sure what you're trying to do, but I had a similar problem and here are two possible solutions.

  1. If your curl call is working the way you want it to, but you just need to convert it to ruby, you can use this online "curl to ruby converter" tool: https://jhawthorn.github.io/curl-to-ruby/

  2. If you want to add an environment variable to your.travis.yml file locally using travis ci (and subsequently push it out when you're ready), you can do something like:

 #your_ruby_script.rb env=some_value travis_cmd = "travis encrypt MY_NEW_ENV=#{env} --add env.global" system(travis_cmd)

This answer has more info on calling shell commands from ruby: How to call shell commands from Ruby

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM