UNPKG

detect-environment

Version:

Detect environments in JavaScript similar to the way Laravel does

124 lines (91 loc) 4.29 kB
# Working with travis When you want your environment variables to be present inside your [travis](https://travis-ci.org) builds, but not be present in your repositories history, you can leverage travis's own [environment variables](http://docs.travis-ci.com/user/environment-variables/#Using-Settings). ## Configuring travis To add variables to your build navigate to your repo on travis and then head to the `/settings/env_vars` page. The url should be somthing like: > https://travis-ci.org/<username>/<reponame>/settings/env_vars You will need to do this for every value in your `.env` files. Next you will need to add a new variable for each variable in your `.env` file. Follow the steps below to add new variables. 1. Click `Add a new variable`. 2. Set `Name` and `Value`. 3. Optionall check `Display value in build logs`. By checking `Display value in build logs` a log will appear in the travss build like so: ```sh $ export TRAVIS_FOO=TRAVIS_BAR ``` Otherwise you will see: ```sh $ export TRAVIS_FOO=[secure] ``` Either way the value is set all the same. ## Working locally Given the following `.env` file, ```yml # .env.testing.yml --- key: "12345678123456781234567812345678" secret: "abcdefghabcdefghabcdefghabcdefgh" ``` To reproduce the variables within the travis environment, add two new environment variables to travis by navigating the the example url below. > https://travis-ci.org/<username>/<reponame>/settings/env_vars In accordance with the `.env.testing.yml` file we need to add two variables to travis. These variables should be namespaced so as to not run into any collisions with other variables. Example input is provided below | Name | Value | | :------------ | :------------------------------- | | TRAVIS_KEY | 12345678123456781234567812345678 | | TRAVIS_SECRET | abcdefghabcdefghabcdefghabcdefgh | Because the JavaScript environment that travis is run is just an ordinary `node` environment, we can use nodes build in [`process.env`](http://nodejs.org/api/process.html#process_process_env) variable to gain access to the above defined environment variables. Consideer the following code snippet. ```js console.log(process.env.TRAVIS_KEY); // 12345678123456781234567812345678 console.log(process.env.TRAVIS_SECRET); // abcdefghabcdefghabcdefghabcdefgh ``` Because the vm that travis runs its tests on is just an ordinary linux environment, we can exploit that fact and test to see if certain directories exist. The `$HOME` directory exposed by travis is `/home/travis`. We can us the node [`fs`](http://nodejs.org/api/fs.html) module to test to see if this directory exists[[1]](#travis-warning), and if it does we can have a reasonable level of confidence that we are running from within the travis environment. ```js // gulpfile.js // Pull in any system pacakges. var fs = require('fs'); // Pull in `gulp` and its plugins. var gulp = require('gulp'); var gutil = require('gulp-util'); // Pull in the `detect-environment` plugin. var de = require('detect-environment'); // Determine our environment name and fetch its data. var env = de(function (envName, envData) { if (fs.existsSync('/home/travis')) { gutil.log('Travis detected. Setting environment to "testing"'); envData.key = process.env.TRAVIS_KEY; envData.secret = process.env.TRAVIS_SECRET; return 'testing'; } return envName; }); ``` In the `de()` callback we check to see if we are running from within the travis vm by checking to see if the `/home/travis` directory exists. If it is found to exist we attach the required variables to the `envData` Object. Modifying the `envData` Object works the same way that modifying the `exports` variable inside a node package works. These additions to the `envData` Object will override any values present in a local `.env` file. But since no `.env` file will exist on the travis box this is a moot point. --- ###### <sub>Travis warning</sub> <sub>The travis docs advise against using this as a form of detection due to the possibility of the directory changing at any point of time. This is however very very unlikely to be changed. Check their [docs](http://docs.travis-ci.com/user/ci-environment/) form more info.</sub>