简体   繁体   中英

How do I access environment variables in Travis CI

I have built a site using SQL Server and Node.js and I use Mocha and Chai for my tests. It all works fine locally, and any tests that don't require accessing the database run correctly on Travis CI, but because I have my password, username, db path, etc. stored in my .env file, which is gitignored Travis can't access the DB for testing.

I tried to set the environment variables according to these instructions from the Travis docs , but the log in fails. I know that the environment variables are being found, because the error message is: Unhandled rejection SequelizeConnectionError: Login failed for user 'EventAdmin'. and 'EventAdmin' is the username from the environment variables, but for some reason the password isn't being accepted. I know that the password is right, because I copied it straight from my .env file.

My .travis.yml file looks like this:

 language: node_js node_js: - "4.1" - "4.0" - "0.12" - "0.11" - "0.10" - "iojs" before_install: - npm install -g grunt-cli script: grunt test 

My test (which works locally) looks like this:

 'use strict'; var chai = require('chai'); var expect = chai.expect; var assert = chai.assert; var chaihttp = require('chai-http'); chai.use(chaihttp); require('../server.js'); describe('Test /showfullteam route', function() { it('should load all MS contacts from /showfullteam', function(done) { chai.request('localhost:3000') .get('/showfullteam') .end(function(err, res) { expect(err).to.eql(null); for (var i = 0; i < res.body.length; i++) { expect(res.body[i]).to.include.keys('firstName', 'lastName', 'email', 'newsletterSubscription', 'contactDescription', 'msTeamMember', 'msTeamTitle', 'showOnHomePage', 'headShot', 'company', 'address', 'country', 'interestId', 'allowNotifications', 'allowPersonalInfoSharing'); expect(res.body[i].msTeamMember).to.eql(true); expect(typeof res.body[i].firstName).to.be.a('string') || expect(typeof res.body[i].firstName).to.be.a('null'); expect(typeof res.body[i].lastName).to.be.a('string') || expect(typeof res.body[i].firstName).to.be.a('null'); expect(typeof res.body[i].email).to.be.a('string') || expect(typeof res.body[i].firstName).to.be.a('null'); if (res.body[i].newsletterSubscription) { assert.typeOf(res.body[i].newsletterSubscription, 'boolean'); } if (res.body[i].msTeamMember) { assert.typeOf(res.body[i].msTeamMember, 'boolean'); } if (res.body[i].showOnHomePage) { assert.typeOf(res.body[i].showOnHomePage, 'boolean'); } if (res.body[i].allowNotifications) { assert.typeOf(res.body[i].allowNotifications, 'boolean'); } if (res.body[i].interestId) { assert.isNumber(res.body[i].interestId); } expect(typeof res.body[i].contactDescription).to.be.a('string') || expect(typeof res.body[i].firstName).to.be.a('null'); expect(typeof res.body[i].headShot).to.be.a('string') || expect(typeof res.body[i].firstName).to.be.a('null'); expect(typeof res.body[i].company).to.be.a('string') || expect(typeof res.body[i].firstName).to.be.a('null'); expect(typeof res.body[i].address).to.be.a('string') || expect(typeof res.body[i].firstName).to.be.a('null'); expect(typeof res.body[i].country).to.be.a('string') || expect(typeof res.body[i].firstName).to.be.a('null'); expect(typeof res.body[i].allowPersonalInfoSharing).to.be.a('string') || expect(typeof res.body[i].firstName).to.be.a('null'); }; done(); }); }); }); 

You can see the full project on my GitHub repo and the failing tests on Travis CI

Please let me know if you need more info. Thanks in advance for all the help!

I figured out how to set the environment variables, but that wasn't the problem. The issue was that SQL Server is not compatible with travis. I switched to mariadb and the tests pass :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM