How to Pass PAT token when building Docker image in azure devops?

Net core project. I have some nuget packages in azure artifact. In Dockerfile I am trying to consume them as below.

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /app
ENV ASPNETCORE_URLS=http://+:80  
EXPOSE 80

# The Personal Access Token arg
ARG PAT

# Set environment variables
ENV NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED true
ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS '{"endpointCredentials":[{"endpoint":"https://myorg.pkgs.visualstudio.com/1234-c281-4ddc-952e-83e5a37a1c66/_packaging/mybusiness/nuget/v3/index.json","username":"USERNAME","password":"'${PAT}'"}]}'

# Get and install the Artifact Credential provider
RUN wget -O - https://raw.githubusercontent.com/Microsoft/artifacts-credprovider/master/helpers/installcredprovider.sh  | bash


COPY mybusiness.sln ./

COPY mybusiness/*.csproj ./mybusiness/

RUN dotnet restore -s "https://mybusiness.pkgs.visualstudio.com/123-c281-4ddc-952e-83e5a37a1c66/_packaging/mybusiness/nuget/v3/index.json" -s "https://api.nuget.org/v3/index.json"


COPY mybusiness/. ./mybusiness/


RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS runtime
WORKDIR /app
COPY --from=build /app/out ./

ENTRYPOINT ["dotnet", "mybusiness.dll"]

Below is my Yaml to build

 - task: [email protected]
 
    - task: [email protected]
      displayName: Build an image
      continueOnError: true
      condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
      inputs:
        containerregistrytype: 'Azure Container Registry'
        azureSubscriptionEndpoint: 'DevOps_on_rg_contributor'
        azureContainerRegistry: 'acr.azurecr.io'
        command: 'build'
        dockerFile: 'src/app/Dockerfile'
        arguments: '--build-arg PAT=$(System.AccessToken)'
        imageName: '$(Build.Repository.Name):$(Build.BuildId)'

This gives me below error Response status code does not indicate success: 401 (Unauthorized).

I am not able to identify the root cause here. Just worried If I am passing correct PAT token? Cansomeone help me to identify the issue. Any help would be appreciated. Thanks

Source: Docker Questions

LEAVE A COMMENT