英文:
How to store variable in Azure YAML pipeline using a Powershell expression
问题
我管道中有两个阶段: 构建 (Microsoft Hosted) 和调用 (无代理)。在构建代码之后,我添加了一个 PowerShell 任务来获取一个 EXE 版本
```shell
- task: PowerShell@2
displayName: GetVar
inputs:
targetType: 'inline'
script: |
'$fileVersion = (Get-Item TargetEngine.Aspen.Services.Internal.JobScheduler\bin\Release\net6.0\TargetEngine.Aspen.Services.Internal.JobScheduler.exe).VersionInfo.FileVersion'
Write-Host 'Version: $fileVersion'
然而,我的 Write-Host 命令打印出一个空行。
我想将这个 $fileVersion 变量添加到 Azure Function 的查询参数中。
我用以下代码替换了脚本部分:文档
Write-Host "##vso[task.setvariable variable=FileVersion;isOutput=true](Get-Item TargetEngine.Aspen.Services.Internal.JobScheduler\bin\Release\net6.0\TargetEngine.Aspen.Services.Internal.JobScheduler.exe).VersionInfo.FileVersion"
Write-Host "Version: $FileVersion"
然而,它没有评估表达式,而是按原样写入,如下所示:
(Get-Item TargetEngine.Aspen.Services.Internal.JobScheduler\bin\Release\net6.0\TargetEngine.Aspen.Services.Internal.JobScheduler.exe).VersionInfo.FileVersion
我尝试了以下方式引用变量,但它没有起作用:
powershell: Write-Host "this is $env:Version"
我想获取 FileVersion 并将其用作 Azure Function 的另一个阶段的查询参数。
目前我正在手动将 Version 注入到管道代码中。
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
触发器:
分支:
包括:
- develop
- release
- main
变量:
solution: "**/*.sln"
buildPlatform: "Any CPU"
buildConfiguration: "Release"
阶段:
- 阶段: 构建和复制
池:
vmImage: windows-latest
任务:
- 任务: 构建
步骤:
- task: NuGetToolInstaller@1
displayName: 安装 Nuget 工具
inputs:
versionSpec: "6"
- task: NuGetCommand@2
displayName: 恢复解决方案包
inputs:
restoreSolution: "$(solution)"
- task: VSBuild@1
displayName: 构建解决方案
inputs:
solution: "$(solution)"
vsVersion: latest
msbuildArgs: '/p:SkipInvalidConfigurations=true /p:DeployOnBuild=true /p:WebPublishMethod=FileSystem /p:publishUrl="$(build.artifactstagingdirectory)\" /p:DeployDefaultTarget=WebPublish'
platform: "$(buildPlatform)"
configuration: "$(buildConfiguration)"
- powershell: Write-Host "##vso[task.setvariable variable=FileVersion;isOutput=true]2.0.0.0"
name: getVar
- powershell: Write-Host 'this is $(getVar.FileVersion)'
- task: PublishBuildArtifacts@1
displayName: "发布 API"
- 阶段: 调用 Azure 函数
任务:
- 任务: 调用
变量:
exeVersion: $[stageDependencies.BuildandCopy.build.outputs['getVar.FileVersion']]
池: server
步骤:
- task: AzureFunction@1
inputs:
function: 'URL'
key: 'mykey=='
method: 'POST'
queryParameters: 'Version=$(exeVersion)'
waitForCompletion: 'false'
英文:
I have two stages in the pipeline: build (Microsoft Hosted) and Invoke (agentless). After building the code, I added a PowerShell task to retrieve an EXE version
- task: PowerShell@2
displayName: GetVar
inputs:
targetType: 'inline'
script: |
'$fileVersion = (Get-Item TargetEngine.Aspen.Services.Internal.JobScheduler\bin\Release\net6.0\TargetEngine.Aspen.Services.Internal.JobScheduler.exe).VersionInfo.FileVersion'
Write-Host 'Version: $fileVersion'
However, my Write-Host command prints a blank line.
I want to add this $fileVersion variable in the Azure Function query parameters.
I replaced the script section with the following code: Docs
Write-Host "##vso[task.setvariable variable=FileVersion;isOutput=true](Get-Item TargetEngine.Aspen.Services.Internal.JobScheduler\bin\Release\net6.0\TargetEngine.Aspen.Services.Internal.JobScheduler.exe).VersionInfo.FileVersion"
Write-Host "Version: $FileVersion"
However, it doesn't evaluate the expression and instead writes it as it is, as shown below:
(Get-Item TargetEngine.Aspen.Services.Internal.JobScheduler\bin\Release\net6.0\TargetEngine.Aspen.Services.Internal.JobScheduler.exe).VersionInfo.FileVersion
I tried referring to the variable as follows, but it didn't work:
powershell: Write-Host "this is $env:Version"
I want to retrieve the FileVersion and use it in another stage for an Azure Function as query param.
For now I am manually injecting Version in the pipeline code.
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
trigger:
branches:
include:
- develop
- release
- main
variables:
solution: "**/*.sln"
buildPlatform: "Any CPU"
buildConfiguration: "Release"
stages:
- stage: BuildandCopy
pool:
vmImage: windows-latest
jobs:
- job: Build
steps:
- task: NuGetToolInstaller@1
displayName: Install Nuget tools
inputs:
versionSpec: "6"
- task: NuGetCommand@2
displayName: Restore Solution Packages
inputs:
restoreSolution: "$(solution)"
- task: VSBuild@1
displayName: Building Solution
inputs:
solution: "$(solution)"
vsVersion: latest
msbuildArgs: '/p:SkipInvalidConfigurations=true /p:DeployOnBuild=true /p:WebPublishMethod=FileSystem /p:publishUrl="$(build.artifactstagingdirectory)\\" /p:DeployDefaultTarget=WebPublish'
platform: "$(buildPlatform)"
configuration: "$(buildConfiguration)"
- powershell: Write-Host "##vso[task.setvariable variable=FileVersion;isOutput=true]2.0.0.0"
name: getVar
- powershell: Write-Host 'this is $(getVar.FileVersion)'
- task: PublishBuildArtifacts@1
displayName: "Publish APIs"
- stage: InvokeAzureFunction
jobs:
- job: Invoke
variables:
exeVersion: $[stageDependencies.BuildandCopy.build.outputs['getVar.FileVersion']]
pool: server
steps:
- task: AzureFunction@1
inputs:
function: 'URL'
key: 'mykey=='
method: 'POST'
queryParameters: 'Version=$(exeVersion)'
waitForCompletion: 'false'
答案1
得分: 0
在第一个 PowerShell 任务中,你似乎将 '$fileVersion = (Get-Item TargetEngine.Aspen.Services.Internal.JobScheduler\bin\Release\net6.0\TargetEngine.Aspen.Services.Internal.JobScheduler.exe).VersionInfo.FileVersion'
用单引号括起来。这样做只是声明了一个字符串,而没有将 .exe
的版本分配给一个变量。
$fileVersion
为空的原因是它从未被赋值,只在 Write-Host 命令期间创建了。
尝试移除单引号,看看是否可以工作。
更新:
你似乎还在 Write-Host
输出周围使用了单引号。
这是我运行的更新测试,它运行良好。
我只是将 .exe
路径更改为我的代理中有的内容。
displayName: GetVar
inputs:
targetType: 'inline'
script: |
cd "c:\Program Files\Microsoft SQL Server0\DAC\bin"
$fileVersion = (Get-Item SqlPackage.exe).VersionInfo.FileVersion
Write-Host "Version: $fileVersion"
日志
Starting: GetVar
==============================================================================
Task : PowerShell
Description : 在 Linux、macOS 或 Windows 上运行 PowerShell 脚本
Version : 2.220.0
Author : Microsoft Corporation
Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
==============================================================================
Generating script.
========================== Starting Command Output ===========================
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\fd8338df-b2d0-437a-90d5-aaad1b24b079.ps1'"
Version: 16.1.8089.0
Finishing: GetVar
英文:
In the first PowerShell task, you seem to have the '$fileVersion = (Get-Item TargetEngine.Aspen.Services.Internal.JobScheduler\bin\Release\net6.0\TargetEngine.Aspen.Services.Internal.JobScheduler.exe).VersionInfo.FileVersion'
encased in single quotes. By doing that, you are just declaring a string there and not assigning the .exe
version to a variable.
The reason the $fileVersion
is empty is that it is, as it was never assigned anything and has only been created during the Write-Host command.
Try removing the single quotes and see if that works.
Update:
You also seem to have single quotes around the Write-Host
output.
Here's an updated test that I ran and it works fine.
I just changed the .exe
path to something that I had on my agent.
displayName: GetVar
inputs:
targetType: 'inline'
script: |
cd "c:\Program Files\Microsoft SQL Server0\DAC\bin"
$fileVersion = (Get-Item SqlPackage.exe).VersionInfo.FileVersion
Write-Host "Version: $fileVersion"
Logs
Starting: GetVar
==============================================================================
Task : PowerShell
Description : Run a PowerShell script on Linux, macOS, or Windows
Version : 2.220.0
Author : Microsoft Corporation
Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
==============================================================================
Generating script.
========================== Starting Command Output ===========================
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\fd8338df-b2d0-437a-90d5-aaad1b24b079.ps1'"
Version: 16.1.8089.0
Finishing: GetVar
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论