Azure 管道 bash 尝试执行变量而不是扩展

Azure pipelines bash tries to execute the variable instead of expanding

这真的很愚蠢,但让我发疯了几个小时。我正在测试如何在 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

我尝试了各种引号,双引号,简单引号,none。没有任何效果。

解决方案:

+              Write-Host "##vso[task.setvariable variable=LastCommit;isOutput=True]$response"
-              Write-Host "##vso[task.setvariable variable=LastCommit;]$response"

结果是“isOutput”破坏了它,因为这意味着你我正在创建一个多作业输出变量并试图在同一个作业中使用它。

来自官方文档:

If you want to make a variable available to future jobs, you must mark it as an output variable by using isOutput=true. Then you can map it into future jobs by using the $[] syntax and including the step name that set the variable. Multi-job output variables only work for jobs in the same stage.

To pass variables to jobs in different stages, use the stage dependencies syntax.

创建多作业输出变量时,应将表达式分配给变量。 例如:

myVarFromJobA: $[ dependencies.A.outputs['setvarStep.myOutputVar'] ] # map in the variable

如果你想使用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)