gpt4 book ai didi

c# - Azure DevOps Docker 在管道中构建并在版本中推送 Docker

转载 作者:行者123 更新时间:2023-12-02 23:54:32 30 4
gpt4 key购买 nike

我们在 Azure DevOps 中使用构建和发布管道。

我可以在构建管道中构建并推送 Docker 容器,如下所示:

- task: Docker@2
inputs:
containerRegistry: 'MyContainerRegistry'
repository: 'classificationapi'
command: 'buildAndPush'
Dockerfile: 'src\Services\Classification\ClassificationService.Api\Dockerfile'
buildContext: ''

我已在 Azure 容器注册表中验证它是否有效。

enter image description here

但是我不想在每个构建中推送 Docker 镜像。

https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/build/docker?view=azure-devops&tabs=yaml

https://learn.microsoft.com/en-us/azure/devops/pipelines/ecosystems/containers/acr-template?view=azure-devops

我想在发布管道运行时推送构建的 Docker 镜像。

我可以像这样从工件中使用 buildAndPush 。

steps:
- task: Docker@2
displayName: 'buildAndPush - classificationapi'
inputs:
containerRegistry: MyContainerRegistry
repository: classificationapi
Dockerfile: '$(System.DefaultWorkingDirectory)/_Project/Src-DockerContent/src/Services/Classification/ClassificationService.Api/Dockerfile'
buildContext: '$(System.DefaultWorkingDirectory)/_Project/Src-DockerContent'

这会产生这样的错误。

2022-10-20T17:23:31.0333929Z Unable to use package assets cache dueto I/O error. This can occur when the same project is built more thanonce in parallel. Performance may be degraded, but the build resultwill not be impacted. 2022-10-20T17:23:31.3189682Z C:\ProgramFiles\dotnet\sdk\6.0.402\Microsoft.Common.CurrentVersion.targets(2302,5):warning MSB3101: Could not write state file"obj\Release\net6.0\Project.Metadata.csproj.AssemblyReference.cache".Access to the pathC:\src\src\Common\Project.Metadata\obj\Release\net6.0\Project.Metadata.csproj.AssemblyReference.cache'is denied.[C:\src\src\Common\Project.Metadata\Project.Metadata.csproj]2022-10-20T17:23:31.3192917Z C:\ProgramFiles\dotnet\sdk\6.0.402\Microsoft.Common.CurrentVersion.targets(2302,5):warning MSB3101: Could not write state file"obj\Release\net6.0\ApiPaging.csproj.AssemblyReference.cache". Accessto the path'C:\src\src\Helpers\ApiPaging\obj\Release\net6.0\ApiPaging.csproj.AssemblyReference.cache'is denied. [C:\src\src\Helpers\ApiPaging\ApiPaging.csproj]2022-10-20T17:23:31.3324719Z C:\ProgramFiles\dotnet\sdk\6.0.402\Roslyn\Microsoft.Managed.Core.targets(190,5):error MSB4018: The "GenerateMSBuildEditorConfig" task failedunexpectedly. [C:\src\src\Helpers\ApiPaging\ApiPaging.csproj]2022-10-20T17:23:31.3543549Z C:\ProgramFiles\dotnet\sdk\6.0.402\Microsoft.Common.CurrentVersion.targets(5650,5):error MSB3491: Could not write lines to file"obj\Release\net6.0\Project.Metadata.csproj.FileListAbsolute.txt".Access to the path'C:\src\src\Common\Project.Metadata\obj\Release\net6.0\Project.Metadata.csproj.FileListAbsolute.txt'is denied.[C:\src\src\Common\Project.Metadata\Project.Metadata.csproj]2022-10-20T17:23:31.3697424Z Unable to use package assets cache dueto I/O error. This can occur when the same project is built more thanonce in parallel. Performance may be degraded, but the build resultwill not be impacted.

但是这对我们来说也不理想。我想在构建管道中使用 Docker 构建,并在运行发布管道时使用 Docker Push。这可能吗?

如果我尝试仅在发布中进行推送并尝试使用标签 $(Build.BuildId) 查找图像(标签在构建和发布中均设置),则它不起作用,因为预计。

2022-10-25T13:25:28.1790448Z ##[section]Starting: Push -classificationapi 2022-10-25T13:25:28.1981638Z============================================================================== 2022-10-25T13:25:28.1982033Z Task : Docker2022-10-25T13:25:28.1982408Z Description : Build or push Dockerimages, login or logout, start or stop containers, or run a Dockercommand 2022-10-25T13:25:28.1982789Z Version : 2.210.02022-10-25T13:25:28.1983359Z Author : Microsoft Corporation2022-10-25T13:25:28.1983731Z Help :https://aka.ms/azpipes-docker-tsg 2022-10-25T13:25:28.1984083Z============================================================================== 2022-10-25T13:25:39.2512933Z [command]C:\Windows\system32\docker.exeimages 2022-10-25T13:25:39.4268097Z[command]C:\Windows\system32\docker.exe push/classificationapi:4574 2022-10-25T13:25:56.6251803Z REPOSITORY TAG IMAGE ID CREATED SIZE2022-10-25T13:25:56.6253863Z mcr.microsoft.com/dotnet/framework/aspnet4.8-windowsservercore-ltsc2022 df75de263e44 13 days ago 6.04GB 2022-10-25T13:25:56.6255727Z mcr.microsoft.com/dotnet/framework/sdk
4.8-windowsservercore-ltsc2022 93905cba049c 13 days ago 9.34GB 2022-10-25T13:25:56.6256761Zmcr.microsoft.com/dotnet/framework/runtime
4.8-windowsservercore-ltsc2022 a736641c6939 13 days ago 5.7GB 2022-10-25T13:25:56.6257571Z mcr.microsoft.com/windows/servercore
ltsc2022 06bb218927a8 2 weeks ago 5.26GB2022-10-25T13:25:56.6258377Z mcr.microsoft.com/windows/nanoserver
ltsc2022 fc628a662d22 2 weeks ago 297MB2022-10-25T13:25:56.6509177Z The push refers to repository[
/classificationapi] 2022-10-25T13:25:56.6510441Z An image does notexist locally with the tag: ***/classificationapi2022-10-25T13:25:56.6709669Z ##[error]The process'C:\Windows\system32\docker.exe' failed with exit code 12022-10-25T13:25:56.6790024Z ##[section]Finishing: Push -classificationapi

最佳答案

构建

您需要在构建管道中构建它,使用docker save将其存储为tar文件,然后将tar文件作为管道工件上传。

    steps:
- task: Docker@2
displayName: Build
inputs:
containerRegistry: 'My registered registry'
repository: 'repo/image'
command: 'build'
Dockerfile: '$(Build.SourcesDirectory)/PathTo/Dockerfile'
buildContext: '$(Build.SourcesDirectory)'
tags: |
dev-latest
$(Build.BuildNumber)
$(Build.BuildId)
- task: Bash@3
displayName: Save Docker Image
inputs:
targetType: 'inline'
script: |
docker save -o $(Build.ArtifactStagingDirectory)/image.tar image:$(Build.BuildNumber)
- task: PublishPipelineArtifact@1
inputs:
targetPath: '$(Build.ArtifactStagingDirectory)/image.tar'
artifact: 'my-artifact'
publishLocation: 'pipeline'

发布

然后在您的版本中,使用代理池创建一个包含代理的阶段,该代理池可以访问您要发布到的注册表。然后,添加两个任务;一个 bash 任务将保存的镜像 tar 加载到当前 docker 中,然后一个 docker 任务将其推送到注册表。

(发布任务的yaml输出)

steps:
- bash: |
docker load --input $(Agent.ReleaseDirectory)/_BuildArtifact/my-artifact/image.tar
displayName: 'Load container from build'
- task: Docker@2
displayName: 'Push to Regitry'
inputs:
containerRegistry: 'My registered registry'
repository: 'repo/image'
command: push
tags: |
dev-latest
$(Build.BuildNumber)
$(Build.BuildId)

关于c# - Azure DevOps Docker 在管道中构建并在版本中推送 Docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74194454/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com