在 yml 文件中添加缓存前,请确保在同一步骤中没有任何步骤将使用另一个不同的缓存键。例如,如果你有“Restore NuGet Packages”步骤和一个SetCache步骤,则需要使用相同的键名在同一步骤中使用它们。以下是一个示例:
task: PowerShell@2 displayName: Cache PowerShell modules inputs: targetType: 'inline' script: | $modulesFolderPath = '/path/to/powershell/modules'
if(Test-Path $(Build.SourcesDirectory)/.module_expiration) {
$timeout = [datetime]::ParseExact((Get-Content $(Build.SourcesDirectory)/.module_expiration), 'yyyyMMddHHmmss', $null) - (Get-Date)
Write-Host "Using cached PowerShell modules. Cache will timeout in $($timeout.ToString())."
if($timeout.TotalMinutes -le 0) {
Write-Host "Cache has timed out. Discarding cache."
} else {
Write-Host "Cache still valid. Skipping module restore."
Write-Host "##vso[task.setvariable variable=MODULES_FOLDER_PATH]$($modulesFolderPath)" #Set variable for downstream tasks.
exit 0
}
}
Write-Host "No cached PowerShell modules found. Restoring..."
# Restore modules here...
# Set cache for downstream jobs.
Write-Host "##vso[task.setvariable variable=CACHE_SCREAM_MODULE]true"
Write-Host "##vso[task.setvariable variable=MODULES_FOLDER_PATH]$($modulesFolderPath)"
task: Cache@2 displayName: Cache PowerShell modules folder inputs: key: 'powershellmodules | "$(BUILD.SOURCEVERSION)"' path: '$(MODULES_FOLDER_PATH)'
上一篇:AzureDevOps自托管代理上不支持使用System.Data.SqlClient
下一篇:AzureDevOps|E265blockcommentshouldstartwith''#''(linting)