How to run manual validation task in azure devops based on some variable set in previous job

vijay kumar 0 Reputation points
2024-01-17T17:08:34.5033333+00:00

I am trying to create 2 jobs in single devops pipeline. In first one i am reading some json file and setting some task variable and wanted to run the manual validation task in another job based on the task variable i setup in previous job. However i am not able to refer that variable in second job, need some help here

Community Center Not monitored
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Luis Arias 8,621 Reputation points Volunteer Moderator
    2024-01-17T20:45:33.45+00:00

    Hi vijay,

    Can you share your code to help you? I can bring you an example of pipeline with powershell on jobs and sharing variables between jobs. Starting point with the file example.json:

    {
      "someKey": "expectedValue",
      "anotherKey": "anotherValue"
    }
    
    

    This is the pipeline that you can test Its working fine:

    trigger: none
    
    pool:
      vmImage: 'windows-latest'
    
    jobs:
    - job: Job1
      displayName: 'Job 1: Read JSON and set variable'
      steps:
      - powershell: |
          # Read the JSON file
          $json = Get-Content -Path example.json | ConvertFrom-Json
          # Get a value from the JSON file
          $value = $json.someKey
          # Set the value as a variable
          Write-Host "##vso[task.setvariable variable=someVariable;isOutput=true;]$value"
        name: setVariableStep
    
    - job: Job2
      displayName: 'Job 2: Validate variable'
      dependsOn: Job1
      variables:
        someVariableFromJob1: $[ dependencies.Job1.outputs['setVariableStep.someVariable'] ]
      steps:
      - pwsh: |
          Write-Host "The variable from Job 1 is: $(someVariableFromJob1)"
          # Validate the variable
          if ("$(someVariableFromJob1)" -eq "expectedValue") {
            Write-Host "The variable is as expected."
          } else {
            Write-Host "The variable is not as expected."
          }
    
    

    If you run this pipeline will compare the expectedValue on the json file with the job2. Let me know if this help you. Luis


    If the information helped address your question, please Accept the answer.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.