如何在ADO管道中使用PowerShell模块?

huangapple go评论68阅读模式
英文:

How to Use Powershell Module in an ADO Pipeline?

问题

Sure, here's the translated content:

我有一个包含许多函数的 PowerShell .psm1 模块,我想在许多 Azure DevOps YAML 构建管道中使用它,以及可能在发布管道中使用。我想将该模块发布为 ADO Artiact 并在许多地方使用它。

目标:我认为我在这里的目标是使用命令 Register-PSRepository 然后使用 Install-Module 来使来自 feed 的模块内容作为 PSRepository 在后续步骤或任务中可用于管道中。我使用此 Microsoft Learn 教程 作为所有有效步骤的指南(如下),但尝试调整最后一步以在 Azure ADO 管道中运行。

问题:管道无法将包注册为 PSRepository。

有效步骤:

  1. 所有函数在 Axis.DevOpsLib.psm1 文件中定义和本地测试。
  2. 所有函数通过 Export-ModuleMember -Function 使其可见。
  3. 使用 nuget spec Axis.DevOpsLib 创建清单 - 这有效。
  4. 根据需要编辑了 .psd1 文件。
  5. 使用 nuget pack .\Axis.DevOpsLib.nuspec 创建了 nuspec 文件 - 这有效,创建了 nupkg 文件。
  6. 使用 nuget push Axis.DevOpsLib.1.0.0.nupkg -ApiKey AzureDevops -Source "Axis-Private" 将包发布到 ADO 私有 feed,该 feed 由我们的许多其他管道使用 - 这有效,预期中 nuget 包的正确版本显示在 feed 中。到目前为止,一切顺利。

(在下面的代码中,我意识到一旦这个工作,我需要从 Key Vault 中获取用户名和令牌,但在概念被证明之前,我会保持简单)

失败的部分:

variables:
  system.debug: true
  feed: https://pkgs.dev.azure.com/<org>/_packaging/Axis-Private/nuget/v2
  cred: '{"endpointCredentials": [{"endpoint":"$(feed)", "username":"<username>", "<patcleartext>":"ACCESS TOKEN"}]}'

pool:
  vmImage: 'ubuntu-latest'

steps:
  - task: AzurePowerShell@5
    displayName: 'register'
    inputs:
      azureSubscription: '<subscriptionid>'
      ScriptType: 'InlineScript'
      Inline: 'Register-PSRepository -Name Axis.DevOpsLib -SourceLocation "$(feed)" -InstallationPolicy Trusted -Credential "$(cred)" -Verbose'

结果:

-Verbose 模式当然会产生大量输出。在构建日志中,register 任务中出现了真正的问题的第一个迹象:

##[debug]Processed: ##vso[telemetry.publish area=TaskHub;feature=AzurePowerShellV5]{"moduleSource":null,"targetAzurePs":"null"}
Validate-VersionParameters: Cannot convert value 'null' to type 'System.Version'.
VERBOSE: The value of the module path is: /usr/share/az_null
VERBOSE: No module path found with this name
VERBOSE: The updated value of the PSModulePath is: /home/vsts/.local/share/powershell/Modules:/usr/local/share/powershell/Modules:/opt/microsoft/powershell/7/Modules
Exception: /home/vsts/work/_tasks/AzurePowerShell_72a1931b-effb-4d2e-8fd8-f8472a07cb62/5.220.0/Utility.ps1:94
Line |
  94 |  …             throw ("Could not find the module path with given version …
     |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Could not find the module path with given version.

我真的不知道如何解决这个问题,也不确定在哪里查找修复 VersionParameters 中的空值、奇怪的空模块路径,或者甚至我的方法是否正确或完整。有人可以建议如何解决这个错误吗?

英文:

I have a PowerShell .psm1 module that contains many functions I'd like to use in many Azure Devops yaml build pipelines, and maybe later in release pipelines as well. I want to publish the module as an ADO Artiact in a feed and use it in many places.

GOAL: I think my goal here is to use the commands Register-PSRepository and then Install-Module to make the module's contents from a feed as a PSRepository available to later steps or tasks in the pipeline. I was using this Microsoft Learn Tutorial as a guide for all the steps that work (below) but trying to adapt the final step to run in an Azure ADO pipeline.

PROBLEM: Pipeline fails to register the package as a PSRepository.

WHAT WORKS:

  1. All functions are defined and tested locally in the Axis.DevOpsLib.psm1 file
  2. All functions are made visible with Export-ModuleMember -Function
  3. I created a manifest using nuget spec Axis.DevOpsLib -- this works
  4. I edited the .psd1 file as necessary
  5. I created a nuspec file using nuget pack .\Axis.DevOpsLib.nuspec -- this works, nupkg file created
  6. I published the package to a ADO private feed which is used by many of our other pipelines using nuget push Axis.DevOpsLib.1.0.0.nupkg -ApiKey AzureDevops -Source "Axis-Private" -- this works, the nuget package of the correct versio shows up in the feed as expected. So far, so good.

(in code below, I realize that once this works I will need to grab username and token from the Key Vault, keeping it simple until concept is proved)

WHAT IS FAILING:

variables:
  system.debug: true
  feed: https://pkgs.dev.azure.com/<org>/_packaging/Axis-Private/nuget/v2
  cred: '{"endpointCredentials": [{"endpoint":"$(feed)", "username":"<username>", "<patcleartext>":"ACCESS TOKEN"}]}'

pool:
  vmImage: 'ubuntu-latest'

steps:
  - task: AzurePowerShell@5
    displayName: 'register'
    inputs:
      azureSubscription: '<subscriptionid>'
      ScriptType: 'InlineScript'
      Inline: 'Register-PSRepository -Name Axis.DevOpsLib -SourceLocation "$(feed)" -InstallationPolicy Trusted -Credential "$(cred)" -Verbose'

RESULTS:

-Verbose mode of course gives a lot of output. The first sign of real trouble in the build log in the register task:

##[debug]Processed: ##vso[telemetry.publish area=TaskHub;feature=AzurePowerShellV5]{"moduleSource":null,"targetAzurePs":"null"}
Validate-VersionParameters: Cannot convert value 'null' to type 'System.Version'.
VERBOSE: The value of the module path is: /usr/share/az_null
VERBOSE: No module path found with this name
VERBOSE: The updated value of the PSModulePath is: /home/vsts/.local/share/powershell/Modules:/usr/local/share/powershell/Modules:/opt/microsoft/powershell/7/Modules
Exception: /home/vsts/work/_tasks/AzurePowerShell_72a1931b-effb-4d2e-8fd8-f8472a07cb62/5.220.0/Utility.ps1:94
Line |
  94 |  …             throw ("Could not find the module path with given version …
     |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Could not find the module path with given version.

I really don't know what to make of this, not sure where to look to correct whatever is null with VersionParameters, the weirdly null module path, or even if my approach is correct or complete. Can anyone advise on how to find address this error?

答案1

得分: 1

你是否已经下载了NuGet并尝试在本地导入它?仅仅因为它已经构建并上传并不一定意味着内容实际上是有效的。

对我来说,错误消息似乎不是来自你提供的代码块,而可能是在尝试加载特定版本时出现的?


你也可以使用构建帐户进行NuGet身份验证并加载模块,而不是使用单独的服务帐户或用户凭据。要使此操作生效,您需要在NuGet feed上对构建帐户进行身份验证。

这对我来说有效,因为我总是希望使用最新版本。

英文:

Have you downloaded the nuget and tried to import it yourself locally? Just because it was built and uploaded does not necessarily mean the content is actually valid.

For me the error message does not seem to come from the code-block you provided but probably later when you try to load a specific version?


You could also use the build account to auth to nuget and load the module, instead of having a separate service account or user credentials. For this to work you will need to auth the build account on the nuget feed.

parameters:
  - name: ModuleName
    default: Module
  - name: nugetFeed
    default: nugetFeedName
  - name: nugetProject
    default: project
  - name: organization
    default: organization

steps:
  - powershell: |
      $patToken = "$(System.AccessToken)" | ConvertTo-SecureString -AsPlainText -Force 
      $credential = New-Object System.Management.Automation.PSCredential("$(System.AccessToken)", $patToken) 
      try {
        Register-PackageSource -ProviderName 'PowerShellGet' -Name ${{ parameters.nugetFeed }} -Location "https://pkgs.dev.azure.com/${{ parameters.organization }}/${{ parameters.nugetProject }}/_packaging/${{ parameters.nugetFeed }}/nuget/v2" -Credential $credential
        Install-Module -Name ${{ parameters.ModuleName }} -Credential $credential -AllowClobber -Force -Verbose
        Write-Output "Force importing module"
        Import-Module ${{ parameters.ModuleName }} -Force -Verbose
      } catch {
        throw $_
      }
    displayName: Import modules
    name: importmodules

This works for me because I always want to use the latest version.

huangapple
  • 本文由 发表于 2023年5月18日 00:19:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274218.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定