Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Travis CI: How to set environment variables for deployment script only?

Tags:

travis-ci

In my .travis.yml configuration, I'd like to set up deployment to different stages (development/production) depending on the branch that triggered the build. I'm using a shell script to deploy, i.e., the script provider.

The question: I need to pass a different environment variables for different stages (mostly AWS keys). And these variables need to be encrypted when stored in version control. So, I'd like to do something like this:

deploy:
  - provider: script
    script: ./deploy.sh development
    env:
      -secure: <encrypted AWS_* variables for dev>
    on:
      branch: master
  - provider: script
    script: ./deploy.sh production
    env:
      -secure: <encrypted AWS_* variables for prod>
    on:
      branch: release

The env key can be used only globally, however. Is it possible somehow to specify the environment only for the specific script and not anything else?

like image 604
Jarda Snajdr Avatar asked Apr 04 '17 15:04

Jarda Snajdr


People also ask

How do you set variables in environment?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable.

How do you set an environment variable in a script?

The easiest way to set environment variables in Bash is to use the “export” keyword followed by the variable name, an equal sign and the value to be assigned to the environment variable.


1 Answers

You accomplish this by restricting an environment variable to a specific branch when you define it using the repository settings in the web interface (I don't know how to do this using your .travis.yaml). You can create multiple variables with the same name so long as they are restricted to different branches. In your case, create AWS_SECRET_FOO for master with your dev credential and AWS_SECRET_FOO for release with your production credential.

Regardless, it would be awesome if you could define deployment-specific env vars simply for convenience.

like image 139
dcow Avatar answered Oct 16 '22 08:10

dcow