Simon Finney
Simon Finney

Reputation: 469

Travis CI - Using repository environment variables in .travis.yml

I'm looking to declare environment variables in my Travis CI repository settings and use them in my .travis.yml file to deploy an application and post a build notification in Slack.

I've set environment variables in my Travis CI repository settings like so:

Travis CI repository environment variables

My .travis.yml file appears as the following:

language: node_js
node_js:
  - '0.12'
cache:
  directories:
    - node_modules
deploy:
  edge: true
  provider: cloudfoundry
  api: $CF_API
  username: $CF_USERNAME
  password: $CF_PASSWORD
  organization: $CF_ORGANIZATION
  space: $CF_SPACE
notifications:
  slack: $NOTIFICATIONS_SLACK

When I add the values into the .travis.yml file as they are, everything works as planned.

However, when I try to refer to the environment variables set in the repository, I receive no Slack notification on a build status, and the deployment fails.

Am I following this process correctly or is there something I'm overseeing?

Upvotes: 30

Views: 18669

Answers (1)

Dominic Jodoin
Dominic Jodoin

Reputation: 2538

I think it is probably too early in Travis CI's sequence for your environment variables to be read.

What I would suggest is to rather encrypt them using the travis command-line tool.

E.g.

$ travis encrypt
Reading from stdin, press Ctrl+D when done
username
Please add the following to your .travis.yml file:

secure: "TD955qR6qvpVIz3fLkGeeUhV76deeVRaLVYjW9YjV6Ob7wd+vPtACZ..."

Pro Tip: You can add it automatically by running with --add.

Then I would copy/paste the secure: "TD955qR6qvpVIz3fLkGeeUhV76d..." result at the appropriate place in your .travis.yml file:

language: node_js
node_js:
  - '0.12'
cache:
  directories:
    - node_modules
deploy:
  edge: true
  provider: cloudfoundry
  api:
    secure: "bHU4+ZDFeZcHpuE/WRpgMBcxr8l..."
  username:
    secure: "TD955qR6qvpVIz3fLkGeeUhV76d..."

You can have more details about how to encrypt sensitive data on Travis CI here.

Hope this helps.

Upvotes: 19

Related Questions