简体   繁体   中英

How to make staging environment for Ember-CLI?

There are development, test and production environments in Ember CLI . I have test and production server. And I need to make build on test server like on production but with another environment configs. But test env in Ember CLI used for automatic tests.

I tried to call ember build --environment development on test server with next options in ember-cli-build.js file:

var app = new EmberApp(defaults, {
   // Add options here
   fingerprint: {
     prepend: 'https://s-test.mycdn.com/',
     enabled: true
   }
});

But I got error:

The Broccoli Plugin: [AssetRewrite] failed with:
TypeError: Cannot read property '2' of null
.....
The broccoli plugin was instantiated at:
.....

What is right way to build ember application on test server?

This might be confusing, but what you want is not environments. You want deploy targets . Here's a great blog post about difference between the two: Do not confuse environment for deploy target (that URL has been trashed in a reorg/takeover of deveo.com but a snapshot exists on archive.org ).

In Ember, environments are about how your code is minified, your assets are fingerprinted, enabling/disabling certain debugging features, etc. They are not about where you want to deploy your code, which API URL you want to use, or anything like that.

You might happen to have servers called test or development , but when deploying to these servers, you should always set environment to production , not test or development .

In order to support multiple servers (deploy targets), I would use env vars to do that. Something like:

DEPLOY_TARGET=development ember build --environment=production
DEPLOY_TARGET=test ember build --environment=production
DEPLOY_TARGET=staging ember build --environment=production
DEPLOY_TARGET=production ember build --environment=production

And in your ember-cli-deploy.js , you simply access the value through process.env.DEPLOY_TARGET , like this:

const deployTarget = process.env.DEPLOY_TARGET;
if (deployTarget === 'development') {
  // ...
} else if (deployTarget === 'test') {
  // ...
} else if (deployTarget === 'staging') {
  // ...
} else if (deployTarget === 'production') {
  // ...
}

I recommend using ember-cli-deploy addon to automate this process, so that you could just type ember deploy staging to create a build for staging and deploy it.

Unfortunately, support of environmements in ember-cli is poor. You have only development and production and can't add new ones. On top of that, production build does additional things like minifying, fingerprinting, etc, which are not necessarily needed on test/staging server and time consuming.

The good news is we have an access to Node API inside environment.js. In fact, environment.js is a node module. This allows us to add additional parameters to ember build and parse them. I'm successfuly using this trick in ember-cli 2.7 and used in 2.8 too. You will need to:

  1. Install minimist : npm install minimist --save-dev
  2. Add this code to the beginning of environment.js:

     var argv = require('minimist')(process.argv.slice(2)); console.log('Sub-environment is set to ' + argv.subenv);
  3. Use argv.subenv in environment.js to recognize different "sub-environments". I suggest to do this after "environment" blocks:

     if (argv.subenv === 'mirage') { ENV['ember-cli-mirage'] = { enabled: true }; ENV.API.namespace = ''; ENV.API.host = ''; } if (argv.subenv === 'staging') { ENV['ember-cli-mirage'] = { enabled: false }; ENV.API.host = 'https://your-server.com/'; ENV.API.namespace = ''; }

    You can combine environment and argv.subenv in your code as you need.

  4. Use your argument when building: ember build --environment=development --subenv=staging . Ember-cli will output a warning about non-supported arguments, but everything will work just fine.

Using this method, you can have as many servers as you need and build development/production version of app on any of them.

As for your error, I'm not sure what causing it, probably you may find something here

Edit your config/environment.js to set your environment when the param equals staging . For example:

``` function(environment) {

var ENV = {
    // SET ENV properties
    APP: {
        // Here you can pass flags/options to your application instance
        // when it is created
    },
    contentSecurityPolicy: {
        // Configure security
    }
};

if (environment === 'production') {
    // Set production values
}

if (environment === 'staging') {
    // Set staging values
}

if (environment === 'development') {
    // Set staging values
}

if (environment === 'test') {
    // Set testing values
}

return ENV;

};

```

And then when trying to build the version use the following command:

ember build --environment="staging"

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