azure-devops

setCustomerId not passing the variable from stage 1 to 2 in ado


I want the customer id to be passed from stage 1 to stage 2, stage 1 is coming but not passing to stage 2. Stage 3 is also there which is dependent on stage 2 also I need

stages:
  - stage: CreateCustomer
    jobs:
    ## Run Create customer Tests scripts ###
      - job: CreateSubscriber
        displayName: 'CreateSubscriber'
        steps:
        - checkout: self
          persistCredentials: true

        - task: PowerShell@2
          displayName: 'Set Test Filters'
          inputs:
            targetType: 'inline'
            Script: |
              Write-Host "Selected Environment Name is : ${{ parameters.EnvironmentName }}"
              [System.Environment]::SetEnvironmentVariable('environmentName','${{ parameters.EnvironmentName }}', [EnvironmentVariableTarget]::User)
              [System.Environment]::SetEnvironmentVariable('ClientName','${{ parameters.ClientName }}', [EnvironmentVariableTarget]::User)
      
        - task: PowerShell@2
          displayName: 'Configure test settings'
          inputs:
            filePath: '$(Build.SourcesDirectory)\Set-TestSettingsConfig.ps1'
            arguments: '-Environment ${{ parameters.EnvironmentName }}'
      

        - task: NuGetAuthenticate@1
          displayName: NuGet Authenticate
        - task: DotNetCoreCLI@2
          displayName: 'Compile'
          inputs:
            command: 'build'
            projects: 'Se/Se/Se.csproj'

        - task: PowerShell@2
          displayName: 'CreateSubscriber'
          inputs:
            targetType: 'inline'
            Script: |
              $categoryG = "CreateSubscriber"
              $argumentsG = ""
              if ($categoryG -ne "SmokTest")
              {
                $argumentsG = " --filter Category=$categoryG"
              }
              Write-Host "Setting DotNet Test Arguments ($argumentsG)"
              Write-Host "##vso[task.setvariable variable=TestArgumentsG;]$argumentsG"

        - task: DotNetCoreCLI@2
          displayName: 'CreateSubscriber'
          inputs:
            command: test
            arguments: $(TestArgumentsG)
            projects: 'Se/Se/Se.csproj' 

        - task: PowerShell@2
          displayName: "Print Current Working Directory"
          inputs:
            targetType: 'inline'
            script: 'Write-Host "Current Directory: $(Get-Location)"'

        - task: PowerShell@2
          displayName: "change to build.sorce"
          inputs:
            targetType: 'inline'
            script: |
              Set-Location "$(Build.SourcesDirectory)"
                   Write-Host "Switched to: $(Get-Location)"
              Write-Host "Files in Build.SourcesDirectory:"
                   Get-ChildItem -Path "$(Build.SourcesDirectory)" -Recurse | ForEach-Object { Write-Host $_.FullName }

        - task: PowerShell@2
          inputs:
            targetType: 'inline'
            script: |
              $filePath = "$(Get-Location)\Se\Se\QA\Json\ShareJsonFiles\CustomerID.json"
                   Write-Host "Reading file: $filePath"
               $jsonData = Get-Content $filePath | ConvertFrom-Json
                   $customerId = $jsonData.CustomerId
                   Write-Host "Extracted Customer ID: $customerId"
                   Write-Host "##vso[task.setvariable variable=CustomerId;isOutput=true]$customerId"
            workingDirectory: '$(Build.SourcesDirectory)'
          name: setCustomerId
        - task: PublishTestResults@2
          displayName: 'Publish Test Results'
          condition: succeededOrFailed()
          inputs:
            testResultsFormat: VSTest
            testResultsFiles: '$(Build.SourcesDirectory)\artifacts\**\TestOutput.xml'
          
        - task: tagBuildOrRelease@0
          displayName: 'Add Tag: Passed CreateCustomer'
          inputs:
            type: 'Build'
            tags: 'Passed CreateCustomer'

        - task: DeleteFiles@1
          displayName: 'Delete files from repository'
          inputs:
            SourceFolder: '$(Build.SourcesDirectory)'
            Contents: '**'

  - stage: Products_PP_Verification
    dependsOn: 
    - CreateCustomer
    condition: succeeded()
    jobs:
    ## Run regression Tests scripts ###
      - job: Products_PP_Verification
        variables:
          CustomerId: $[ stageDependencies.CreateCustomer.CreateSubscriber.outputs['setCustomerId.CustomerId'] ]
        displayName: 'Products_PP_Verification'
        workspace:
          clean: all
        steps:

        - checkout: self
          persistCredentials: true

        - task: PowerShell@2
          displayName: 'Set Test Filters'
          inputs:
            targetType: 'inline'
            Script: |
              Write-Host "Selected Environment Name is : ${{ parameters.EnvironmentName }}"
              [System.Environment]::SetEnvironmentVariable('environmentName','${{ parameters.EnvironmentName }}', [EnvironmentVariableTarget]::User)
              [System.Environment]::SetEnvironmentVariable('ClientName','${{ parameters.ClientName }}', [EnvironmentVariableTarget]::User)
      
        - task: PowerShell@2
          displayName: 'Configure test settings'
          inputs:
            filePath: '$(Build.SourcesDirectory)\Set-TestSettingsConfig.ps1'
            arguments: '-Environment ${{ parameters.EnvironmentName }}'
      
        - task: PowerShell@2
          displayName: "Debug - Print Dependency Outputs in Stage 2"
          inputs:
           targetType: inline
           script: |
            Write-Host "Debugging Dependencies Output"
            Write-Host "CustomerId from dependencies: $[ stageDependencies.CreateCustomer.CreateSubscriber.outputs['CustomerId'] ]

        - task: PowerShell@2
          inputs:
            targetType: 'inline'
            script: |
              Get-ChildItem Env:
          displayName: "find variable"

        - task: PowerShell@2
          displayName: "Debug - Print CustomerId"
          inputs:
            targetType: inline
            script: |
              Write-Host "CustomerId from previous stage: $(CustomerId)"

        - task: PowerShell@2
          displayName: 'Products_PP_Verification'
          inputs:
            targetType: 'inline'
            Script: |
              $categoryG = "Products_PP_Verification"
              $argumentsG = ""
              if ($categoryG -ne "SmokTest")
              {
                $argumentsG = " --filter Category=$categoryG"
              }
              Write-Host "Setting DotNet Test Arguments ($argumentsG)"
              Write-Host "##vso[task.setvariable variable=TestArgumentsG;]$argumentsG"

        - task: DotNetCoreCLI@2
          displayName: 'Products_PP_Verification'
          inputs:
            command: test
            arguments: $(TestArgumentsG)
            projects: 'Se/Se/Se.csproj' 
          env:
            CUSTOMER_ID: $(CustomerId)

Solution

  • I have extract your steps only related to customId, remove other distractors.

    I think your variables config in stage 2 should works.

    CustomerId: $[ stageDependencies.CreateCustomer.CreateSubscriber.outputs['setCustomerId.CustomerId'] ]

    Just copy and paste and have a try. main.yaml

    pool:
      vmImage: 'windows-latest'
    
    stages:
      - stage: CreateCustomer
        jobs:
        ## Run Create customer Tests scripts ###
          - job: CreateSubscriber
            displayName: 'CreateSubscriber'
            steps:
            - checkout: self
              persistCredentials: true
            - task: PowerShell@2
              inputs:
                targetType: 'inline'
                script: |
                  $customerId = "123"
                  Write-Host "Extracted Customer ID: $customerId"
                  Write-Host "##vso[task.setvariable variable=CustomerId;isOutput=true]$customerId"
      
              name: setCustomerId
    
      - stage: Products_PP_Verification
        dependsOn: 
        - CreateCustomer
        condition: succeeded()
        jobs:
        ## Run regression Tests scripts ###
          - job: Products_PP_Verification
            variables:
              CustomerId: $[ stageDependencies.CreateCustomer.CreateSubscriber.outputs['setCustomerId.CustomerId'] ]
            displayName: 'Products_PP_Verification'
            workspace:
              clean: all
            steps:
    
            - checkout: self
              persistCredentials: true
    
            - task: PowerShell@2
              displayName: "Debug - Print CustomerId"
              inputs:
                targetType: inline
                script: |
                  Write-Host "option 1 CustomerId from previous stage: $(CustomerId)"
            
            - task: PowerShell@2
              displayName: "Debug - Print CustomerId"
              inputs:
                targetType: inline
                script: |
                  Write-Host "option 2 CustomerId from previous stage as env: $ENV:ENV_CUSTOMER_ID"
              env:
                ENV_CUSTOMER_ID: $(CustomerId)
    

    debug