image - 使用azure pipelines构建docker镜像仍然是401(未经授权)

标签 image azure docker azure-devops azure-pipelines

我正在尝试通过 azure pipelines 构建 Linux docker 镜像。

主要问题是从私有(private)提要恢复。

我读了很多文章如何做到这一点,但仍然遇到同样的错误 “错误:响应状态代码并不表示成功:401(未经授权)。”

我的文件看起来怎么样

Dockerfile:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-alpine AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-alpine AS build
WORKDIR /src

COPY MyAppService/MyAppService.csproj MyAppService/
COPY NuGet.Config .


ARG NUGET_PAT
ARG AZURE_FEED

ENV NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED true
ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS "{\"endpointCredentials\": [{\"endpoint\":\"${AZURE_FEED}\", \"username\":\"docker\", \"password\":\"${NUGET_PAT}\"}]}"

RUN wget -O - https://aka.ms/install-artifacts-credprovider.sh

# check env are ok
RUN printenv

RUN dotnet restore --configfile NuGet.Config "MyAppService/MyAppService.csproj"

COPY MyAppService/. MyAppService/
RUN dotnet build "MyAppService/MyAppService.csproj" -c Release -o /app/build

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

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

NuGet.Config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="mytestorganization" value="https://pkgs.dev.azure.com/mytestorganization/_packaging/mytestorganization/nuget/v3/index.json" />
  </packageSources>
</configuration>

尝试构建图像时来自 azure 管道的日志文件:

2021-05-20T11:36:22.7867465Z ##[section]Starting: build
2021-05-20T11:36:22.7875209Z ==============================================================================
2021-05-20T11:36:22.7875497Z Task         : Docker
2021-05-20T11:36:22.7875990Z Description  : Build or push Docker images, login or logout, start or stop containers, or run a Docker command
2021-05-20T11:36:22.7876270Z Version      : 2.185.0
2021-05-20T11:36:22.7876466Z Author       : Microsoft Corporation
2021-05-20T11:36:22.7876713Z Help         : https://aka.ms/azpipes-docker-tsg
2021-05-20T11:36:22.7876983Z ==============================================================================
2021-05-20T11:36:27.0474614Z [command]/usr/bin/docker build -f /home/vsts/work/1/s/Dockerfile --label com.azure.dev.image.system.teamfoundationcollectionuri=https://dev.azure.com/mytestorganization/ --label com.azure.dev.image.system.teamproject=TestProject --label com.azure.dev.image.build.repository.name=MyAppService --label com.azure.dev.image.build.sourceversion=63bc9999e1005c9aec016231dcc0f15cf9ec83da --label com.azure.dev.image.build.repository.uri=https://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="630e1a170610170c1104020d0a1902170a0c0d230706154d02191611064d000c0e" rel="noreferrer noopener nofollow">[email protected]</a>/mytestorganization/TestProject/_git/MyAppService --label com.azure.dev.image.build.sourcebranchname=testbranch --label com.azure.dev.image.build.definitionname=TestProject-CI --label com.azure.dev.image.build.buildnumber=144 --label com.azure.dev.image.build.builduri=vstfs:///Build/Build/144 --build-arg AZURE_FEED=http://pkgs.dev.azure.com/mytestorganization/_packaging/mytestorganization/nuget/v3/index.json --build-arg NUGET_PAT=wz2ilhvt2urpzqlqlldd2f2hl6do77jkmreydrpmskwco3fc7sva /home/vsts/work/1/s
2021-05-20T11:36:30.9509673Z Sending build context to Docker daemon  203.3kB
2021-05-20T11:36:30.9510485Z 
2021-05-20T11:36:31.0442686Z Step 1/32 : FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-alpine AS base
2021-05-20T11:36:31.2889870Z 3.1-alpine: Pulling from dotnet/core/aspnet
2021-05-20T11:36:31.2907305Z 540db60ca938: Already exists
2021-05-20T11:36:31.3239989Z 9c2872b91813: Pulling fs layer
2021-05-20T11:36:31.3240441Z 8628df474c5d: Pulling fs layer
2021-05-20T11:36:31.3240760Z b54f4c6d2c91: Pulling fs layer
2021-05-20T11:36:31.3981063Z 9c2872b91813: Verifying Checksum
2021-05-20T11:36:31.3981449Z 9c2872b91813: Download complete
2021-05-20T11:36:31.4335354Z b54f4c6d2c91: Verifying Checksum
2021-05-20T11:36:31.4339309Z b54f4c6d2c91: Download complete
2021-05-20T11:36:31.6869948Z 8628df474c5d: Verifying Checksum
2021-05-20T11:36:31.6870377Z 8628df474c5d: Download complete
2021-05-20T11:36:32.0023325Z 9c2872b91813: Pull complete
2021-05-20T11:36:33.1851800Z 8628df474c5d: Pull complete
2021-05-20T11:36:33.5201247Z b54f4c6d2c91: Pull complete
2021-05-20T11:36:33.5262719Z Digest: sha256:97c4986a2f9c8d2bc40443eb0baac59f35c43c267fc59ecc626b449701152df9
2021-05-20T11:36:33.5294596Z Status: Downloaded newer image for mcr.microsoft.com/dotnet/core/aspnet:3.1-alpine
2021-05-20T11:36:33.5314476Z  ---> f039e2539446
2021-05-20T11:36:33.5314887Z Step 2/32 : WORKDIR /app
2021-05-20T11:36:33.5511196Z  ---> Running in 935279916ded
2021-05-20T11:36:34.0084321Z Removing intermediate container 935279916ded
2021-05-20T11:36:34.0085263Z  ---> acbf5abca7a7
2021-05-20T11:36:34.0085629Z Step 3/32 : EXPOSE 80
2021-05-20T11:36:34.0265101Z  ---> Running in f7fa1a4dd856
2021-05-20T11:36:35.0147354Z Removing intermediate container f7fa1a4dd856
2021-05-20T11:36:35.0148907Z  ---> ac616daad00f
2021-05-20T11:36:35.0152411Z Step 4/32 : EXPOSE 443
2021-05-20T11:36:35.0322549Z  ---> Running in b3850b437c79
2021-05-20T11:36:36.0098236Z Removing intermediate container b3850b437c79
2021-05-20T11:36:36.0099219Z  ---> cb4e679d3e31
2021-05-20T11:36:36.0099779Z Step 5/32 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1-alpine AS build
2021-05-20T11:36:36.5177448Z 3.1-alpine: Pulling from dotnet/core/sdk
2021-05-20T11:36:36.5178298Z 540db60ca938: Already exists
2021-05-20T11:36:36.5178735Z 9c2872b91813: Already exists
2021-05-20T11:36:36.5179120Z 8628df474c5d: Already exists
2021-05-20T11:36:36.5179575Z b54f4c6d2c91: Already exists
2021-05-20T11:36:36.5180041Z c1b534528ba7: Pulling fs layer
2021-05-20T11:36:36.5180819Z db0b8f999807: Pulling fs layer
2021-05-20T11:36:36.5181265Z 668300e8efd0: Pulling fs layer
2021-05-20T11:36:36.5181785Z 668300e8efd0: Verifying Checksum
2021-05-20T11:36:36.5182381Z 668300e8efd0: Download complete
2021-05-20T11:36:36.5182681Z c1b534528ba7: Verifying Checksum
2021-05-20T11:36:36.5182991Z c1b534528ba7: Download complete
2021-05-20T11:36:36.9095207Z db0b8f999807: Verifying Checksum
2021-05-20T11:36:36.9095902Z db0b8f999807: Download complete
2021-05-20T11:36:37.5777094Z c1b534528ba7: Pull complete
2021-05-20T11:36:41.4075714Z db0b8f999807: Pull complete
2021-05-20T11:36:42.0343103Z 668300e8efd0: Pull complete
2021-05-20T11:36:42.0398152Z Digest: sha256:468b7f80a5ddad15081eddf072ea67d2d003e82c7937392bf927692b134633f5
2021-05-20T11:36:42.0430106Z Status: Downloaded newer image for mcr.microsoft.com/dotnet/core/sdk:3.1-alpine
2021-05-20T11:36:42.0445206Z  ---> a08ae6c8a2aa
2021-05-20T11:36:42.0445502Z Step 6/32 : WORKDIR /src
2021-05-20T11:36:42.0748860Z  ---> Running in 6dae098fc8ed
2021-05-20T11:36:43.0138598Z Removing intermediate container 6dae098fc8ed
2021-05-20T11:36:43.0139677Z  ---> e151bdc4004f
2021-05-20T11:36:43.0140964Z Step 7/32 : COPY MyAppService/MyAppService.csproj MyAppService/
2021-05-20T11:36:44.0159585Z  ---> 30c607bb8a6e
2021-05-20T11:36:44.0159936Z Step 8/32 : COPY NuGet.Config .
2021-05-20T11:36:45.0146826Z  ---> 21b4b5d1f04c
2021-05-20T11:36:45.0147184Z Step 9/32 : ARG NUGET_PAT
2021-05-20T11:36:45.0460635Z  ---> Running in 88d83e60cf3a
2021-05-20T11:36:46.0172444Z Removing intermediate container 88d83e60cf3a
2021-05-20T11:36:46.0173490Z  ---> 58d9dde29d1d
2021-05-20T11:36:46.0173821Z Step 10/32 : ARG AZURE_FEED
2021-05-20T11:36:46.0360209Z  ---> Running in f10912b5009e
2021-05-20T11:36:47.0134976Z Removing intermediate container f10912b5009e
2021-05-20T11:36:47.0135971Z  ---> e792f313ec6c
2021-05-20T11:36:47.0136338Z Step 11/32 : ENV NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED true
2021-05-20T11:36:47.0340003Z  ---> Running in dc9e064c144a
2021-05-20T11:36:48.0165125Z Removing intermediate container dc9e064c144a
2021-05-20T11:36:48.0166236Z  ---> 2cb8ce18a003
2021-05-20T11:36:48.0166823Z Step 12/32 : ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS "{\"endpointCredentials\": [{\"endpoint\":\"${AZURE_FEED}\", \"username\":\"docker\", \"password\":\"${NUGET_PAT}\"}]}"
2021-05-20T11:36:48.0346391Z  ---> Running in 06e748e76592
2021-05-20T11:36:49.0167180Z Removing intermediate container 06e748e76592
2021-05-20T11:36:49.0168216Z  ---> 21dff5c2a393
2021-05-20T11:36:49.0168806Z Step 13/32 : RUN wget -O - https://aka.ms/install-artifacts-credprovider.sh
2021-05-20T11:36:49.0331863Z  ---> Running in 653d535fd87e
2021-05-20T11:36:50.5360687Z [91mConnecting to aka.ms (104.67.209.176:443)
2021-05-20T11:36:50.9255227Z [0m[91mConnecting to raw.githubusercontent.com (185.199.109.133:443)
2021-05-20T11:36:51.0266615Z [0m[91mwriting to stdout
2021-05-20T11:36:51.0269224Z [0m#!/usr/bin/env bash
2021-05-20T11:36:51.0269620Z # DESCRIPTION: A simple shell script designed to fetch the latest version
2021-05-20T11:36:51.0270020Z # of the artifacts credential provider plugin for dotnet and
2021-05-20T11:36:51.0270333Z # install it into $HOME/.nuget/plugins.
2021-05-20T11:36:51.0270979Z # SEE: https://github.com/Microsoft/artifacts-credprovider/blob/master/README.md
2021-05-20T11:36:51.0271177Z 
2021-05-20T11:36:51.0271529Z REPO="Microsoft/artifacts-credprovider"
2021-05-20T11:36:51.0271833Z FILE="Microsoft.NuGet.CredentialProvider.tar.gz"
2021-05-20T11:36:51.0272080Z VERSION="latest"
2021-05-20T11:36:51.0272612Z # URL pattern documented at https://help.github.com/en/articles/linking-to-releases as of 2019-03-29
2021-05-20T11:36:51.0273028Z URI="https://github.com/$REPO/releases/$VERSION/download/$FILE"
2021-05-20T11:36:51.0273327Z NUGET_PLUGIN_DIR="$HOME/.nuget/plugins"
2021-05-20T11:36:51.0273475Z 
2021-05-20T11:36:51.0273677Z # Ensure plugin directory exists
2021-05-20T11:36:51.0274069Z if [ ! -d "${NUGET_PLUGIN_DIR}" ]; then
2021-05-20T11:36:51.0274767Z   echo "INFO: Creating the nuget plugin directory (i.e. ${NUGET_PLUGIN_DIR}). "
2021-05-20T11:36:51.0275283Z   if ! mkdir -p "${NUGET_PLUGIN_DIR}"; then
2021-05-20T11:36:51.0275655Z       echo "ERROR: Unable to create nuget plugins directory (i.e. ${NUGET_PLUGIN_DIR})."
2021-05-20T11:36:51.0275955Z       exit 1
2021-05-20T11:36:51.0276136Z   fi
2021-05-20T11:36:51.0276320Z fi
2021-05-20T11:36:51.0276403Z 
2021-05-20T11:36:51.0276599Z echo "Downloading from $URI"
2021-05-20T11:36:51.0276893Z # Extract netcore from the .tar.gz into the plugin directory
2021-05-20T11:36:51.0277180Z 
2021-05-20T11:36:51.0277502Z #Fetch the file
2021-05-20T11:36:51.0278259Z curl -H "Accept: application/octet-stream" \
2021-05-20T11:36:51.0278673Z      -s \
2021-05-20T11:36:51.0279032Z      -S \
2021-05-20T11:36:51.0279377Z      -L \
2021-05-20T11:36:51.0279833Z      "$URI" | tar xz -C "$HOME/.nuget/" "plugins/netcore"
2021-05-20T11:36:51.0280016Z 
2021-05-20T11:36:51.0280338Z echo "INFO: credential provider netcore plugin extracted to $HOME/.nuget/"
2021-05-20T11:36:51.0291723Z [91m-                    100% |********************************|  1213[0m[91m  0:00:00 ETA
2021-05-20T11:36:51.0292150Z written to stdout
2021-05-20T11:36:52.0145556Z [0mRemoving intermediate container 653d535fd87e
2021-05-20T11:36:52.0146179Z  ---> 90962e0fe2f3
2021-05-20T11:36:52.0146481Z Step 14/32 : RUN printenv
2021-05-20T11:36:52.0341626Z  ---> Running in de688137dee7
2021-05-20T11:36:52.3685737Z DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
2021-05-20T11:36:52.3686185Z NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED=true
2021-05-20T11:36:52.3686469Z HOSTNAME=de688137dee7
2021-05-20T11:36:52.3686745Z DOTNET_USE_POLLING_FILE_WATCHER=true
2021-05-20T11:36:52.3686993Z SHLVL=1
2021-05-20T11:36:52.3687187Z HOME=/root
2021-05-20T11:36:52.3687432Z DOTNET_RUNNING_IN_CONTAINER=true
2021-05-20T11:36:52.3687794Z AZURE_FEED=http://pkgs.dev.azure.com/mytestorganization/_packaging/mytestorganization/nuget/v3/index.json
2021-05-20T11:36:52.3688201Z NUGET_PAT=wz2ilhvt2urpzqlqlldd2f2hl6do77jkmreydrpmskwco3fc7sva
2021-05-20T11:36:52.3689296Z POWERSHELL_DISTRIBUTION_CHANNEL=PSDocker-DotnetCoreSDK-Alpine-3.13
2021-05-20T11:36:52.3689701Z PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
2021-05-20T11:36:52.3690173Z LANG=en_US.UTF-8
2021-05-20T11:36:52.3690539Z LC_ALL=en_US.UTF-8
2021-05-20T11:36:52.3690774Z ASPNETCORE_URLS=
2021-05-20T11:36:52.3691003Z PWD=/src
2021-05-20T11:36:52.3691618Z VSS_NUGET_EXTERNAL_FEED_ENDPOINTS={"endpointCredentials": [{"endpoint":"http://pkgs.dev.azure.com/mytestorganization/_packaging/mytestorganization/nuget/v3/index.json", "username":"docker", "password":"wz2ilhvt4ghjrpzqlqllxsda2hl6do77jkmreyvotsaskwco3fc7sva"}]}
2021-05-20T11:36:52.3692283Z NUGET_XMLDOC_MODE=skip
2021-05-20T11:36:53.0269908Z Removing intermediate container de688137dee7
2021-05-20T11:36:53.0277361Z  ---> 47c3745c86a9
2021-05-20T11:36:53.0278468Z Step 15/32 : RUN dotnet restore --configfile NuGet.Config "MyAppService/MyAppService.csproj"
2021-05-20T11:36:53.0300480Z  ---> Running in 7cb2aad61823
2021-05-20T11:36:54.4782539Z   Determining projects to restore...
2021-05-20T11:36:55.5458935Z /usr/share/dotnet/sdk/3.1.409/NuGet.targets(128,5): error : Unable to load the service index for source https://pkgs.dev.azure.com/mytestorganization/_packaging/mytestorganization/nuget/v3/index.json. [/src/MyAppService/MyAppService.csproj]
2021-05-20T11:36:55.5459838Z /usr/share/dotnet/sdk/3.1.409/NuGet.targets(128,5): error :   Response status code does not indicate success: 401 (Unauthorized). [/src/MyAppService/MyAppService.csproj]
2021-05-20T11:36:55.7208436Z The command '/bin/sh -c dotnet restore --configfile NuGet.Config "MyAppService/MyAppService.csproj"' returned a non-zero code: 1
2021-05-20T11:36:55.7348351Z ##[error]The command '/bin/sh -c dotnet restore --configfile NuGet.Config "MyAppService/MyAppService.csproj"' returned a non-zero code: 1
2021-05-20T11:36:55.7359274Z ##[error]The process '/usr/bin/docker' failed with exit code 1
2021-05-20T11:36:55.7376406Z ##[section]Finishing: build

我尝试了很多想法,比如提供完整的访问 token 。那么我做错了什么?

最佳答案

尝试将验证信息添加到您的包源中。使用packageSourceCredentials在自定义 nuget.config 文件下:

将您的 nuget.config 修改为:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="mytestorganization" value="https://pkgs.dev.azure.com/mytestorganization/_packaging/mytestorganization/nuget/v3/index.json" />
  </packageSources>
  <packageSourceCredentials>
    <mytestorganization>
        <add key="Username" value="xxxx" />
        <add key="Password" value="xxxx" />
    </mytestorganization>
  </packageSourceCredentials>


</configuration>

关于image - 使用azure pipelines构建docker镜像仍然是401(未经授权),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67619820/

相关文章:

caching - azure : shared cache - delete all?

linux - docker在后台运行一个shell脚本而不退出容器

docker - Docker卷安装路径

python - 在两种不同颜色之间更改像素颜色

c# - 在 C# 中将字符串转换为图像

node.js - 检查 CosmosDB 查询中参数是否为 null

docker - 如何运行CKAN泊坞镜像?

python - 如何使用numpy调整图像数据的大小?

c# - 转换后的 BitmapImage 未显示在页面上

azure - 是否可以使用美国政府租户在 'commercial' 公共(public) Azure 门户中注册的 Azure AD 应用程序?