Skip to content

Commit f5079f6

Browse files
committed
Rewrite of Env Vars and moving default vars into env vars topic
1 parent 00185d2 commit f5079f6

File tree

2 files changed

+121
-124
lines changed

2 files changed

+121
-124
lines changed

user/ci-environment.md

+1-63
Original file line numberDiff line numberDiff line change
@@ -168,75 +168,13 @@ Language-specific workers have multiple runtimes for their respective language (
168168

169169
### Environment variables
170170

171-
* `CI=true`
172-
* `TRAVIS=true`
173-
* `CONTINUOUS_INTEGRATION=true`
174-
* `DEBIAN_FRONTEND=noninteractive`
175-
* `HAS_JOSH_K_SEAL_OF_APPROVAL=true`
176-
* `USER=travis` (**do not depend on this value**)
177-
* `HOME=/home/travis` (**do not depend on this value**)
178-
* `LANG=en_US.UTF-8`
179-
* `LC_ALL=en_US.UTF-8`
180-
* `RAILS_ENV=test`
181-
* `RACK_ENV=test`
182-
* `MERB_ENV=test`
183-
* `JRUBY_OPTS="--server -Dcext.enabled=false -Xcompile.invokedynamic=false"`
184-
185-
Additionally, Travis CI sets environment variables you can use in your build, e.g.
186-
to tag the build, or to run post-build deployments.
187-
188-
* `TRAVIS_BRANCH`:For builds not triggered by a pull request this is the
189-
name of the branch currently being built; whereas for builds triggered
190-
by a pull request this is the name of the branch targeted by the pull
191-
request (in many cases this will be `master`).
192-
* `TRAVIS_BUILD_DIR`: The absolute path to the directory where the repository
193-
being built has been copied on the worker.
194-
* `TRAVIS_BUILD_ID`: The id of the current build that Travis CI uses internally.
195-
* `TRAVIS_BUILD_NUMBER`: The number of the current build (for example, "4").
196-
* `TRAVIS_COMMIT`: The commit that the current build is testing.
197-
* `TRAVIS_COMMIT_RANGE`: The range of commits that were included in the push
198-
or pull request.
199-
* `TRAVIS_JOB_ID`: The id of the current job that Travis CI uses internally.
200-
* `TRAVIS_JOB_NUMBER`: The number of the current job (for example, "4.1").
201-
* `TRAVIS_PULL_REQUEST`: The pull request number if the current job is a pull
202-
request, "false" if it's not a pull request.
203-
* `TRAVIS_SECURE_ENV_VARS`: Whether or not secure environment vars are being
204-
used. This value is either "true" or "false".
205-
* `TRAVIS_REPO_SLUG`: The slug (in form: `owner_name/repo_name`) of the
206-
repository currently being built. (for example, "travis-ci/travis-build").
207-
* `TRAVIS_OS_NAME`: On multi-OS builds, this value indicates the platform the job is running on.
208-
Values are `linux` and `osx` currently, to be extended in the future.
209-
* `TRAVIS_TAG`: If the current build for a tag, this includes the tag's name.
210-
211-
Language-specific builds expose additional environment variables representing
212-
the current version being used to run the build. Whether or not they're set
213-
depends on the language you're using.
214-
215-
* `TRAVIS_GO_VERSION`
216-
* `TRAVIS_JDK_VERSION`
217-
* `TRAVIS_JULIA_VERSION`
218-
* `TRAVIS_NODE_VERSION`
219-
* `TRAVIS_OTP_RELEASE`
220-
* `TRAVIS_PERL_VERSION`
221-
* `TRAVIS_PHP_VERSION`
222-
* `TRAVIS_PYTHON_VERSION`
223-
* `TRAVIS_RUBY_VERSION`
224-
* `TRAVIS_RUST_VERSION`
225-
* `TRAVIS_SCALA_VERSION`
226-
227-
The following environment variables are available for Objective-C builds.
228-
229-
* `TRAVIS_XCODE_SDK`
230-
* `TRAVIS_XCODE_SCHEME`
231-
* `TRAVIS_XCODE_PROJECT`
232-
* `TRAVIS_XCODE_WORKSPACE`
171+
There is a [list of default environment variables](/user/environment-variables#Default-Environment-Variables) available in each build environment.
233172

234173
### Libraries
235174

236175
* OpenSSL
237176
* ImageMagick
238177

239-
240178
### apt configuration
241179

242180
apt is configured to not require confirmation (assume -y switch by default) using both `DEBIAN_FRONTEND` env variable and apt configuration file. This means `apt-get install -qq` can be used without the -y flag.

user/environment-variables.md

+120-61
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,43 @@ layout: en
44
permalink: /user/environment-variables/
55
---
66

7-
A common way to customize the build process is to use environment variables. These can be accessed from any stage in your build process.
7+
A common way to customize the build process is to use environment variables, which can be accessed from any stage in your build process.
88

99
<div id="toc"></div>
1010

11-
## Where to define variables
11+
* Variables defined in [.travis.yml](#Defining-Variables-in-.travis.yml) are tied to a certain commit. Changing them requires a new commit, restarting an old build uses the old values. They are also available automatically on forks of the repository. Define variables in `.travis.yml` that:
1212

13-
Travis CI offers two different places for defining environment variables: In the [.travis.yml](#Variables-in-the-.travis.yml) or using the [repository settings](#Using-Settings). These differ slightly in their scope.
13+
+ are needed for the build to run and that don't contain sensitive data. For instance, a test suite for a Ruby application might require `$RACK_ENV` to be set to `test`.
14+
+ differ per branch.
15+
+ differ per job.
1416

15-
Variables in the **.travis.yml** are bound to a certain commit. Changing them requires a new commit, restarting an old build will use the old values. They will also be available automatically on forks of the repository.
17+
* Variables defined in [repository settings](#Defining-Variables-in-Repository-Settings) are the same for all builds. When you restart an old build, it uses the latest values. These variables are not automatically available to forks. Define variables in the Repository Settings that:
1618

17-
On the other hand, variables defined in the **repository settings** are the same for all builds. When you restart an old build, it will use the latest values. These variables are not automatically available to forks.
19+
+ differ per repository.
20+
+ contain sensitive data, such as third-party credentials.
1821

19-
Keep this in mind when choosing where to store which variable.
22+
* Use [Encrypted variables](#Encrypted-Variables) for sensitive data such as authentication tokens.
2023

21-
Examples for using the **.travis.yml**:
24+
> If you define a variable with the same name in `.travis.yml` and in the Repository Settings, the one in `.travis.yml` takes precedence. If you define a variable as both encrypted and unencrypted, the one defined later in the file takes precedence.
2225
23-
* Variables generally needed for the build to run, that don't contain sensitive data. For instance, a test suite for a Ruby application might require `$RACK_ENV` to be set to `test`.
24-
* Variables that have to differ per branch.
25-
* Variables that have to [differ per job](#Matrix-Variables).
26+
There is also a [complete list of default environment variables](#Default-Environment-Variables) which are present in all Travis CI environments.
2627

27-
Examples for using **repository settings**:
28+
## Defining Variables in .travis.yml
2829

29-
* Variables that have to differ per repository.
30-
* Variables that contain sensitive data, such as third-party credentials.
30+
To define an environment variable in your `.travis.yml`, add the `env` line, for example:
3131

32-
If you define a variable with the same name in both places, the one from the .travis.yml will override the one from the repository settings.
33-
34-
## Variables in the `.travis.yml`
35-
36-
To specify an environment variable in your `.travis.yml`, add the following line:
37-
38-
env: DB=postgres
32+
env:
33+
- DB=postgres
3934

40-
Environment variables are useful for configuring build scripts. See the example in [Database setup](/user/database-setup/#multiple-database-systems).
35+
> Note that environment variable values may need quoting. For example, if they have asterisks (`*`) in them:
36+
> ````
37+
> env:
38+
> PACKAGE_VERSION="1.0.*"
39+
> ````
4140
42-
### Matrix Variables
41+
### Defining Multiple Variables per Item
4342
44-
You can specify more than one environment variable per item in the `env` array:
43+
When you specify multiple variables per item in the `env` array (matrix variables), one build is triggered per item.
4544
4645
rvm:
4746
- 1.9.3
@@ -50,21 +49,17 @@ You can specify more than one environment variable per item in the `env` array:
5049
- FOO=foo BAR=bar
5150
- FOO=bar BAR=foo
5251
53-
With this configuration, **4 individual builds** will be triggered:
52+
this configuration triggers **4 individual builds**:
5453
5554
1. Ruby 1.9.3 with `FOO=foo` and `BAR=bar`
5655
2. Ruby 1.9.3 with `FOO=bar` and `BAR=foo`
57-
3. Rubinius in 1.8 mode with `FOO=foo` and `BAR=bar`
58-
4. Rubinius in 1.8 mode with `FOO=bar` and `BAR=foo`
56+
3. Rubinius latest version (rbx) with `FOO=foo` and `BAR=bar`
57+
4. Rubinius latest version (rbx) with `FOO=bar` and `BAR=foo`
5958
60-
Note that environment variable values may need quoting. For example, if they have asterisks (`*`) in them:
61-
62-
env:
63-
- PACKAGE_VERSION="1.0.*"
6459
6560
### Global Variables
6661
67-
Sometimes you may want to use env variables that are global to the matrix, i.e. they're inserted into each matrix row. That may include keys, tokens, URIs or other data that is needed for every build. In such cases, instead of manually adding such keys to each env line in matrix, you can use `global` and `matrix` keys to differentiate between those two cases. For example:
62+
Sometimes you may want to use environment variables that are global to the matrix, i.e. they're inserted into each matrix row. That may include keys, tokens, URIs or other data that is needed for every build. In such cases, instead of manually adding such keys to each `env` line in matrix, you can use `global` and `matrix` keys to differentiate between those two cases. For example:
6863
6964
env:
7065
global:
@@ -74,65 +69,129 @@ Sometimes you may want to use env variables that are global to the matrix, i.e.
7469
- USE_NETWORK=true
7570
- USE_NETWORK=false
7671
77-
Such configuration will generate matrix with 2 following env rows:
72+
triggers builds with the following `env` rows:
7873
7974
USE_NETWORK=true CAMPFIRE_TOKEN=abc123 TIMEOUT=1000
8075
USE_NETWORK=false CAMPFIRE_TOKEN=abc123 TIMEOUT=1000
8176
82-
## Using Settings
77+
## Defining Variables in Repository Settings
8378
84-
Besides the **.travis.yml**, you can also use the **repository settings** to set an environment variable.
79+
To define variables in Repository Settings, make sure you're logged in, navigate to the repository in question, choose "Settings" from the cog menu, and click on "Add new variable" in the "Environment Variables" section.
8580
8681
<figure>
87-
[ ![Environment Variables in the Repository Settings](/images/settings-env-vars.png) ](/images/settings-env-vars.png)
88-
<figcaption>Environment Variables in the Repository Settings]</figcaption>
82+
![Environment Variables in the Repository Settings](/images/settings-env-vars.png)
83+
<figcaption>Environment Variables in the Repository Settings</figcaption>
8984
</figure>
9085
86+
By default, the value of these new environment variables is hidden from the `export` line in the logs. This corresponds to the behavior of [encrypted variables](#Encrypted-Variables) in your `.travis.yml`.
9187
92-
To do so, make sure you're logged in, navigate to the repository in question, choose "Settings" from the cog menu, and click on "Add new variable" in the "Environment Variables" section.
93-
94-
By default, the value of these new environment variables will be hidden from the `export` line in the logs. This corresponds to the behavior of [encrypted variables](#Secure-Variables) in your .travis.yml.
95-
96-
Similarly, we do not expose these values to untrusted builds, triggered by pull requests from another repository.
88+
Similarly, we do not provide these values to untrusted builds, triggered by pull requests from another repository.
9789
9890
As an alternative to the web interface, you can also use the CLI's [`env`](https://github.com/travis-ci/travis.rb#env) command.
9991
100-
## Secure Variables
92+
## Encrypted Variables
10193
102-
Additionally, variables can be marked "secure", which will encrypt the content and hide their value from the corresponding `export` line in the build. This can be used to expose sensitive data, like API credentials, to the Travis CI build.
94+
Variables can be encrypted so that their content is not shown in the corresponding `export` line in the build. This is used to provide sensitive data, like API credentials, to the Travis CI build. Encrypted variables are not added to untrusted builds such as pull requests coming from another repository.
10395
104-
Secure variables will not be added to untrusted builds (ie, builds for pull requests coming from another repository).
105-
106-
### In the .travis.yml
107-
108-
In the previous `.travis.yml` example, one of the environment variables had a token in it. Since putting private tokens in a file in cleartext isn't always the best idea, you might want to encrypt the environment variable. This allows you to keep parts of the configuration private. A configuration with secure environment variables might look something like this:
96+
A `.travis.yml` file containing encrypted variables looks like this:
10997
11098
env:
11199
global:
112-
- secure: <encrypted string here>
100+
- secure: <long encrypted string here>
113101
- TIMEOUT=1000
114102
matrix:
115103
- USE_NETWORK=true
116104
- USE_NETWORK=false
117105
- secure: <you can also put encrypted vars inside matrix>
118106
119-
You can encrypt environment variables using public key attached to your repository. The simplest way to do that is to use travis gem:
107+
> Encrypted environment variables are not available to pull requests from forks due to the security risk of exposing such information to unknown code.
108+
109+
### Encrypting Variables Using a Public Key
110+
111+
Encrypt environment variables using the public key attached to your repository using the travis gem:
120112
121113
gem install travis
122114
cd my_project
123115
travis encrypt MY_SECRET_ENV=super_secret
124116
125-
Please note that secure env variables are not available for pull requests from forks. This is done due to the security risk of exposing such information in submitted code. Everyone can submit a pull request and if an unencrypted variable is available there, it could be easily displayed.
126-
127-
You can also automatically add it to your `.travis.yml`:
117+
To automatically add the encrypted environment variable to your `.travis.yml`:
128118
129119
travis encrypt MY_SECRET_ENV=super_secret --add env.matrix
130120
131-
To make the usage of secure environment variables easier, we expose info on their availability and info about the type of this build:
132-
133-
* `TRAVIS_SECURE_ENV_VARS` is set to "true" or "false" depending on the availability of environment variables
134-
* `TRAVIS_PULL_REQUEST`: The pull request number if the current job is a pull request, "false" if it's not a pull request.
135-
136-
Please also note that keys used for encryption and decryption are tied to the repository. If you fork a project and add it to travis, it will have different pair of keys than the original.
137-
138-
The encryption scheme is explained in more detail in [Encryption keys](/user/encryption-keys).
121+
> Encryption and decryption keys are tied to the repository. If you fork a project and add it to Travis CI, it will have different keys to the original.
122+
123+
The encryption scheme is explained in more detail in [Encryption keys](/user/encryption-keys).
124+
125+
### Convenience Variables
126+
127+
To make using encrypted environment variables easier, the following environment variables are available:
128+
129+
* `TRAVIS_SECURE_ENV_VARS` "true" or "false" depending on the availability of environment variables
130+
* `TRAVIS_PULL_REQUEST` the pull request number if the current job is a pull request, or "false" if it's not a pull request.
131+
132+
## Default Environment Variables
133+
134+
The following default environment variables are available to all builds.
135+
136+
* `CI=true`
137+
* `TRAVIS=true`
138+
* `CONTINUOUS_INTEGRATION=true`
139+
* `DEBIAN_FRONTEND=noninteractive`
140+
* `HAS_JOSH_K_SEAL_OF_APPROVAL=true`
141+
* `USER=travis` (**do not depend on this value**)
142+
* `HOME=/home/travis` (**do not depend on this value**)
143+
* `LANG=en_US.UTF-8`
144+
* `LC_ALL=en_US.UTF-8`
145+
* `RAILS_ENV=test`
146+
* `RACK_ENV=test`
147+
* `MERB_ENV=test`
148+
* `JRUBY_OPTS="--server -Dcext.enabled=false -Xcompile.invokedynamic=false"`
149+
150+
Additionally, Travis CI sets environment variables you can use in your build, e.g.
151+
to tag the build, or to run post-build deployments.
152+
153+
* `TRAVIS_BRANCH`:For builds not triggered by a pull request this is the
154+
name of the branch currently being built; whereas for builds triggered
155+
by a pull request this is the name of the branch targeted by the pull
156+
request (in many cases this will be `master`).
157+
* `TRAVIS_BUILD_DIR`: The absolute path to the directory where the repository
158+
being built has been copied on the worker.
159+
* `TRAVIS_BUILD_ID`: The id of the current build that Travis CI uses internally.
160+
* `TRAVIS_BUILD_NUMBER`: The number of the current build (for example, "4").
161+
* `TRAVIS_COMMIT`: The commit that the current build is testing.
162+
* `TRAVIS_COMMIT_RANGE`: The range of commits that were included in the push
163+
or pull request.
164+
* `TRAVIS_JOB_ID`: The id of the current job that Travis CI uses internally.
165+
* `TRAVIS_JOB_NUMBER`: The number of the current job (for example, "4.1").
166+
* `TRAVIS_PULL_REQUEST`: The pull request number if the current job is a pull
167+
request, "false" if it's not a pull request.
168+
* `TRAVIS_SECURE_ENV_VARS`: Whether or not encrypted environment vars are being
169+
used. This value is either "true" or "false".
170+
* `TRAVIS_REPO_SLUG`: The slug (in form: `owner_name/repo_name`) of the
171+
repository currently being built. (for example, "travis-ci/travis-build").
172+
* `TRAVIS_OS_NAME`: On multi-OS builds, this value indicates the platform the job is running on.
173+
Values are `linux` and `osx` currently, to be extended in the future.
174+
* `TRAVIS_TAG`: If the current build for a tag, this includes the tag's name.
175+
176+
Language-specific builds expose additional environment variables representing
177+
the current version being used to run the build. Whether or not they're set
178+
depends on the language you're using.
179+
180+
* `TRAVIS_GO_VERSION`
181+
* `TRAVIS_JDK_VERSION`
182+
* `TRAVIS_JULIA_VERSION`
183+
* `TRAVIS_NODE_VERSION`
184+
* `TRAVIS_OTP_RELEASE`
185+
* `TRAVIS_PERL_VERSION`
186+
* `TRAVIS_PHP_VERSION`
187+
* `TRAVIS_PYTHON_VERSION`
188+
* `TRAVIS_RUBY_VERSION`
189+
* `TRAVIS_RUST_VERSION`
190+
* `TRAVIS_SCALA_VERSION`
191+
192+
The following environment variables are available for Objective-C builds.
193+
194+
* `TRAVIS_XCODE_SDK`
195+
* `TRAVIS_XCODE_SCHEME`
196+
* `TRAVIS_XCODE_PROJECT`
197+
* `TRAVIS_XCODE_WORKSPACE`

0 commit comments

Comments
 (0)