Azure DevOps 管道任务从私有代理上的云端下载缓存

Azure DevOps pipeline task downloads cache from cloud on private agent

在 azure devops 中,我们有一个管道 运行 在不同 mac mac 网络上的几个私有托管代理上。我们定义了一个缓存任务来防止重新下载依赖:

- task: Cache@2
  displayName: 'Cache gradle distribution'
  inputs:
    key: 'gradleCache'
    path: 'gradle-6.8'
    cacheHitVar: GRADLE_CACHE_HIT

- script: |
    curl -s https://downloads.gradle-dn.com/distributions/gradle-6.8-bin.zip --output gradle-6.8-bin.zip
    unzip -q gradle-6.8-bin.zip  # a folder named gradle-6-8 will be created
  condition: eq(variables.GRADLE_CACHE_HIT, 'false')
  displayName: 'Download and install gradle'

- script: |
    export PATH=`pwd`/gradle-6.8/bin:$PATH
    gradle -v
  displayName: 'Gradle version'  

在第二个 运行 缓存处于活动状态,但缓存文件是从 Web 下载的,这错过了缓存点。是否可以实现“本地”缓存?

当缓存任务报告“缓存命中”时,缓存文件从管道的第二个 运行 上的 azure devops 服务器下载是默认行为。

在第一个 运行 上,将从您在缓存任务的 path 字段(即 gradle-6.8)中指定的文件夹中的文件创建缓存,并上传到 azure devops 服务器。

第二次 运行,当缓存任务报告“缓存命中”时。缓存文件将从 azure devops 服务器下载到缓存任务中指定的路径 gradle-6.8

使用缓存任务不是您的场景的最佳解决方案。由于您使用的是私有托管代理,因此您可以使用以下解决方法来完全跳过重新下载 gradle 工具。

1,您可以将 gradle-6.8 预安装到托管您的私人托管代理的 mac mac 内地。这样就不需要在您的管道中下载和安装 gradle。

2,您可以将 gradle-6.8 下载到私有托管代理 $(System.DefaultWorkingDirectory) 以外的其他地方,这可能会在未来的管道中自动清理 运行 .

例如在 windows machines 中:在管道的第一个 运行 中。您可以将 gradle-6.8 保存到 D:\custom\folder\forPipelinetool 而不是 C:\Agent\_work\s(即 $(System.DefaultWorkingDirectory))。

然后 gradle-6.8 将保存在代理 machines 上的不同位置。并且后面的运行里就不用下载gradle-6.8了。然后,您可以禁用下载 gradle-6.8.

的脚本任务