[玩转系统] 在 Azure Pipelines 中缓存 PowerShell 模块
作者:精品下载站 日期:2024-12-14 07:13:52 浏览:12 分类:玩电脑
在 Azure Pipelines 中缓存 PowerShell 模块
介绍
这是一篇简短的文章,旨在演示如何使用 Azure DevOps (Azure Pipelines) 中的缓存来存储管道期间所需的任何 PowerShell 模块。
不要让您的管道依赖于 PowerShell Gallery 的可用性!
这确实适用于任何事情,但在这篇文章中我将重点关注 PowerShell Gallery。这个说法有两个简单的理由:
- 从库下载模块需要时间,这不是我每次运行管道时都想做的事情。
- 画廊可能不可用,我不希望这破坏我的管道。
由于这些原因,我想缓存每个管道执行之间的任何下载的模块或其他依赖项。公平地说,如果我也在我自己的私人画廊中托管所有依赖项,我可能会更好,但那是其他时间的帖子。
准备管道
我喜欢将将来或在项目或环境之间更改的任何可配置值设置为管道变量。这不仅减少了多次输入相同值的需要,而且当每个值仅存在一次并且所有可更改值都定义在同一位置时,更改值也变得更加容易。正如孩子们所说(或者他们确实这么做了?),保持干燥(不要重复自己)。
话虽这么说,让我们设置一些变量。我们需要一个地方来存储模块以及下载它们的脚本的路径。
variables:
modulesFolder: '$(System.DefaultWorkingDirectory)/powershellmodules'
restoreModulesScript: '$(Build.Repository.LocalPath)/scripts/restoremodules.ps1'
完成后,我们需要编写脚本来下载模块。在我的简单示例中,我将使用模块 ModuleBuilder 作为示例。我的脚本将采用一个路径参数来保存模块。然后我将使用 Save-Module
从 PowerShell Gallery 下载模块。
这里准确指定我们想要的每个模块的版本非常重要,因为我将使用此脚本的哈希和作为标识缓存数据的密钥的一部分。这意味着如果我们更改文件,缓存将失效,并且脚本将运行以再次下载模块。
如果您有许多或复杂的依赖项,我建议将下载它们的脚本与配置分开。在这些情况下,我通常有一个包含配置的 dependency.json
或 psd1 文件,并且我使用脚本和配置文件来计算哈希密钥。稍后会详细介绍。这是我将用于示例的脚本:
param(
$Path
)
if(-not (Test-Path -Path $Path)) {
$null = New-Item -Path $Path -ItemType Directory
}
Save-Module -Name ModuleBuilder -RequiredVersion '2.0.0' -Path $Path
使用缓存任务
这根本不像听起来那么难! Azure DevOps Pipelines 中有一个用于缓存文件夹的内置任务!它简称为“缓存”,它在 Microsoft 文档中进行了记录。
我们只是这样使用它:
- task: Cache@2
displayName: Cache Powershell Modules
# This task will restore modules from cache if key is found.
# If contents of restoremodules.ps1 changes, key changes and cache is not restored.
# If cache is restored, variable PSModules_IsCached is set to true
inputs:
key: restoremodules | ${{ variables.restoreModulesScript }}
path: ${{ variables.modulesFolder }}
cacheHitVar: PSModules_IsCached
缓存任务需要三个输入:键、路径和变量名。让我们从文档中看一下它们的定义。
缓存的键(唯一标识符)
这应该是一个可以使用“|”分割的字符串。文件路径可以是绝对路径,也可以是相对于 $ (System.DefaultWorkingDirectory) 的路径。
要缓存的文件夹路径
可以是完全限定的或相对于 $ (System.DefaultWorkingDirectory)。不支持通配符。支持变量。
缓存命中变量
当缓存恢复(缓存命中)时设置为“true”的变量,否则设置为“false”。
在这里您可以看到我们正在设置缓存的密钥。就像文档中所说的那样,我们可以使用用管道字符分隔的文件的字符串或路径。在此示例中,我将字符串 restoremodules
与脚本路径结合使用。如果我将配置放在单独的文件中,我也会在此处添加该文件的路径。
该任务的输出将显示密钥是如何解析的,如下所示:
下载依赖项的任务
接下来我们需要在任务中运行我们的restoremodules.ps1 脚本。仅当模块尚未从缓存中恢复时才应运行此任务。这是通过添加一个要求管道变量 PSModules_IsCached 不为 true 的条件来解决的。这是它的 yaml:
- task: PowerShell@2
displayName: 'Download Powershell Modules'
# This task runs my restoremodules.ps1 script if the cache was not restored.
condition: ne(variables.PSModules_IsCached, 'true')
inputs:
targetType: filePath
filePath: ${{ variables.restoreModulesScript }}
arguments: -Path ${{ variables.modulesFolder }}
pwsh: true
这部分是相当不言自明的。现在我们已经拥有了运行缓存所需的一切!
为了演示这是如何工作的,我将添加一个额外的任务,该任务仅在缓存恢复时运行,如下所示:
- task: PowerShell@2
displayName: 'Log that cached modules were used'
# This task will only run if modules were restored from cache
# This task is pointless, just serves to show that cache is working
condition: eq(variables.PSModules_IsCached, 'true')
inputs:
pwsh: true
targetType: 'inline'
script: |
Write-Verbose -Message 'Using cached modules' -Verbose
Get-ChildItem -Path ${{ variables.modulesFolder }}
加载下载的模块
由于我选择将模块下载到它们自己的文件夹中,PowerShell 将不知道它们在那里,因此不会自动加载它们。这意味着我需要使用模块的路径加载模块,或者告诉 PowerShell 在哪里可以找到它们。我可以告诉 PowerShell 通过将它们添加到环境变量 PSModulePath
这里来查找它们,我希望有一个任务只为所有剩余任务设置 PSModulePath 变量在我的管道中,但我还没有找到一个好的方法来做到这一点,所以我只是在需要加载模块的每个任务中修改 PSModulePath(通常不是那么多)。
看起来是这样的:
- task: PowerShell@2
displayName: 'Load modules'
# This would be where your actual pipeline starts, I will just load a module to show that it works
inputs:
pwsh: true
targetType: 'inline'
script: |
$Env:PSModulePath = '${{ variables.modulesFolder }}', $Env:PSModulePath -join [System.IO.Path]::PathSeparator
Write-Host $Env:PSModulePath
Import-Module "ModuleBuilder"
Get-Module ModuleBuilder | Format-List -Property *
概括
总而言之,如果我们想要缓存 PowerShell 模块(或任何真正的东西),我们可以添加一个缓存任务来查找我们的缓存内容。缓存任务会自动向我们的 pipeline 添加一个后期任务,它将我们指定的文件夹上传到缓存。
我的管道第一次运行时将如下所示:
我们可以看到下载 PowerShell 模块任务已运行,并且有一个后期作业将文件夹上传到我的缓存。
如果我再次运行相同的管道,它将如下所示:
这里跳过了“下载 PowerShell 模块”任务,我们节省了整整 11 秒!这并不能节省很多时间,因为在本例中它是一个非常小的模块,但我们也知道我们的管道将在不依赖于可用的 PowerShell Gallery 的情况下运行。
我的整个管道 yaml 如下所示:
trigger:
- main
pool:
vmImage: ubuntu-latest
variables:
modulesFolder: '$(System.DefaultWorkingDirectory)/powershellmodules'
restoreModulesScript: '$(Build.Repository.LocalPath)/scripts/restoremodules.ps1'
steps:
- task: Cache@2
displayName: Cache Powershell Modules
# This task will restore modules from cache if key is found.
# If contents of restoremodules.ps1 changes, key changes and cache is not restored.
# If cache is restored, variable PSModules_IsCached is set to true
inputs:
key: restoremodules | ${{ variables.restoreModulesScript }}
path: ${{ variables.modulesFolder }}
cacheHitVar: PSModules_IsCached
- task: PowerShell@2
displayName: 'Download Powershell Modules'
# This task runs my restoremodules.ps1 script if the cache was not restored.
condition: ne(variables.PSModules_IsCached, 'true')
inputs:
targetType: filePath
filePath: ${{ variables.restoreModulesScript }}
arguments: -Path ${{ variables.modulesFolder }}
pwsh: true
- task: PowerShell@2
displayName: 'Log that cached modules were used'
# This task will only run if modules were restored from cache
# This task is pointless, just serves to show that cache is working
condition: eq(variables.PSModules_IsCached, 'true')
inputs:
pwsh: true
targetType: 'inline'
script: |
Write-Verbose -Message 'Using cached modules' -Verbose
Get-ChildItem -Path ${{ variables.modulesFolder }}
- task: PowerShell@2
displayName: 'Load modules'
# This would be where your actual pipeline starts, I will just load a module to show that it works
inputs:
pwsh: true
targetType: 'inline'
script: |
$Env:PSModulePath = '${{ variables.modulesFolder }}', $Env:PSModulePath -join [System.IO.Path]::PathSeparator
Write-Host $Env:PSModulePath
Import-Module "ModuleBuilder"
Get-Module ModuleBuilder | Format-List -Property *
让我知道您如何使用缓存!请在下面发表评论。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag