How use Azure Artifacts Credential Provider for Nuget feeds, with VisualStudio 2022, Linux Docker container?

Viewed 41

I'm running C# unit tests in Visual Studio 2022, for native Linux system calls, in Linux .NET 6.0 SDK containers. In the first attempts, I had a PAT (Personal Access Token) in plain text in the Dockerfile, which I want to replace by ARG FEED_ACCESSTOKEN, taken from environment variable.

There is basically just the .csproj and the Dockerfile, no docker-compose.yml. Optional launchSettings.json in the project.

I don't get any value for FEED_ACCESSTOKEN, trying either variable $(FEED_ACCESSTOKEN) or $(VSS_NUGET_ACCESSTOKEN), test with RUN echo. Visual Studio shows both with blue underlines, "Property '...' is not defined".

How can I get an access token? I found a NuGetAuthenticate task under Azure DevOps, docker-compose.yml examples. I'm not even sure whether I shall use it as docker build or run parameter (therefore I left both in the example).

The project file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>disable</ImplicitUsings>

      <IsPackable>false</IsPackable>

    <AssemblyName>xxx.UnitTests</AssemblyName>

    <RootNamespace>xxx.UnitTests</RootNamespace>

      <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
      <DockerfileContext>../..</DockerfileContext>
      <DockerfileRunArguments>-e FEED_ACCESSTOKEN=$(VSS_NUGET_ACCESSTOKEN)</DockerfileRunArguments>
      <DockerfileBuildArguments>--build-arg FEED_ACCESSTOKEN=$(VSS_NUGET_ACCESSTOKEN)</DockerfileBuildArguments>
  </PropertyGroup>

<!-- ... -->

    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />

<!-- ... -->

</Project>

The Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS base
RUN curl -L https://raw.githubusercontent.com/Microsoft/artifacts-credprovider/master/helpers/installcredprovider.sh  | sh
ARG FEED_ACCESSTOKEN
RUN echo "Feed access token: $FEED_ACCESSTOKEN"
VOLUME "C:\Users\<me>\AppData\Local\Temp" "/tmp/hostuser"
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR src
COPY ["src/xxx.UnitTests/xxx.UnitTests.csproj", "src/xxx.UnitTests/"]
COPY ["nuget.config", "."]
# [add credentials to feed endpoints, redacted]

RUN dotnet restore "src/xxx.UnitTests/xxx.UnitTests.csproj"
COPY . .
WORKDIR "/src/xxx.UnitTests"
RUN dotnet build "xxx.UnitTests.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "xxx.UnitTests.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
# ENTRYPOINT ["dotnet", "xxx.UnitTests.dll"]
0 Answers
Related