Home > database >  Aure pipeline : use output variable in multi-stage parametrised
Aure pipeline : use output variable in multi-stage parametrised

Time:05-06

I am facing an issue I could not resolve.

regarding this documentation it seams to be possible to set a variable as output of a job in the first stage, then use it as input to set a job variable in second stage.

The pbm I'm facing, is that my stages are parametrised, so their name are not constant.

here my sample code :

name: mypipeline

parameters:
  - name: Application
    displayName: Application to deploy the front end using front door
    type: string
  - name: stage_vars
    displayName: stage VARS
    type: object
    default:
    - stage_1
    - stage_2
    - none
trigger: none

stages:
  - stage: Build
    displayName: ${{ parameters.Application }} - Build.
    pool:
      name: Azure Pipelines
      vmImage: windows-2019
    jobs:
    - template: ../templates/job_BuildAndPublish.yml
      parameters:
        BuildId:  $(Build.BuildId)
        importTerratest: false
        importARM: true

  - ${{ each stageregion in parameters.stage_vars }}:
    - ${{ if ne(stageregion, 'none') }}:
        - template: ../templates/isolated_web/tf_plan_stage.yml
          parameters:
            BuildId: $(system.BuildId)
            Predecessors: 'Build'

        - template: ../templates/isolated_web/tf_apply_stage.yml
          parameters:
            Predecessors: '${{stageregion}}_prepare'

build template is not usefull, but the ones from tf_plan_stage and tf_apply_stage are below :

tf_plan_stage.yml :

parameters:
  - name: BuildId
    type: string
    default: $(system.BuildId)
  - name: stage
    type: string
    default: Deploy_prepare
  - name: Predecessors
    type: string
    default: none
stages:
  - stage: ${{ parameters.stage }}
    ? ${{ if and(ne(parameters.Predecessors, 'previous'), ne(parameters.Predecessors, 'none') ) }}
    : dependsOn: ${{parameters.Predecessors}}
    displayName: ${{ parameters.TF_VAR_STAGE }} Terraform plan & publish artifact for ${{ parameters.TF_VAR_APPLICATION }}
    pool:
      name: Azure Pipelines
      vmImage: windows-2019
    jobs:
    - deployment: PreDeployTerraform
      displayName: ${{ parameters.TF_VAR_STAGE }} Terraform plan & publish artifact     
      environment: PLAN_${{ parameters.TF_VAR_ENVIRONMENT }}
      timeoutInMinutes: 480
      strategy:
        runOnce:
          deploy:
            steps:
            - checkout: none
            # set output planAttempt output available for later Apply job
            - powershell: | 
                echo "jobAttempt is $(System.JobAttempt)"
                echo "##vso[task.setvariable variable=planAttempt;isOutput=true;]$(System.JobAttempt)"
              name: setVarJobAttempt
            - powershell: | 
                echo "variable setVarJobAttempt.planAttempt value is : $(setVarJobAttempt.planAttempt)"
              name: getplanAttemptVar

tf_apply_stage.yml :

    parameters:
  - name: stage
    type: string
    default: Deploy_prepare
  - name: Predecessors
    type: string
    default: none
stages:
  - stage: ${{ parameters.stage }}
    ? ${{ if and(ne(parameters.Predecessors, 'previous'), ne(parameters.Predecessors, 'none') ) }}
    : dependsOn: ${{parameters.Predecessors}}
    displayName: ${{ parameters.TF_VAR_STAGE }} download artifact & Terraform apply changes to ${{ parameters.TF_VAR_APPLICATION }}
    pool:
      name: Azure Pipelines
      vmImage: windows-2019
    jobs:
    - deployment: DeployTerraform
      displayName: ${{ parameters.TF_VAR_STAGE }} download artifact & Terraform apply changes
      variables:
        # this one fails in pipeline : "[not recognise"
        planJobAttempt: $[ stageDependencies.[parameters.Predecessors].PreDeployTerraform.outputs['setVarJobAttempt.planAttempt'] ]
        # this one runs with no result in pipeline : planJobAttempt is the string "stageDependencies['parameters.Predecessors'].PreDeployTerraform.outputs['setVarJobAttempt.planAttempt']"
        # planJobAttempt: $[ stageDependencies['parameters.Predecessors'].PreDeployTerraform.outputs['setVarJobAttempt.planAttempt'] ]
      environment: ${{ parameters.TF_VAR_ENVIRONMENT }}
      timeoutInMinutes: 480
      strategy:
        runOnce:
          deploy:
            steps:
              - checkout: none
              
              - powershell: | 
                  echo "jobAttempts outputs are :  $[ stageDependencies.[parameters.Predecessors].PreDeployTerraform.outputs ]"

              - powershell: | 
                  echo "jobAttempt is $[ stageDependencies['parameters.Predecessors'].PreDeployTerraform.outputs['setVarJobAttempt.planAttempt'] ]"

              - powershell: | 
                  echo "plan file for terraform is : $(pipeline_artifact_folder_download)/$(planJobAttempt)_${{ parameters.Predecessors }}/$(artefact_terraform_plan)_${{ parameters.TF_VAR_STAGE }}"

I tried differents things to get my system.jobAttempts from tf_plan_stage stage to tf_apply_stage stage, but without any success, as the variable in the last stage (tf_apply) seams unable to find the value from a "parametrised" stage name.

is there a way for that ?

thank-you for any answers.

CodePudding user response:

Okay,

Once again, I will answer my own question :D

I've finallt find the way to use the parameters.Predecessors value in my variable declaration. And, second, the Microsoft documentation seams not to be updated.

The tf_apply_stage.yml should be like that below ;

  parameters:
  - name: stage
    type: string
    default: Deploy_prepare
  - name: Predecessors
    type: string
    default: none
stages:
  - stage: ${{ parameters.stage }}
    ? ${{ if and(ne(parameters.Predecessors, 'previous'), ne(parameters.Predecessors, 'none') ) }}
    : dependsOn: ${{parameters.Predecessors}}
    displayName: ${{ parameters.TF_VAR_STAGE }} download artifact & Terraform apply changes to ${{ parameters.TF_VAR_APPLICATION }}
    pool:
      name: Azure Pipelines
      vmImage: windows-2019
    jobs:
    - deployment: DeployTerraform
      displayName: ${{ parameters.TF_VAR_STAGE }} download artifact & Terraform apply changes
      variables:
        *# this one give he good value for Variable*
        planJobAttempt: $[ stageDependencies.${{ parameters.Predecessors }}.PreDeployTerraform.outputs['PreDeployTerraform.setVarJobAttempt.planAttempt'] ]            
      environment: ${{ parameters.TF_VAR_ENVIRONMENT }}
      timeoutInMinutes: 480
      strategy:
        runOnce:
          deploy:
            steps:
              - checkout: none
              
              - powershell: | 
                  echo "plan file for terraform is : $(pipeline_artifact_folder_download)/$(planJobAttempt)_${{ parameters.Predecessors }}/$(artefact_terraform_plan)_${{ parameters.TF_VAR_STAGE }}"

I put in BOLD the main changes above :

planJobAttempt: $[ stageDependencies.${{ parameters.Predecessors }}.PreDeployTerraform.outputs['PreDeployTerraform.setVarJobAttempt.planAttempt'] ]

The Job name have to be set twice in variable declaration !

Thanks for those who read my post ;)

And many thanks to this post from @Kay07949 who gives the good answers ;)

  • Related