这真的很愚蠢,但让我疯狂了几个小时。我正在测试如何在 Powershell 和 Bash 之间传递变量。相关代码:
steps:
- task: PowerShell@2
name: 'pwsh_script'
inputs:
targetType: 'inline'
script: |
$response = "6458ddcd4edd7b7f68bec10338d47b55d221e975"
echo "latest (harcoded) commit: $response"
Write-Host "##vso[task.setvariable variable=LastCommit;isOutput=True]$response"
- task: Bash@3
name: 'bash_script1'
inputs:
targetType: 'inline'
script: |
echo $(LastCommit)
而且我不断收到以下错误:
<块引用>/d/a/_temp/b40e64e8-8b5f-42d4-8118-82e8cf8a28c2.sh: line 1: LastCommit: command not found
我尝试了各种引号,双引号、简单引号、无引号。没有任何效果。
答案 0 :(得分:0)
解决方案:
+ Write-Host "##vso[task.setvariable variable=LastCommit;isOutput=True]$response"
- Write-Host "##vso[task.setvariable variable=LastCommit;]$response"
结果是“isOutput”破坏了它,因为这意味着我正在创建一个多作业输出变量并尝试在同一个作业中使用它。
来自官方文档:
<块引用>如果您想让一个变量在以后的工作中可用,您必须使用 isOutput=true 将其标记为输出变量。然后,您可以通过使用 $[] 语法并包括设置变量的步骤名称将其映射到未来的作业中。多作业输出变量仅适用于同一阶段的作业。
要将变量传递给不同阶段的作业,请使用阶段依赖语法。
创建多作业输出变量时,应将表达式分配给变量。 例如:
myVarFromJobA: $[ dependencies.A.outputs['setvarStep.myOutputVar'] ] # map in the variable
答案 1 :(得分:0)
如果您想使用 echo $(LastCommit)
那么你只需要删除 isOutput
Write-Host "##vso[task.setvariable variable=LastCommit]$response"
使用 isOutput
您需要通过任务名称引用
steps:
- task: PowerShell@2
name: 'pwsh_script'
inputs:
targetType: 'inline'
script: |
$response = "6458ddcd4edd7b7f68bec10338d47b55d221e975"
echo "latest (harcoded) commit: $response"
Write-Host "##vso[task.setvariable variable=LastCommit;isOutput=True]$response"
- task: Bash@3
name: 'bash_script1'
inputs:
targetType: 'inline'
script: |
echo $(pwsh_script.LastCommit)