当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 在 Azure Pipelines 中缓存 PowerShell 模块

作者:精品下载站 日期:2024-12-14 07:13:52 浏览:12 分类:玩电脑

在 Azure Pipelines 中缓存 PowerShell 模块


介绍

这是一篇简短的文章,旨在演示如何使用 Azure DevOps (Azure Pipelines) 中的缓存来存储管道期间所需的任何 PowerShell 模块。

不要让您的管道依赖于 PowerShell Gallery 的可用性!

这确实适用于任何事情,但在这篇文章中我将重点关注 PowerShell Gallery。这个说法有两个简单的理由:

  1. 从库下载模块需要时间,这不是我每次运行管道时都想做的事情。
  2. 画廊可能不可用,我不希望这破坏我的管道。

由于这些原因,我想缓存每个管道执行之间的任何下载的模块或其他依赖项。公平地说,如果我也在我自己的私人画廊中托管所有依赖项,我可能会更好,但那是其他时间的帖子。

准备管道

我喜欢将将来或在项目或环境之间更改的任何可配置值设置为管道变量。这不仅减少了多次输入相同值的需要,而且当每个值仅存在一次并且所有可更改值都定义在同一位置时,更改值也变得更加容易。正如孩子们所说(或者他们确实这么做了?),保持干燥(不要重复自己)。

话虽这么说,让我们设置一些变量。我们需要一个地方来存储模块以及下载它们的脚本的路径。

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 添加一个后期任务,它将我们指定的文件夹上传到缓存。

我的管道第一次运行时将如下所示:

[玩转系统] 在 Azure Pipelines 中缓存 PowerShell 模块

我们可以看到下载 PowerShell 模块任务已运行,并且有一个后期作业将文件夹上传到我的缓存。

如果我再次运行相同的管道,它将如下所示:

[玩转系统] 在 Azure Pipelines 中缓存 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 *

让我知道您如何使用缓存!请在下面发表评论。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯