goosseno
goosseno

Reputation: 175

Azure pipelines bash tries to execute the variable instead of expanding

This is really stupid but was driving me crazy for a couple of hours. I'm testing how to pass variables between Powershell and Bash. Relevant code:

      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)

And I keep getting errors like:

/d/a/_temp/b40e64e8-8b5f-42d4-8118-82e8cf8a28c2.sh: line 1: LastCommit: command not found

I tried with all kinds of quotes, double, simple, none. Nothing works.

Upvotes: 2

Views: 2507

Answers (2)

Krzysztof Madej
Krzysztof Madej

Reputation: 40553

If you want to use echo $(LastCommit)

then you just need to remove isOutput

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

And with isOutput you need to reference via task name

      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)

Upvotes: 4

goosseno
goosseno

Reputation: 175

Solution:

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

Turns out that the "isOutput" was breaking it, as it means you I was creating a multi-job output variable and trying to use it inside the same job.

From the official documentation:

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.

When you creating a multi-job output variable, you should assign the expression to a variable. For example:

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

Upvotes: 2

Related Questions