本文介绍了如何在Azure DevOps中启用Docker层缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行以下yaml脚本来构建docker映像并将其推入kubernetes集群,但与此同时,我想在构建yaml脚本时在azure DevOps中启用docker层缓存.将任务添加到Azure开发人员中以执行此操作.

Yaml:

 #启动程序管道#从最小的管道开始,您可以自定义管道以构建和部署代码.#添加构建,运行测试,部署等步骤:#https://aka.ms/yaml扳机:- 掌握水池:vmImage:'ubuntu-latest'变量:标记:网络"DockerImageName:'boiyaa/google-cloud-sdk-nodejs'脚步:-任务:Docker @ 2输入:命令:"build"Dockerfile:'**/Dockerfile'标签:网络"-脚本:|回声$ {GCLOUD_SERVICE_KEY_STAGING}>$ {HOME}/gcp-key.jsongcloud auth激活服务帐户--key文件$ {HOME}/gcp-key.json --project $ {GCLOUD_PROJECT_ID_STAGING}gcloud容器集群获取凭证$ {GCLOUD_PROJECT_CLUSTER_ID_STAGING} \--zone $ {GCLOUD_PROJECT_CLUSTER_ZONE_STAGING} \--project $ {GCLOUD_PROJECT_ID_STAGING}displayName:设置阶段性凭证"-bash:bash ./deploy/deploy-all.sh暂存displayName:'Deploy_script_staging' 
解决方案

azure devops当前不支持Docker层缓存.

I am running the below yaml script to build docker images and push into kubernetes cluster but at the same time I wanted to enable docker layer caching in the azure DevOps while building the yaml script.Could you please explain how to enable or how to add the task in azure devops to do this.

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'
解决方案

Docker layer caching is not supported in azure devops currently. The reason is stated as below:

1,However, Docker layer caching is possible using self-hosted agents. You can try creating your on-premise agents to run your build pipeline.

You may need to disable the Job's option 'Allow scripts to access the OAuth token'. For $(System.AccessToken) is passed to docker build using a --build-arg ACCESS_TOKEN=$(System.AccessToken), and its value varies for every run, which will invalid the caching.

2,You can also you use Cache task and docker save/load commonds to upload the saved docker layer to azure devops server and restore it on the future run. Check this thread for more information.

3,Another workaround as described in this blog is to use --cache-from and --target in your dockerfile.

If above workaround is not satisfying. You can submit a feature request to Microsoft Develop Team. Click Suggest a Feature and choose Azure Devops.

这篇关于如何在Azure DevOps中启用Docker层缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 20:20