英文:
AzureDevops pipeline task with condition not evaluated correctly
问题
我有一个流水线,用于检查提交是否有标签,并且该标签是否是版本标签(即类似于'v1.0.0'或'v01.00.00')。
我的问题是,当我将正则表达式操作的结果保存在变量中时,它似乎首先是True
,但在第二个任务的condition
中使用时突然变为False
。
以下是该流水线的代码:
# 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:
- master
tags:
include:
- "*"
pool:
vmImage: windows-latest
variables:
hasTags: $[contains(variables['Build.SourceBranch'], 'refs/tags/')]
isVersionTag: 'False'
versionTag: ''
steps:
- task: PowerShell@2
condition: eq(variables.hasTags, 'True')
inputs:
targetType: 'inline'
script: |
# get the tag the from the SourcheBranch
$dummy = $Env:Build_SourceBranch
$regex = "(refs)/(tags)/(?<version_tag>.+)$"
$result = [regex]::Matches($dummy, $regex)
$Env:versionTag = $result[0].Groups["version_tag"].Value
# check if the tag is actually a version (e.g. 'v1.0.1')
$regex = "v[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}$"
$Env:isVersionTag = [regex]::IsMatch($Env:versionTag, $regex)
if($Env:isVersionTag -eq 'True')
{
Write-Host 'It is a version tag'
}
Write-Host "Variable Build.SourceBranch = $Env:Build_SourceBranch"
Write-Host "Variable isVersionTag = $Env:isVerionTag"
Write-Host "Variable hasTags = $Env:hasTags"
Write-Host "Variable versionTag = $Env:versionTag"
# This will not run because the condition is evaluated as the following:
# Evaluating: and(eq(variables['hasTags'], 'True'), eq(variables['isVersionTag'], 'True'))
# Expanded: and(eq('True', 'True'), eq('False', 'True'))
# Result: False
- task: PowerShell@2
condition: and(eq(variables.hasTags, 'True'), eq(variables.isVersionTag, 'True'))
inputs:
targetType: 'inline'
script: |
# Write your PowerShell commands here.
Write-Host "The tag is a version tag"
上述流水线的Write-Host
输出如下:
It is a version tag
Variable Build.SourceBranch = refs/tags/v02.01.07
Variable isVersionTag =
Variable hasTags = True
Variable versionTag = v02.01.07
英文:
I have a pipeline which checks whether a commit has a tag and if that tag is a Version tag (meaning it is like 'v1.0.0' or 'v01.00.00').
My problem is that when I save the result of the regex operation in a variable, it appears to first be True
but when using it in the condition
of the second task it is suddenly False
.
Here is the pipeline:
# 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:
- master
tags:
include:
- "*"
pool:
vmImage: windows-latest
variables:
hasTags: $[contains(variables['Build.SourceBranch'], 'refs/tags/')]
isVersionTag: 'False'
versionTag: ''
steps:
- task: PowerShell@2
condition: eq(variables.hasTags, 'True')
inputs:
targetType: 'inline'
script: |
# get the tag the from the SourcheBranch
$dummy = $Env:Build_SourceBranch
$regex = "(refs)\/(tags)\/(?<version_tag>.+)$"
$result = [regex]::Matches($dummy, $regex)
$Env:versionTag = $result[0].Groups["version_tag"].Value
# check if the tag is actually a version (e.g. 'v1.0.1')
$regex = "v[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}$"
$Env:isVersionTag = [regex]::IsMatch($Env:versionTag, $regex)
if($Env:isVersionTag -eq 'True')
{
Write-Host 'It is a version tag'
}
Write-Host "Variable Build.SourceBranch = $Env:Build_SourceBranch"
Write-Host "Variable isVersionTag = $Env:isVerionTag"
Write-Host "Variable hasTags = $Env:hasTags"
Write-Host "Variable versionTag = $Env:versionTag"
# This will not run because the condition is evaluated as the following:
# Evaluating: and(eq(variables['hasTags'], 'True'), eq(variables['isVersionTag'], 'True'))
# Expanded: and(eq('True', 'True'), eq('False', 'True'))
# Result: False
- task: PowerShell@2
condition: and(eq(variables.hasTags, 'True'), eq(variables.isVersionTag, 'True'))
inputs:
targetType: 'inline'
script: |
# Write your PowerShell commands here.
Write-Host "The tag is a version tag"
The output of the Write-Host
of the above pipeline is the following:
It is a version tag
Variable Build.SourceBranch = refs/tags/v02.01.07
Variable isVersionTag =
Variable hasTags = True
Variable versionTag = v02.01.07
答案1
得分: 1
在PowerShell中,环境变量只包含字符串,没有布尔值。在你的情况下,它们包含的要么是字符串"True",要么是字符串"False"。无论哪种情况,它们都包含某些内容,而第二个示例中的表达式$Env:isVersionTag
返回的是"False",这仍然被认为是真值。相反,你可以使用类似于if($Env:isVersionTag -eq "True")
的语句,这样应该可以工作。
要在任何PowerShell窗口中验证这种行为,你可以执行以下操作:
$Env:testbool = $false
Write-Host $Env:testbool.GetType().FullName
英文:
The environment variables in PowerShell only contain strings, no booleans. In your case, they contain either the string "True" or the string "False". Either way, they contain something and the expression $Env:isVersionTag
in the second example returns "False" which still is considered trueish. Use something like if($Env:isVersionTag -eq "True")
instead, this should work.
To verify this behavior in any PowerShell window, you can do this:
$Env:testbool = $false
Write-Host $Env:testbool.GetType().FullName
答案2
得分: 1
我最终将以下代码替换为:
if([regex]::IsMatch($Env:versionTag, $regex))
{
Write-Host "##vso[task.setvariable variable=isVersionTag]True"
}else{
Write-Host "##vso[task.setvariable variable=isVersionTag]True"
}
然后它按预期工作了。
英文:
I ended up replacing
$Env:isVersionTag = [regex]::IsMatch($Env:versionTag, $regex)
with
if([regex]::IsMatch($Env:versionTag, $regex))
{
Write-Host "##vso[task.setvariable variable=isVersionTag]True"
}else{
Write-Host "##vso[task.setvariable variable=isVersionTag]True"
}
and then it worked as expected.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论