microsoft / vscode-remote-release

Visual Studio Code Remote Development: Open any folder in WSL, in a Docker container, or on a remote machine using SSH and take advantage of VS Code's full feature set.
https://aka.ms/vscode-remote
Other
3.66k stars 286 forks source link

Read build args from env file i.e. private npm registry #5475

Open fyyyyy opened 3 years ago

fyyyyy commented 3 years ago

❓ It would be nice if VScode devcontainer.json supports reading build args from a file. So we won't need this workaround:

I have found a workaround to set ENV vars from a file during the docker BUILD. This is i.e. necessary if you want to use a private NPM registry during build, but you don't want to write the secrets in devcontainer.json or Dockerfile, as these files are committed to the repo.

.devcontainer/devcontainer.env ( add this file to .gitignore )

export NPM_USER=***
export NPM_PASS=***
export NPM_EMAIL=***

In the Dockerfile:

# Install npm-cli-adduser which can perform a cli npm login by reading env vars
RUN npm i -g --production --registry https://registry.npmjs.org/ npm-cli-adduser

# Set env variables from devcontainer.env. Needs to be in same RUN step as env vars will be lost in next RUN
COPY devcontainer.env devcontainer.env
RUN $(cat devcontainer.env) && npm-cli-adduser -r https://private.registry.url \
&& npm whoami

# install private registry package
RUN npm i -g -r https://private.registry.url private-registry-package

EDIT: Another option to solve this would be to add a prebuild: property / script to devcontainer.json analog to postbuild:. This could be even more flexible as users can copy files to the context, read envs via shell scripts and many more operations that are usually possible by calling docker build manually.

chrmarti commented 3 years ago

Another option would be to pass the values as local variables with the devcontainer.json:

{
  "build": {
    "dockerfile": "Dockerfile",
    "args": {
      "NPM_USER": "${localEnv:NPM_USER}"
    }
  }
}
fyyyyy commented 3 years ago

Another option would be to pass the values as local variables with the devcontainer.json: "NPM_USER": "${localEnv:NPM_USER}"

This only seems to work when launching VScode via commandline, not via app icon.

Also we would prefer to not pollute the OS env with project specific NPM credentials

fyyyyy commented 3 years ago

Another option to solve this would be to add a prebuild: script to devcontainer.json This would probably be more flexible as we can copy files to the context, read envs via shell scripts etc.

dlangerm commented 2 years ago

Currently waiting on this feature, any updates?

bence42 commented 2 years ago

+1 would be very handy.

ath commented 1 week ago

Another option would be to pass the values as local variables with the devcontainer.json:

{ "build": { "dockerfile": "Dockerfile", "args": { "NPM_USER": "${localEnv:NPM_USER}" } } }

This can work in some cases. It would pollute the dev machine with a user-global env var that exists all the time while it is only required when someone works on this specific project.

But also: we might be working in multiple devcontainers at the same time that all refer to the same env var name, simply because it is a meaningful quality name, but in each instance the env var needs to have a different value.