Authenticate to private npm registry from Dockerfile in azure? - azure

I have the below Dockerfile:
FROM node:alpine
WORKDIR ./usr/local/lib/node_modules/npm/
COPY .npmrc ./
COPY package.json ./
COPY package-lock.json ./
RUN npm ci
COPY . .
EXPOSE 3000
CMD ["npm", "run", "start-prod"]
This file is used in an azure pipeline like so:
variables:
- name: NPMRC_LOCATION
value: $(Agent.TempDirectory)
- stage: BuildPublishDockerImage
displayName: Build and publish Docker image
dependsOn: Build
jobs:
- job: BuildPublishDockerImage
steps:
- checkout: self
- task: DownloadSecureFile#1
name: npmrc
inputs:
secureFile: .npmrc
- task: npmAuthenticate#0
inputs:
workingFile: $(NPMRC_LOCATION)/.npmrc
- task: Docker#2
displayName: Build a Docker image
inputs:
command: build
arguments: --no-cache
I know .npmrc should be in that location (I run RUN ls in the Dockerfile and its there).
However when I run it I keep getting this error:
failed to solve with frontend dockerfile.v0: failed to build LLB: failed to compute cache key: "/.npmrc" not found: not found
I just want to authenticate to a private npm registry. I'm mystified by this. Grateful for any help.

You can in Dockerfile set registry
npm config set registry

Related

The app build failed to produce artifact folder Angular Azure Static Web App Deployment Issue

When i try to build angular app using azure devops and deploy to azure static web app i'm receiving below error
The app build failed to produce artifact folder: 'dist/harmony-front'. Please
ensure this property is configured correctly in your deployment
configuration file.
I tried by changing output_location to / , dist, /dist , dist/harmony-front,
nothing seems to work
here's the yaml code for the deployment section
- task: AzureStaticWebApp#0
inputs:
app_location: "/"
api_location: "api"
output_location: "./dist/harmony-front"
app_build_command: 'npm run build:prod'
skip_api_build: true
verbose: true
env:
continueOnError: true
CUSTOM_BUILD_COMMAND: "npm install --force"
azure_static_web_apps_api_token: $(deployment_token)
What was the mistake i made
Thanks
I have tried to repro the same and got positive results after following the below steps.
Step 1: Create a simple angular project and build the project on a local machine and inspect the dist folder.
Step 2: Push the code to Azure Repos.
Step 3: Create azure pipeline as shown below.
trigger: none
pool:
vmImage: ubuntu-latest
steps:
- task: NodeTool#0
inputs:
versionSpec: '18.x'
displayName: 'Install Node.js'
- script: |
npm install -g #angular/cli
displayName: 'install angular cli'
- task: Npm#1
inputs:
command: 'install'
displayName: 'npm install'
- task: Npm#1
inputs:
command: 'custom'
customCommand: 'run build'
displayName: 'npm build'
- task: Bash#3
inputs:
targetType: 'inline'
script: |
pwd
ls -l
ls -l dist/
- task: AzureStaticWebApp#0
displayName: 'Deploy Azure static webapp'
inputs:
app_location: '/'
output_location: 'dist/my-app'
env:
azure_static_web_apps_api_token: $(static-webapp-token)
Step 4: Verify the result.
If you are still facing that issue, verify the AzureStaticWebApp#0
output location path in angular.json file as below. Both paths must be exact.
Actually I found the issue and will post for future reference. if someone had the same issue.
issue start when you override build command using CUSTOM_BUILD_COMMAND will overwrite the RUN_BUILD_COMMAND so based on the comment of the issue posted on Github instead of npm install --force command combined with build like npm install --force && npm run build works like charm

Cache task in Azure Pipeline to reducer build time

I would like to use Azure Pipeline "Cache Task" to cache npm and later on when I build my docker image based on dockerfile, use that cache to decrease my build time. I do rarely, to almost never change my package.json file nowadays, but the docker image build in Azure Pipeline is very slow. If I take a look at the logs while building etc. based on my dockerfile, the command 'npm install' makes up most of the build time. I've made some research but can't find such case...
This is what I've come up with at the moment:
azure-pipeline.yml
- "test"
resources:
- repo: self
pool:
vmImage: "ubuntu-latest"
variables:
tag: "$(Build.BuildId)"
DOCKER_BUILDKIT: 1
npm_config_cache: $(Pipeline.Workspace)/.npm
steps:
- task: Cache#2
inputs:
key: 'npm | "$(Agent.OS)" | package.json'
path: "$(npm_config_cache)"
cacheHitVar: "CACHE_RESTORED"
displayName: Cache npm
- script: npm ci
- task: Docker#2
displayName: Build an image
inputs:
command: "build"
Dockerfile: "**/Dockerfile.test"
arguments: "--cache-from=$(npm_config_cache)" <--- I WOULD LIKE TO USE THE CACHE IN THIS TASK
tags: "$(tag)"
Dockerfile
FROM node:19-alpine as build
WORKDIR /app
COPY package.json .
ENV REACT_APP_ENVIRONMENT=test
RUN npm ci --cache $(npm_config_cache) <-- This is not right but I would like to do something like this
COPY . .
RUN npm run build
#Stage 2
FROM nginx:1.23.2-alpine
WORKDIR /usr/share/nginx/html
RUN rm -rf *
COPY nginx/default.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE 80
ENTRYPOINT ["nginx", "-g", "daemon off;"]
So what I would like to do is the use the cache task in my azure-pipeline in the next step/task of the azure-pipeline where I build my docker image based on the dockerfile provided above. In my Dockerfile I got the line -> RUN npm ci --cache $(npm_config_cache) That somehow I would like to reach it, or is that even possible? I can't really figure it out and maybe I'm totaly wrong about my approach?
Many thanks!

Azure pipeline not applying label to Docker build container

As the title says, I am trying to apply a label to my docker container so I can then reference said container in a further step in my pipeline. My end result that I am going for is to be able to copy my test results out of the container and publish and display those results.
azure-pipeline.yml
# Docker
# Build a Docker image
# https://learn.microsoft.com/azure/devops/pipelines/languages/docker
trigger:
- main
- develop
resources:
- repo: self
variables:
tag: '$(Build.BuildId)'
stages:
- stage: Build
displayName: Build image
jobs:
- job: Build
displayName: Build
pool:
name: default
steps:
- task: Docker#2
displayName: Build an image
inputs:
command: build
arguments: '--build-arg BuildId=$(Build.BuildId)'
dockerfile: '$(Build.SourcesDirectory)/Dockerfile'
tags: |
$(tag)
- powershell: |
$id=docker images --filter "label=test=$(Build.BuildId)" -q | Select-Object -First 1
docker create --name testcontainer $id
docker cp testcontainer:/testresults ./testresults
docker rm testcontainer
displayName: 'Copy test results'
- task: PublishTestResults#2
displayName: 'Publish test results'
inputs:
testResultsFormat: 'xUnit'
testResultsFiles: '**/*.trx'
searchFolder: '$(System.DefaultWorkingDirectory)/testresults'
Dockerfile
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:6.0-bullseye-slim-amd64 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0-bullseye-slim-amd64 AS build
WORKDIR /src
COPY ["Forecast/Forecast.csproj", "Forecast/"]
WORKDIR "/src/Forecast"
RUN dotnet restore "Forecast.csproj"
COPY Forecast/ .
RUN dotnet build "Forecast.csproj" -c Release -o /app/build
FROM build AS publish
WORKDIR /src
RUN dotnet publish "Forecast/Forecast.csproj" --no-restore -c Release -o /app/publish
FROM build as test
ARG BuildId
LABEL test=${BuildId}
WORKDIR /src
COPY ["ForecastXUnitTest/ForecastXUnitTest.csproj", "ForecastXUnitTest/"]
WORKDIR "/src/ForecastXUnitTest"
RUN dotnet restore "ForecastXUnitTest.csproj"
COPY ForecastXUnitTest/ .
RUN dotnet build "ForecastXUnitTest.csproj" -c Release -o /app
RUN dotnet test -c Release --results-directory /testresults --logger "trx;LogFileName=test_results.trx" "ForecastXUnitTest.csproj"
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Forecast.dll"]
I can see that the label hasn't been applied upon inspection of the build steps. The powershell step specifically the line docker create --name testcontainer $id the variable $id is empty and is telling me that the label is never applied so I'm not able to go any further.
I ended up figuring out where my issue lay. I end up with cache'd ephemeral container images. With these in place Docker no longer runs those steps so along the way I added in the steps but with what had preceded in builds I wasn't getting anything from subsequent calls.
On top of this I had been referring to a previous image with the directive FROM that was preventing my directory from being found.

How to set a Dockerfile ARG in Github actions

I have a Dockerfile for one of my Node.js services that I try to push to my Digitalocean registry through the use of Github actions.
My Node.js service requires a private package that is hosted by myself on npm.js registry.
In my Dockerfile, I have an ARG for that:
FROM node:14-slim
ARG NODE_ENV=production
EXPOSE 5000
WORKDIR /usr/src/app
ARG NPM_TOKEN
COPY .npmrc .npmrc
COPY package*.json ./
RUN npm install
RUN rm -f .npmrc
COPY src src
CMD ["npm", "start"]
and the following .npmrc file:
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
in my Github actions workflow I have two actions. One for running tests:
name: tests-user-service
on:
pull_request:
paths:
- 'user-service/**'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Install dependencies & run tests
run: cd user-service && npm install && npm run test:ci
env:
NPM_TOKEN: ${{secrets.NPM_ACCESS_TOKEN}}
and one for building the docker file and pushing it to the registry:
name: deploy-user-service
on:
push:
branches:
- main
paths:
- 'user-service/**'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Check Out Repo
uses: actions/checkout#v2
- name: Install DigitalOcean Controller
uses: digitalocean/action-doctl#v2
with:
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
- name: Set up Docker Builder
uses: docker/setup-buildx-action#v1
- name: Authenticate with DigitalOcean Container Registry
run: doctl registry login --expiry-seconds 600
- name: Build and Push to DigitalOcean Container Registry
uses: docker/build-push-action#v2
with:
context: ./user-service
push: true
tags: registry.digitalocean.com/xxx/xxx:latest
env:
NPM_TOKEN: ${{secrets.NPM_ACCESS_TOKEN}}
- name: Logout from DigitalOcean Container Registry
run: doctl registry logout
now, the test file works. So I know the NPM_ACCESS_TOKEN is set correctly.
The deployment file, that one fails. It tells me this:
#10 [5/7] RUN npm install
#10 sha256:28b0590a43c14b889983d16b5e375f0156f7fdacc29e32fc3c219bce54e61d69
#10 0.317 Error: Failed to replace env in config: ${NPM_TOKEN}
I tried the following in my Dockerfile:
FROM node:14-slim
ARG NODE_ENV=production
EXPOSE 5000
WORKDIR /usr/src/app
ARG NPM_TOKEN
ENV NPM_TOKEN ${NPM_TOKEN}
COPY .npmrc .npmrc
COPY package*.json ./
RUN npm install
RUN rm -f .npmrc
COPY src src
CMD ["npm", "start"]
and then it goes wrong and tells me this:
#10 2.784 npm ERR! code E404
#10 2.790 npm ERR! 404 Not Found - GET https://registry.npmjs.org/#xxx%2fxxx - Not found
#10 2.790 npm ERR! 404
#10 2.790 npm ERR! 404 '#xxx/xxx#^0.0.11' is not in the npm registry.
#10 2.790 npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
So then it can't find the module. I assume this happens because the NPM_TOKEN is now set with an empty string or so, as in the tests action it does work properly.
Any idea what else I could try?
Actually, I figured it out. Have to add build-args to the Build and Push part, and remove the env from there.
So instead of:
- name: Build and Push to DigitalOcean Container Registry
uses: docker/build-push-action#v2
with:
context: ./user-service
push: true
tags: registry.digitalocean.com/xxx/xxx:latest
env:
NPM_TOKEN: ${{secrets.NPM_ACCESS_TOKEN}}
should use this:
- name: Build and Push to DigitalOcean Container Registry
uses: docker/build-push-action#v2
with:
context: ./user-service
push: true
tags: registry.digitalocean.com/xxx/xxx:latest
build-args: NPM_TOKEN=${{secrets.NPM_ACCESS_TOKEN}}
Build args are list type CSV, check the readme
build-args: |
NPM_TOKEN =${{ secrets.NPM_TOKEN }}

Automatic deploy from gitlab to heroku

Hi guys i was wondering if it is possible to make automatic deploys from gitlab to heroku (react app), right now this is my gitlab-ci
image: node:8.10.0-alpine
cache:
key: "alpine"
paths:
- node_modules/
before_script:
- npm install
build:
stage: build
artifacts:
paths:
- dist/
script:
- npm run build
only:
- master
tags:
- docker
is there a way to push the builded proyect to heroku?

Resources