DEV Community

Bart Wijnants
Bart Wijnants

Posted on • Originally published at hackernoon.com on

2

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

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 dev
firebase login:ci
travis encrypt "1/xxx"

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

deploy:
  - provider: firebase
    project: dev
    token:
      secure: "encrypted stuff"
    skip_cleanup: true
    on:
      branch: "master"
  - provider: firebase
    project: prod
    token:
      secure: "encrypted stuff"
    skip_cleanup: true
    on:
      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 patch
git 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.


SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️