【问题标题】:How to substitute environment variables in Nuget.config when creating a Docker container via Azure Devops YAML Pipelines?通过 Azure Devops YAML Pipelines 创建 Docker 容器时如何替换 Nuget.config 中的环境变量?
【发布时间】:2022-01-20 20:01:45
【问题描述】:

我正在通过 Azure Devops YAML 管道在 docker 容器内创建一个 .NET Core 6 Web 应用程序。

但是,我遇到了 nuget 无法从私人商业外部 nuget 提要(用于 Telerik)恢复包的问题。 “HTTP401 身份验证失败”响应。

那么如何将 %TELERIKUSERNAME% 和 %TELERIKPASSWORD% 环境变量的值从构建管道传递到 docker?

这里的 MS Docs https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/build/docker?view=azure-devopsarguments 被忽略(即使我知道要使用的格式)

仅供参考,我正在从 Azure Keyvault 安全地检索值,通过 powershell 将它们转换为环境变量并将它们传递到不同的 .net 核心 nuget 还原任务(未显示)。这部分管道工作正常,所以我知道我有正确的凭据。

我认为这是我当前问题需要分享的所有必要文件

Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["A/A.csproj", "A/"]
COPY ["B/B.csproj", "B/"]
COPY ["C/C.csproj", "C/"]
COPY ["D/D.csproj", "D/"]
COPY . .
WORKDIR "/src/A"
RUN dotnet build "A.csproj" -c Release -o /app/build

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "A.dll"]

Nuget.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <packageSources>
        <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
        <add key="Telerik_NuGet" value="https://nuget.telerik.com/v3/index.json" protocolVersion="3" />
    </packageSources>   
    <packageSourceCredentials>
        <Telerik_NuGet>
            <add key="Username" value="%TELERIKUSERNAME%" />
            <add key="ClearTextPassword" value="%TELERIKPASSWORD%" />
        </Telerik_NuGet>
    </packageSourceCredentials>
</configuration> 

azure-pipelines.yml

# ... snip...

- task: Docker@2
      displayName: Create Docker Image
      inputs:
        containerRegistry: $(DockerContainerRegistryConnection)
        repository: $(DockerRepository)
        command: 'buildAndPush'
        addPipelineData: true
        tags: $(Build.BuildNumber)
        Dockerfile: '**/Dockerfile'
        buildContext: 'src/'        

干杯

凯尔

【问题讨论】:

    标签: .net-core dockerfile nuget azure-pipelines


    【解决方案1】:

    终于解决了这个问题。遵循这些页面上的建议:

    1. https://github.com/dotnet/dotnet-docker/blob/main/documentation/scenarios/nuget-credentials.md
    2. I am using Azure Devops to build and push my Docker image. How can I pass arguments while doing buildAndPush using Docker task?

    基本上创建 Docker Args 是临时的,出于安全原因不会保存在容器内,因为它们不是容器内的最后一个“层”。还将 TML 任务一分为二。

    将我的文件更改为:

    Dockerfile

    FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
    WORKDIR /app
    EXPOSE 80
    EXPOSE 443
    
    FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
    
    ARG TELERIKUSERNAME
    ARG TELERIKPASSWORD
    
    WORKDIR /src
    COPY ["A/A.csproj", "A/"]
    COPY ["B/B.csproj", "B/"]
    COPY ["C/C.csproj", "C/"]
    COPY ["D/D.csproj", "D/"]
    COPY . .
    WORKDIR "/src/A"
    RUN dotnet build "A.csproj" -c Release -o /app/build
    
    FROM build AS publish
    RUN dotnet publish "A.csproj" -c Release -o /app/publish
    
    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    ENTRYPOINT ["dotnet", "A.dll"]
    
    

    Nuget.config

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <packageSources>
            <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
            <add key="Telerik_NuGet" value="https://nuget.telerik.com/v3/index.json" protocolVersion="3" />
        </packageSources>   
        <packageSourceCredentials>
            <Telerik_NuGet>
                <add key="Username" value="%TELERIKUSERNAME%" />
                <add key="ClearTextPassword" value="%TELERIKPASSWORD%" />
            </Telerik_NuGet>
        </packageSourceCredentials>
    </configuration> 
    
    

    azure-pipelines.yml

    # ... snip ...
    
        - task: Docker@2
          displayName: Build Docker Image
          inputs:
            command: build
            containerRegistry: $(DockerContainerRegistryConnection)
            repository: $(DockerRepository)
            tags: $(Build.BuildNumber)
            arguments: '--build-arg TELERIKUSERNAME=$(TELERIKUSERNAME) --build-arg TELERIKPASSWORD=$(TELERIKPASSWORD)'
            buildContext: 'src/'
    
        - task: Docker@2
          displayName: Push Docker Image
          inputs:
            command: push
            containerRegistry: $(DockerContainerRegistryConnection)
            repository: $(DockerRepository)
            tags: $(Build.BuildNumber)        
            buildContext: 'src/'
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      • 2015-02-07
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多