skip to Main Content

I am trying to use an output variable in a second deployment job, by referencing it with ‘dependencies’, but it is not able to fetch the value. It seems that:

$[dependencies.FetchAppRegistration.outputs['Fetch_Azure_AD.AppClientId']] 

does not return anything in the code below. I have tried to follow the guideline on

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/set-variables-scripts?view=azure-devops&tabs=bash

without any luck. The pipeline output is:

FetchAppRegistration Console output:

Fetching Azure AD App with display name: msys-azure-dev
Application found. AppId: ***
Setting variable AppClientId to: ***

Deploy_Infrastructure Console output:

First Log:

echo "App Client ID: "

Second Log:

   | Missing an argument for parameter 'existingAppClientId'. Specify a
     | parameter of type 'System.String' and try again.

Full Code:

parameters:
  environment: ''
  serviceConnectionName: ''

jobs:
- job: Build
  displayName: 'Build Job'
  steps:
    - script: echo "Building the application..."
      displayName: 'Execute build'

- deployment: FetchAppRegistration
  displayName: 'Fetch Azure AD App Registration Client ID'
  environment: ${{ parameters.environment }}
  dependsOn: Build
  strategy:
    runOnce:
      deploy:
        steps:
          - checkout: self
          - task: AzurePowerShell@5
            name: Fetch_Azure_AD
            displayName: 'Fetch Azure AD App Registration Client ID'
            inputs:
              azureSubscription: ${{ parameters.serviceConnectionName }}
              ScriptType: 'InlineScript'
              Inline: |
                $appName = "msys-azure-${{ parameters.environment }}".ToLower()
                Write-Host "Fetching Azure AD App with display name: $appName"
                $app = Get-AzADApplication -DisplayName $appName
                if ($app -eq $null) {
                  Write-Error "Application not found: $appName"
                  exit 1
                }
                $appId = $app.AppId
                Write-Host "Application found. AppId: $appId"
                Write-Host "##vso[task.setvariable variable=AppClientId;isOutput=true]$appId"
                Write-Host "Setting variable AppClientId to: $appId"
              azurePowerShellVersion: 'LatestVersion'
              pwsh: true

- deployment: Deploy_Infrastructure
  displayName: 'Deploy Infrastructure'
  dependsOn: FetchAppRegistration
  condition: succeeded()
  environment: ${{ parameters.environment }}
  variables:
    AppClientId: $[dependencies.FetchAppRegistration.outputs['Fetch_Azure_AD.AppClientId']]
  strategy:
    runOnce:
      deploy:
        steps:
          - download: current
            artifact: drop
            displayName: Download artifacts
          - script: |
              echo "App Client ID: $(AppClientId)"
            displayName: 'Debug App Client ID'
          - task: AzurePowerShell@5
            displayName: 'Deploy Azure Infrastructure'
            inputs:
              connectedServiceNameARM: ${{ parameters.serviceConnectionName }}
              azurePowerShellVersion: 'LatestVersion'
              ScriptPath: '$(Pipeline.Workspace)/drop/deploy/deploy.ps1'
              ScriptArguments: 
                -resourceGroupName $(ResourceGroupName) `
                -existingAppClientId $(AppClientId)

2

Answers


  1. Based on your description, the issue was about the failure in setting an output variable generated during a deployment job for use in future jobs within the same stage. Please note that Deployment jobs use a different syntax for output variables. To learn more about support for output variables in deployment jobs, see Deployment jobs.

    Per the upstream job deployment: FetchAppRegistration was setting the output variable AppClientId in runOnce stratgy without resourceType, we were able to print the variable value with the syntax of AppClientId: $[dependencies.FetchAppRegistration.outputs['FetchAppRegistration.Fetch_Azure_AD.AppClientId']] in downstream job.

    Besides, it is suggested troubleshooting the dependencies output for a pipeline job or stage by adding a variable for the dependencies and then printing that variable. deps: $[convertToJson(dependencies)] Here is the part of downstream job deployment: Deploy_Infrastructure from a YAML pipeline for your reference.

    - deployment: Deploy_Infrastructure
      displayName: 'Deploy Infrastructure'
      dependsOn: FetchAppRegistration
      condition: succeeded()
      environment: ${{ parameters.environment }}
      variables:
        AppClientId: $[dependencies.FetchAppRegistration.outputs['FetchAppRegistration.Fetch_Azure_AD.AppClientId']]
        deps: $[convertToJson(dependencies)]
      strategy:
        runOnce:
          deploy:
            steps:
              - script: |
                  echo "App Client ID: $(AppClientId)"
                  echo "$(deps)"
                displayName: 'Debug App Client ID'
    

    Image

    Login or Signup to reply.
  2. Access job variables from deployment job in runOnce strategy has the pattern: $[dependencies.JobA.outputs['JobA.StepA.VariableA']]) so here should be AppClientId: $[dependencies.FetchAppRegistration.outputs['FetchAppRegistration.Fetch_Azure_AD.AppClientId']])

    docs link

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search