TravisCI: ENVIRONMENT VARIABLES
Travis CI is a hosted, distributed continuous integration service used to build and test software projects hosted at GitHub. It is basically a tool that helps you deploy software with confidence. It is like a middleman that protects you from breaking your code.
TravisCI easily integrates with popular Travis CI cloud repositories such as Bitbucket and GitHub. Without the need for a dedicated server, they offer many automated CI options that are hosted in the cloud. On different computers with different operating systems, this allows for testing in different environments.
In any programming language we usually see two majorly used components known as variables and constants. Just like variables in any mathematical equation, these take consist of values that change the results of the program.
Variables and constants both represent unique memory locations containing data the program uses in its calculations.
An environment variable is a variable whose value is set outside the actual program or build template file, typically through functionality built into the operating system or microservice and which can be used by multiple components like operating system, any microservice running or any program running on that machine. It is a key/value pair, and can be created multiple times using different keys.
As this is a key/value pair the keys needs be unique while creating multiple variables. The key is used to access the value of that variable anywhere required.
Now let us understand how these environment variable are used in TravisCI.
Environment Variable in TravisCI:
A common way to customize the build process is to define environment variables, which can be accessed from any stage in your build process.
There are multiple ways to define these environment variables as below. We can use any way to create these variables based on our requirement:
- For the information which is not sensitive - add it to .travis.yml
- For the information which is not sensitive, but it is same for all branches – encrypt it and add it to .travis.yml
- For the sensitive information, and might be different for different branches – add it to Repository Settings
Let's go ahead and see how we can create environment variable using above mentioned ways one by one
Environment Variable in .travis.yml
Public variables defined in code are tied to a commit. If you want to change the value which is not sensitive then you have to clone the code and change and then restart the build. Instaed of that you can set those variables in .tavis.yml file. So you don’t need to check out the code just for small changes like these.
if in case you need to change a minor version of any package then it requires a new commit, restarting an old build uses the old values. Then you can just define variables in .travis.yml that are needed for the build to run and does not contain sensitive data and also the values are different for every branch and for every build then all you have to do is to use an asterisks (*) symbol as below.
.travis.yml
|
env: - HOST=127.0.0.1 - PORT=1234 - PACKAGE_VERSION=”1.2.*”
|
Encrypted environment Variable in .travis.yml
There are some scenarios where you need to add sensitive data as an environment variable such as tokens, API keys, etc. In this case one should be very responsible that the data should not be easily visible to anyone.
.travis.yml
|
env: global: - secure: "OrEeqU0z6GJdC6Sx/\newLDTdtbk/dxKurUzwTeRbplIEe9DiyVDCzEiJGfgfq7woh+="
|
Above is the example of how we can add encrypted environment variable in .travis.yml file. We can use travis gem for the value encryption
Encrypting environment variables
By using travis gem we can encrypt environment variables with the public key attached to the repository:
1. Install travis gem by using gem install travis command
2. After installation run below command in the repositor
o travis encrypt TEMP_SECRET_ENV=super_secret --add env.global
3. Add the secret variable in the .travis.yml file as shown above and commit the changes to the repository.
Environment Variable in Repository Settings
Variables defined in the repository settings are same for all the builds triggered within that repository. We can use this way of variable creation if there is a sensitive data like API keys and are only restricted to be used within the same repository.
To define variables in Repository Settings, navigate to the repository, choose “Settings” from the “More options” menu, and click on “Add new variable” in the “Environment Variables” section. Restrict the environment variable to a specific branch by selecting which branch it should be available to or we can keep it for all branches as well.
Summary
In this article, we have learned about Environment variables and ways to create those within Travis CI. Also we have learnt that how we can encrypt the sensitive data using travis gem and assign it to the variable
This is the third blog in the series of the blogs that will be published for TravisCI. More blogs will be published over next few months. If you have any further queries on TravisCI, do feel free to email ashish.sonawane@ibm.com
Author Details: Ashish Sonawane (ashish.sonawane@ibm.com)
Reviewer Details: Boudhayan Chakrabarty (bochakra@in.ibm.com)