2

I have a few Docker builds on Azure DevOps for React apps which include packages from a private npm feed also hosted on Azure DevOps. Recently the builds have started failing at the npm install command.

In order to authenticate the container to install from the private feed I've always used an .npmrc file. This is saved locally as .npmrc.docker and looks like this:

@<package-scope>:registry=https://<devops-username>.pkgs.visualstudio.com/_packaging/<feed-name>/npm/registry/
//<devops-username>.pkgs.visualstudio.com/_packaging/<feed-name>/npm/registry/:username=<feed-name>
//<devops-username>.pkgs.visualstudio.com/_packaging/<feed-name>/npm/registry/:_password=${NPM_TOKEN}
//<devops-username>.pkgs.visualstudio.com/_packaging/<feed-name>/npm/registry/:email=npm requires email to be set but doesn't use the value
//<devops-username>.pkgs.visualstudio.com/_packaging/<feed-name>/npm/:username=<feed-name>
//<devops-username>.pkgs.visualstudio.com/_packaging/<feed-name>/npm/:_password=${NPM_TOKEN}
//<devops-username>.pkgs.visualstudio.com/_packaging/<feed-name>/npm/:email=npm requires email to be set but doesn't use the value

I define a scoped package source at the top and the rest is generated from Azure DevOps via its Connect to feed wizard. ${NPM_TOKEN} is my feed password which I pass into the docker build command as a build argument.

The part of my Dockerfile which uses this looks like this:

FROM node:alpine as build
ARG NPM_TOKEN
COPY ./.npmrc.docker /app/.npmrc
COPY ./package.json /app/package.json
WORKDIR /app
RUN npm install
RUN rm -f .npmrc

In my Azure DevOps build pipeline this has always worked. The Build image part of the pipeline feeds in this build arg from a variable like this - --build-arg NPM_TOKEN=$(ArtifactsNpmPat) - where ArtifactsNpmPat is a variable in my library.

Recently my builds have started failing. Initially I assumed my token had expired so I generated and stored a new one. Here's the error from the agent:

[error]The command '/bin/sh -c npm install' returned a non-zero code: 1

[error]The process '/usr/bin/docker' failed with exit code 1

Note that the same process continues to work locally. So I've no idea how to diagnose this. I did find this SO post which led me to update my Dockerfile to look like this:

FROM node:alpine as build
ARG NPM_TOKEN
COPY ./.npmrc.docker /app/.npmrc
COPY ./package.json /app/package.json
WORKDIR /app
RUN npm install -g vsts-npm-auth
RUN vsts-npm-auth -config .npmrc
RUN npm install
RUN rm -f .npmrc

However, a docker build now generates a pretty crazy error from the RUN vsts-npm-auth command.

/usr/local/bin/vsts-npm-auth: line 1: MZ�╚╝���@���: not found
/usr/local/bin/vsts-npm-auth: line 1: �ԞO���: not found
/usr/local/bin/vsts-npm-auth: line 57: ╔╚��║[�
                                              ╔0�
&� �@ ╗╝═�╗��╔╚.rsrc�@@.reloc
                             �╗�@�H╗║�?�Y
                                         ╗*═d��╚���╗(&
╗╚s'
}║╝╗╗╚═}═╝*╗{═╝*0╗4╔╗╚(      ═╔╗╚(
═╔╚╚(
     ═╔╗╚(
          ═╔╗╚(
+═*0╔╗╚/(═
+═*0╝M╗╗{║╝o(

═()
�╔
  ,+╚═o*
�╔     ,s+
z═o,
Xo-
╝+║╚╝+╝*0╔╗╚?(═
+═*0╔╗╚#(═
+═*0A╚╚r╔po.
-╗{║╝o/
r╔p(0
+╔
═,║╚
    +╗╚/(═
          +*0╝�╝╗╚╝║(═
═�╔    ,&═╝╝,r║p╝�S╔╚(1
s2
z╚║+Z╚═o3

╚╚o,
═X(4
o5

╝-╚+║rgp║-╗(6
%-═&rgp+║rgp╝-rgp+(7
║+║*0╗║║-  ╚╝o8
+╚╝o9

+═*0╚L╗(&
╗╚}
╝╗║}╝╗═}╝╗╗(═}╝*╝═
╗{╝o:

╗(╗{
    ╝s;
╗(═o
╝+#╝o
║╗     ║(═══�╚,╗
╝o
╝╝o
0�s<╔╗30c

╗{╝ripo=

�╚,E(>
r�p�╔%rip�%�(?
�S╔%;�o@
═       sA
oB
═╚╗{╝sC
oB
═sD
╝+╝*0║╗{╝-rp╔p+║r�╔p
╝═╗{╝oE

+*0╗# ╗{
╝%-
   &╗{╝(═: not found
/usr/local/bin/vsts-npm-auth: line 58: syntax error: unexpected word (expecting ")")

So I'm stuck. Has something changed in DevOps around authenticating with its private feeds? Not that I'm aware of, but like I say these builds just stopped working some time in October without me changing anything. Advice appreciated.

Community
  • 1
  • 1
Tom Troughton
  • 3,941
  • 2
  • 37
  • 77
  • auth token expired? – 4c74356b41 Nov 06 '19 at 10:35
  • Like I say above, "I assumed my token had expired so I generated and stored a new one". I've actually done this a couple of times now. Unless there's some bug with DevOps when it generates a new token. I'm grabbing it from the _Connect to feed / npm / Generate credentials_ dialog. – Tom Troughton Nov 06 '19 at 11:58
  • The format of the token has now changed.Will it be because placing the token of the new format in the .npmrc file does not work? You can consider this [situation](https://developercommunity.visualstudio.com/content/problem/619646/unable-to-download-packages-from-npm-registry-usin.html) – Hugh Lin Nov 11 '19 at 10:10
  • Hi. Thank you for the comment. I'm actually not sure what was happening. I decided to try the one I had stored locally in an environment variable and that worked in DevOps. I then tried generating a new one in DevOps and used that and that also worked. I noticed that the one I had stored in my DevOps library was much shorter so my theory is that when I copied the generated key from DevOps it was added to my clipboard with a line break so only the first part was pasted into the textbox where I stored it. But that's just a guess, maybe I was just being daft. Either way, it now works :/ – Tom Troughton Nov 12 '19 at 13:42
  • @getsetcode Congratulations! no matter what, it works now. You can share your solution(what you do to make it works well) as an answer. This will benefit other community members who get the same issues more easily to find the solution and we could archive this thread, thanks. Have a good day :) – Hugh Lin Nov 13 '19 at 07:57
  • Not sure it qualifies as an answer. Most likely my own stupid error! I'll leave it as is.. – Tom Troughton Nov 13 '19 at 10:43
  • 2
    I had exact same error.. Turns out, I was trying to run vsts-npm-auth on a linux docker image. This works only on Windows, maybe works on WSL, but not on linux. – Narayana Jul 10 '20 at 06:00

3 Answers3

0

You are trying to run vsts-npm-auth which is not compatible with Linux platform, which in azure docs in documentation is explained that you only need .npmrc file with credentials

dawid debinski
  • 392
  • 4
  • 6
0

As answered by dawid debinski, vsts-npm-auth doesn't run on Linux platform as written here.

Check this documentation out in which you have to authenticate your pipeline with npm authenticate, or use the YAML version.

enter image description here

Sharky
  • 421
  • 6
  • 5
0

You can authenticate to a private Azure DevOps feed in Docker by following these steps:

  1. In Azure Pipelines, pass the System.AccessToken variable as an argument to the Docker build task:
--build-arg NPM_TOKEN=$(System.AccessToken)
  1. Append a line to your .npmrc file to use the NPM_TOKEN argument to authenticate to your private feed:
ARG NPM_TOKEN

printf "\n//pkgs.dev.azure.com/{org}/{project}/_packaging/{feed}/npm/registry/:_authToken=\${NPM_TOKEN} >> ./npmrc
tjosepo
  • 1
  • 1