paint-brush
Continuous deployment of a webpack app to multiple environments using Travic CIby@bartwijnants
1,653 reads
1,653 reads

Continuous deployment of a webpack app to multiple environments using Travic CI

by Bart WijnantsAugust 1st, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

I set up continuous deployment of a <a href="https://webpack.github.io/" target="_blank">webpack</a> app with tests in <a href="https://facebook.github.io/jest/" target="_blank">Jest</a> hosted on <a href="https://firebase.google.com/" target="_blank">Firebase</a> using <a href="https://github.com/" target="_blank">GitHub</a> and <a href="https://travis-ci.org/" target="_blank">Travis CI</a>. Deploys to the DEV environment are done on every commit to master. Deploys to the <a href="https://hackernoon.com/tagged/prod" target="_blank">PROD</a> environment are done on every tag to master.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Continuous deployment of a webpack app to multiple environments using Travic CI
Bart Wijnants HackerNoon profile picture

TL;DR

I set up continuous deployment of a webpack app with tests in Jest hosted on Firebase using GitHub and Travis CI. Deploys to the DEV environment are done on every commit to master. Deploys to the PROD environment are done on every tag to master.

Check out the code here.

How to?

In code I use a default value:

var text = __CONFIG__ || “default”;

That value can be overridden in a webpack config file:





plugins: [new webpack.DefinePlugin({__CONFIG__: JSON.stringify("override")})]

In my .firebaserc file I defined multiple Firebase projects:






{"projects": {"prod": "prod-multi-webpack-travis","dev": "dev-multi-webpack-travis"}}

This allows me to setup ci for each project by executing the following commands:



firebase use devfirebase login:citravis encrypt "1/xxx"

In the .travis.yml file I defined multiple deploys:

deploy:







  • provider: firebaseproject: devtoken:secure: "encrypted stuff"skip_cleanup: trueon:branch: "master"







  • provider: firebaseproject: prodtoken:secure: "encrypted stuff"skip_cleanup: trueon:tags: true

Now I can also use the environment variables that Travis defines in my webpack config file:







plugins: [new webpack.DefinePlugin({__CONFIG__: JSON.stringify(process.env.TRAVIS_TAG ? "override on tag" : "override")})]

If I do a push to GitHub the dev deploy is triggered and if I update the version then the dev and prod deploy are triggered:


npm version patchgit push --follow-tags

To get Jest tests working on files where the environment files are used I added some Jest configuration to my package.json file:





"jest": {"globals": {"__CONFIG__": null}}

Conclusion

It took me way too long to get this working so I decided to write this blog post. This setup makes it very easy to have different configurations for each environment.

A downside is that the code needs to be bundled again every time you change environment.