Skip to content

environment set to undefinded in config/environment.js when running ember deploy #474

Description

@loqtor

Hi guys,

I'm not sure if this issue should be raised here (or even if it isn't something I am doing wrong), but I'd rather share what's going on.

Basically, when running ember deploy [deployment_target] there are two things that called my attention:

1 - It seems to go through config/environment.js several times.
2 - The first time it runs environment is undefined.

This causes the configuration for endpoints to be the default one for any deployment_target
, making my application in other environment than develop not to work properly.

I'm currently deploying to S3 using ember-cli-deploy-s3.

Am I missing something here? Please, bear in mind this is the first time I'm using the addon, so let me know in case I'm way off here. :)

Here's my package.json:

{
  "name": "branz-artizan-web",
  "version": "0.0.4",
  "description": "branz-artizan-web web application",
  "license": "MIT",
  "author": "",
  "directories": {
    "doc": "doc",
    "test": "tests"
  },
  "repository": "",
  "scripts": {
    "postinstall": "bower install",
    "build": "ember build",
    "start": "ember server",
    "test": "ember test",
    "lint": "eslint app/ tests/",
    "lint:fix": "eslint --fix app/ tests"
  },
  "devDependencies": {
    "babel-plugin-transform-builtin-extend": "^1.1.2",
    "broccoli-asset-rev": "^2.4.5",
    "ember-awesome-macros": "^0.35.0",
    "ember-cli": "2.13.0",
    "ember-cli-app-version": "^3.0.0",
    "ember-cli-autoprefixer": "^0.7.0",
    "ember-cli-babel": "^6.0.0",
    "ember-cli-dependency-checker": "^1.3.0",
    "ember-cli-deploy": "^1.0.0",
    "ember-cli-deploy-build": "^1.1.0",
    "ember-cli-deploy-display-revisions": "^1.0.0",
    "ember-cli-deploy-git-tag": "^0.2.1",
    "ember-cli-deploy-gzip": "^1.0.0",
    "ember-cli-deploy-manifest": "^1.0.0",
    "ember-cli-deploy-revision-data": "^1.0.0",
    "ember-cli-deploy-s3": "^1.1.0",
    "ember-cli-deploy-s3-index": "^1.0.0",
    "ember-cli-document-title": "^0.3.3",
    "ember-cli-eslint": "^4.0.0",
    "ember-cli-htmlbars": "^1.1.1",
    "ember-cli-htmlbars-inline-precompile": "^0.4.0",
    "ember-cli-inject-live-reload": "^1.4.1",
    "ember-cli-mirage": "^0.3.3",
    "ember-cli-moment-shim": "^3.1.0",
    "ember-cli-qunit": "^4.0.0-beta.1",
    "ember-cli-sass": "^6.1.3",
    "ember-cli-shims": "^1.1.0",
    "ember-cli-uglify": "^1.2.0",
    "ember-concurrency": "^0.8.6",
    "ember-concurrency-test-waiter": "^0.2.0",
    "ember-cp-validations": "^3.3.1",
    "ember-data": "2.12.2",
    "ember-do-forms": "^1.8.1",
    "ember-drag-drop": "^0.4.4",
    "ember-export-application-global": "^2.0.0",
    "ember-file-upload": "^2.0.0-beta.41",
    "ember-get-config": "^0.2.1",
    "ember-href-to": "^1.12.0",
    "ember-load-initializers": "^1.0.0",
    "ember-lodash": "^4.17.2",
    "ember-macro-helpers": "^0.14.4",
    "ember-moment": "^7.3.0",
    "ember-native-dom-helpers": "^0.4.1",
    "ember-one-way-controls": "^2.0.0",
    "ember-resolver": "^4.0.0",
    "ember-route-action-helper": "^2.0.3",
    "ember-source": "~2.13.0",
    "ember-test-selectors": "^0.3.4",
    "ember-truth-helpers": "^1.3.0",
    "eslint": "^4.1.1",
    "eslint-plugin-ember-suave": "^1.0.0",
    "loader.js": "^4.2.3"
  },
  "engines": {
    "node": ">= 4"
  },
  "private": true,
  "dependencies": {
    "ember-storage": "^1.1.2",
    "ember-transition-helper": "^0.0.6"
  }
}

Here's my environment.js (note the early return at the beginning):

/* eslint-env node */
let path       = require('path');
let getRepoInfo = require('git-repo-info');

let awsRegion = 'REGION'; // default global region.

const DEFAULT_ENDPOINT = 'https://some-url.com';
const DEMO_ENDPOINT = 'https://some-other-url.com';
const DEFAULT_STAGE = 'dev';
const DEMO_STAGE = 'demo';

/*
 * TODO: Update this when the production environments are created
 */
const ENDPOINTS = {
  develop: DEFAULT_ENDPOINT,
  demo: DEMO_ENDPOINT,
  test: DEFAULT_ENDPOINT,
  production: DEMO_ENDPOINT,
  // production: DEFAULT_ENDPOINT,
};

const STAGES = {
  develop: DEFAULT_STAGE,
  demo: DEMO_STAGE,
  test: DEFAULT_STAGE,
  production: DEMO_STAGE,
  // production: DEFAULT_STAGE
};

module.exports = function(environment) {
  let deployEnvironment = process.env.DEPLOY_TARGET || environment;

  /*
   * When running `ember deploy` the first time this file is ran, the environment
   * is undefined, which was causing the endpoints to always be default regardless
   * of the deployment target. This seems to solve it.
   *
   */
  if (!deployEnvironment) {
    return;
  }

  let version = getVersion(environment);
  let endpoint = ENDPOINTS[deployEnvironment] || DEFAULT_ENDPOINT;
  let stage = STAGES[deployEnvironment] || DEFAULT_STAGE;

  let ENV = {
    modulePrefix: 'PREFIX',
    environment,
    rootURL: '/',
    locationType: 'auto',
    EmberENV: {
      FEATURES: {
        // Here you can enable experimental features on an ember canary build
        // e.g. 'with-controller': true
      },
      EXTEND_PROTOTYPES: {
        // Prevent Ember Data from overriding Date.parse.
        Date: false,
      },
    },

    moment: {
      includeTimezone: 'all',
    },

    apiDateFormat: 'YYYY-MM-DDTHH:mm:ss.SSS\\Z',
    timezoneString: 'Pacific/Auckland',

    'ember-do-forms': {
      // Auto generate relevant data-test-* for components
      // Overridable per component
      autoDataTestSelectors: true,

      // CSS classes to be applied to components
      // Overridable per component
      defaultClasses: {
        form: ['Form'],
        field: ['Form-field'],
        label: ['Form-field-label'],
        control: ['Form-field-control'],
        feedback: ['Alert Alert--error'],
        hint: ['Form-field-hint'],
      },

      // CSS classes to be applied to do-field and do-control
      // components based on the validation state of the object
      validationClasses: {
        fieldSuccess: [],
        fieldError: ['has-error'],
        controlSuccess: [],
        controlError: [],
      },
    },

    aws: {
      region: awsRegion,
      api: {
        region: awsRegion,
        endpoint,
        stage,
      },
      s3: {
        region: awsRegion,
        bucketName: 'BUCKETNAME',
        unauthedBucketName: 'UNATHEDBUCKETNAME',
      },
      cognito: {
        userPool: {
          region: awsRegion,
          userPoolId: 'USERPOOLID',
          clientId: 'CLIENTID',
        },

        identityPool: {
          region: awsRegion,
          identityPoolId: 'POOLID',
          logins: {
            cognito: 'COGNITOLOGIN',
          },
        },
      },
    },

    APP: {
      name: 'APPNAME', // displayed in Ember inspector
      version: version.display,
      deployVersion: version.withCommit,
      // Here you can pass flags/options to your application instance
      // when it is created
    },
  };

  if (environment === 'development') {
    // ENV.APP.LOG_RESOLVER = true;
    // ENV.APP.LOG_ACTIVE_GENERATION = true;
    // ENV.APP.LOG_TRANSITIONS = true;
    // ENV.APP.LOG_TRANSITIONS_INTERNAL = true;
    // ENV.APP.LOG_VIEW_LOOKUPS = true;
  }

  if (environment === 'test') {
    // Testem prefers this...
    ENV.locationType = 'none';

    // keep test console output quieter
    ENV.APP.LOG_ACTIVE_GENERATION = false;
    ENV.APP.LOG_VIEW_LOOKUPS = false;

    ENV.APP.rootElement = '#ember-testing';
  }

  if (environment === 'production') {
    ENV['ember-cli-mirage'] = {
      enabled: true,
    };
  }

  ENV.contentSecurityPolicy = {
    'default-src': "'none'",
    'script-src': "'self' 'unsafe-eval' http://example.com:35729 https://connect.facebook.net http://connect.facebook.net https://graph.facebook.com",
    'font-src': "'self'",
    'connect-src': "'self' ws://example.com:35729",
    'img-src': "'self' https://www.facebook.com",
    'style-src': "'self' 'unsafe-inline'",
    'media-src': "'self'",
    'frame-src': 'http://static.ak.facebook.com http://staticxx.facebook.com https://s-static.ak.facebook.com https://www.facebook.com http://www.facebook.com',
  };

  return ENV;
};

// Modified version of https://github.com/cibernox/git-repo-version/blob/master/index.js
function getVersion(environment) {
  let info = getRepoInfo();
  let shaLength = 8;

  let packageVersion  = require(path.join(process.cwd(), 'package.json')).version;
  let sha = info.sha || '';
  let prefix;

  if (packageVersion != null) {
    prefix = packageVersion;
  } else if (info.branch) {
    prefix = info.branch;
  } else {
    prefix = 'DETACHED_HEAD';
  }

  let suffix = environment === 'production' ? '' : `<${environment}>`;
  let withCommit = `${prefix}+${sha.slice(0, shaLength) + suffix}`;
  let withTag = info.tag ? `${prefix}#${info.tag + suffix}` : null;

  return {
    display: packageVersion + suffix,
    withCommit,
    withTag,
  };
}

And here's my deploy.js (as said, I'm deploying to S3):

/* jshint node: true */

// All targets that are available for deployment
var deployTargetConfig = {
  'develop': {
    code: 'SOME-CODE',
    region: 'SOME-REGION',
    bucket: 'SOME-BUCKET',

    // For S3 Index Addon. (Note that you shouldn't really be overwritting
    // revisions, as a revision represents a build in time. Thats why we
    // only allow it for develop, to avoid having to commit).
    allowOverwrite: true
  },
  'demo': {
    code: 'SOME-CODEE',
    region: 'SOME-REGION',
    bucket: 'SOME-BUCKET'
  },
  'test': {
    code: 'SOME-CODE',
    region: 'SOME-REGION',
    bucket: 'SOME-BUCKET',
  },
  'production': {
    code: 'SOME-CODE',
    region: 'SOME-REGION',
    bucket: 'SOME-BUCKET',
  },
};

module.exports = function(deployTarget) {
  var ENV = {

    pipeline: {
      alias: {
        s3: { as: ['s3-all', 's3-apple-app-site-association'] },
      },

      runOrder: {
        's3-all': { before: 's3-apple-app-site-association' },
        's3-apple-app-site-association': { after: 's3-all' }
      }
    },

    build: {
      environment: 'production'
    },
    'revision-data': {
      type: 'version-commit'
    },
    'git-tag': {
      deployTag: function(context) {
        var revisionKey  = this.readConfig("revisionKey");
        var deployTarget = context.config.deployTargetCode;

        return ['versions', deployTarget, revisionKey].join('/');
      }
    },
    deployTargetCode: deployTargetConfig[deployTarget].code
    // include other plugin configuration that applies to all deploy targets here
  };

  var credsConfig = {
    accessKeyId: process.env.AWS_KEY,
    secretAccessKey: process.env.AWS_SECRET
  };

  var deploymentTargetConfig = deployTargetConfig[deployTarget];
  var baseConfig = Object.assign({}, deploymentTargetConfig, credsConfig);

  ENV['s3-all'] = Object.assign({}, baseConfig);

  ENV['s3-apple-app-site-association'] = Object.assign({
    filePattern: '**/apple-app-site-association',
    defaultMimeType: 'application/json',
    cacheControl: 'no-cache, no-store, must-revalidate',
    expires: 0
  }, baseConfig);

  ENV['s3-index'] = Object.assign({}, baseConfig);

  return ENV;
};

As per the environment.js I seemed to find a workaround for now (I can deploy properly with the early return I added), but I'm just curious to know if I should be doing something different.

Any comments would be greatly appreciated.

Cheers!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions