geeklearningio / gl-vsts-tasks-yarn

Yarn Package Manager Visual Studio Team Services Build and Release Management extensions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Environment Variables not being passed to Node.js

Sawtaytoes opened this issue · comments

Before opening an issue please read this.

Are you reporting a bug or a feature request ?

  • Bug
  • Feature request

Description

When adding environment variables to a build pipeline, they don't show up in Node.js when running a yarn command.

Expected behavior

I should see my environment variables in process.env.

Reproduction

Run a command that looks at process.env.NODE_ENV and set that up in the build pipeline as an environment variable.

MANDATORY FOR BUGS: Insert debug trace

In order to allow us to quickly reproduce you issue please include a link to a reproduction repository

System information

VSTS or TFS:

  • VSTS
  • TFS

Agent:

  • Hosted
  • Custom

OS:

  • Windows
  • Mac
  • Linux

Yarn version:
1.10.1

Node version:
8.12.0

There's this misconception that VSTS variables are environement variable but they are not.
Btw, duplicate of #40 if you want more context.

Given your comment on the previous issue, I realize you might be using Yaml builds in which case you need to map the environement variable on the task itself see env on the yaml task documentation:
https://docs.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=vsts#task

Thanks!

Actually, their docs told me those secret vars could be used as env vars:

You can use agent variables as environment variabes in your scripts and as parameters in your build tasks. You cannot use them to customize the build number or to apply a version control label or tag.
https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=vsts

I'd completely forgotten, but when I do builds in Docker containers, I pass them in as args too, but I can still define the environment as well using Docker's yml files. As far as I know, stuff like NODE_ENV=production needs to be set before running right?

Having my app accept env vars in command line arguments won't fix Webpack building all my assets in development mode.

Is it not possible to add an "environment variables" section to the yarn task? Using Docker, I have them come in there and then grab them in my Dockerfile. All the other scripting tasks also accept Environment Variables in a separate section so that's how I'd do it with those. My goal going direct through Azure DevOps is to avoid using Docker for builds.

I ended up using a command line task with Environment Variables:

export NODE_ENV=production
yarn compile

Would you mind providing me the yaml of your build? it's hard to understand the problem without it.

Here is an example of usage or environments variables via YAML:

- task: geeklearningio.gl-vsts-tasks-yarn-dev.yarn-task.Yarn@2
  displayName: 'No custom package no npmrc'
  env:
    SOMEVAR: '$(TestVar)'
  inputs:
    ProjectDirectory: .

    customRegistry: useFeed

    customFeed: 'ead7b14e-a830-4f35-9087-f87c338a6bfe'

    Arguments: test

Package.json

{
  "name": "sample",
  "version": "1.0.0",
  "private": true,
  "devDependencies": {
  },
  "scripts": {
    "test" : "test.cmd"
  },
  "dependencies": {
    "fs-extra": "0.30.0",
    "micromatch": "2.3.11",
    "semver": "5.3.0",
    "vsts-task-lib": "0.9.6"
  }
}

test.cmd

echo %SOMEVAR%