Search code examples
azure-devopscicd

Azure Devops: How to pass variable FROM agent job TO agentless job?


I have a pipeline with 2 jobs.

  1. CheckoutAndDeploy (standard agent job)
  2. WaitDeployStatus (agentless)

In WaitDeployStatus (1) I am trying to use an agentless job with Invoke Rest API. In this step, I need to post on an URL, the URL requires OAuth authentication

I can get the OAuth token from the previous job (CheckoutAndDeploy) and set isOutput=true to make the var available for other jobs (like described in the documentation) but I am unable to use it in job 2.

I already tried:

 $(SOMEREF.ACCESS_TOKEN)"
 $[ dependencies.Get_Access_Token.outputs['SOMEREF.ACCESS_TOKEN'] ]"
 $[ dependencies.Get_Access_Token.CheckoutAndDeploy.outputs['SOMEREF.ACCESS_TOKEN'] ]"
 $[ stageDependencies.CheckoutAndDeploy.outputs['SOMEREF.ACCESS_TOKEN'] ]" 

To set the variable is a bash task with this code:

TOKEN_RESQUEST=$(command_to_get_the_token)
echo "##vso[task.setvariable variable=ACCESS_TOKEN;isOutput=true]$TOKEN_REQUEST"

Then I use SOMEREF as a reference name to access the variable.


Solution

  • The below usage will be able to pass the runtime variables from agent job to agentless job:

    trigger:
    - none
    
    pool:
      vmImage: ubuntu-latest
    
    jobs:
    - job: A #agent job
      steps:
      - bash: |
          echo "A"
          echo "##vso[task.setvariable variable=outputVar;isoutput=true]<Auth information here>"
        name: passOutput
    - job: B #agentless job
      pool: server
      dependsOn: A
      variables:
        myVarFromJobA: $[ dependencies.A.outputs['passOutput.outputVar'] ]  
      steps:
        - task: InvokeRESTAPI@1
          inputs:
            connectionType: 'connectedServiceName'
            serviceConnection: 'GetProjects'
            method: 'GET'
            headers: |
              {
              "Content-Type":"application/json",  
              "Authorization": "$(myVarFromJobA)"
              }
            waitForCompletion: 'false'
    

    Auth passed, I can successfully get the results here:

    enter image description here