gpt4 book ai didi

azure-devops - 如何在 Azure DevOps 中启用 Docker 层缓存

转载 作者:行者123 更新时间:2023-12-03 09:49:27 25 4
gpt4 key购买 nike

我正在运行下面的 yaml 脚本来构建 docker 镜像并推送到 kubernetes 集群,但同时我想在构建 yaml 脚本时在 azure DevOps 中启用 docker 层缓存。你能解释一下如何启用或如何添加azure devops 中的任务来做到这一点。
Yaml:

# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

trigger:
- master

pool:
vmImage: 'ubuntu-latest'

variables:
tag: 'web'
DockerImageName: 'boiyaa/google-cloud-sdk-nodejs'


steps:
- task: Docker@2
inputs:
command: 'build'
Dockerfile: '**/Dockerfile'
tags: 'web'

- script: |
echo ${GCLOUD_SERVICE_KEY_STAGING} > ${HOME}/gcp-key.json
gcloud auth activate-service-account --key-file ${HOME}/gcp-key.json --project ${GCLOUD_PROJECT_ID_STAGING}
gcloud container clusters get-credentials ${GCLOUD_PROJECT_CLUSTER_ID_STAGING} \
--zone ${GCLOUD_PROJECT_CLUSTER_ZONE_STAGING} \
--project ${GCLOUD_PROJECT_ID_STAGING}
displayName: 'Setup-staging_credentials'


- bash: bash ./deploy/deploy-all.sh staging
displayName: 'Deploy_script_staging'

最佳答案

这是我解决这个问题的方法。我只是将最新版本的镜像从我的注册表(在我的例子中是 Azure 容器注册表)拉到 Azure DevOps 托管代理。然后我添加 --cache-from指向指向这个最新标签的 Docker 构建参数,它刚刚下载到本地机器/缓存。

- task: Docker@2
inputs:
containerRegistry: '$(ContainerRegistryName)'
command: 'login'

- script: "docker pull $(ACR_ADDRESS)/$(REPOSITORY):latest"
displayName: Pull latest for layer caching
continueOnError: true # for first build, no cache

- task: Docker@2
displayName: build
inputs:
containerRegistry: '$(ContainerRegistryName)'
repository: '$(REPOSITORY)'
command: 'build'
Dockerfile: './dockerfile '
buildContext: '$(BUILDCONTEXT)'
arguments: '--cache-from=$(ACR_ADDRESS)/$(REPOSITORY):latest'
tags: |
$(Build.BuildNumber)
latest

- task: Docker@2
displayName: "push"
inputs:
command: push
containerRegistry: "$(ContainerRegistryName)"
repository: $(REPOSITORY)
tags: |
$(Build.BuildNumber)
latest

关于azure-devops - 如何在 Azure DevOps 中启用 Docker 层缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59266670/

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