1
votes

I have a pipeline that runs two stages. The stages are essentially identical, except one depends on the other. They both reference a template that contains two jobs, one job depending on the other. The first job creates an output variable and the second job consumes it.

The problem is that there are two JobA's and two JobB's and JobB doesn't seem to know which JobA is the correct one. Here's the YAML:

# azure-pipelines.yaml
stages:
  - stage: deployQA
    jobs:
    - template: stage-template.yaml
      parameters:
        environment: QA
  - stage: deployStaging
    dependsOn: deployQA
    condition: succeeded()
    jobs:
    - template: stage-template.yaml
      parameters:
        environment: STAGING
# stage-template.yaml
parameters:
  environment: ''
jobs:
- job: preDeploy
  variables:
    artifactName: preDeploy-${{ parameters.environment }}
    environment: ${{ parameters.environment }}
  steps:
  - checkout: none
  - publish: $(Pipeline.Workspace)
    artifact: $(artifactName)
  - pwsh: |
      echo "##vso[task.setvariable variable=artifactName;isOutput=true]$($env:ARTIFACTNAME)"
    name: outputVars
- job: deployment
  dependsOn: preDeploy
  variables:
    artifactName: $[dependencies.preDeploy.outputs['outputVars.artifactName']]
  steps:
  - checkout: none
  - download: current
    artifact: $(artifactName)

The problem is toward the bottom of the second file on this line:

artifactName: $[dependencies.preDeploy.outputs['outputVars.artifactName']]

When the QA stage is run, $[dependencies.preDeploy.outputs['outputVars.artifactName']] resolves to preDeploy-QA and when the Staging stage is run, it also resolves to preDeploy-QA. Looking at the logs, I can see them being resolved incorrectly:

// 2_deployment.txt
Variables:
  artifactName:
    Parsing expression: <dependencies.preDeploy.outputs['outputVars.artifactName']>
    Evaluating: dependencies['preDeploy']['outputs']['outputVars.artifactName']
    Result: 'preDeploy-QA'

// 2_deployment (1).txt
Variables:
  artifactName:
    Parsing expression: <dependencies.preDeploy.outputs['outputVars.artifactName']>
    Evaluating: dependencies['preDeploy']['outputs']['outputVars.artifactName']
    Result: 'preDeploy-QA'

I am doing something wrong here? Or is this a bug?

You can see the YAML and logs [here][1].

  [1]: https://dev.azure.com/lolinctest/PublicTest/_build/results?buildId=506&view=results
1

1 Answers

1
votes

I am doing something wrong here? Or is this a bug?

No, you did not do any thing wrong, refer to this issue ticket which report on our Developer Community recently: Setting output variable when multiple stages share a job results in unexpected sharing. Just while the multi stage share the the same job(template), the value will be confusing.

This is the issue that the Product Group has identified it as caused by our side. We have finished the investigate, the fixed script is pending release now.

But, for not to affect the normal build, there has a workaround, we can avoid to use this issue variable expression.

Just reconstruct the artifact name with another method:

  - download: current
    artifact: preDeploy-${{ parameters.environment }}

enter image description here

Since it's correct to get the value from parameters.environment, so we can construct the artifact name with these script in the Download task.

On our backend, the fixed code has been committed and PR is finished. Just patience for waiting, we will deploy these fixed recently. You can also track that ticket which reported in Developer Community. Once it deployed, our team engineer will inform it there at first time.