最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

azure - Output variable for use in future stages - Stack Overflow

programmeradmin5浏览0评论

Here is my YAML file. When my Redis stage is run it successfully creates my Azure Redis cache server and sent an output to next task RedisSetvar and it successfully writes my output host and displays the value.

  - stage: Redis
    dependsOn: "Information"
    jobs:
      - deployment: "Redis"
        pool:
          name: "Azure VM 2019"
        environment: "dev"
        strategy:
          runOnce:
            deploy:
              steps:
                - checkout: ""
                  path: ""
                - task: AzureResourceManagerTemplateDeployment@3
                  name: Redis 
                  displayName: "Redis"
                  inputs:
                    deploymentScope: "Resource Group"
                    azureResourceManagerConnection: "$(GlobalResourceManagerConnection)"
                    subscriptionId: "$(GlobalSubscriptionId)"
                    action: "Create Or Update Resource Group"
                    resourceGroupName: "$(ResourceGroupNameValue)"
                    location: "$(GlobalLocation)"
                    templateLocation: "Linked artifact"
                    deploymentMode: "Incremental"
                    csmFile: "$(Agent.BuildDirectory)/abc/Pipelines/Deployment/Architecture/Redis/Redis.azuredeploy.json"
                    csmParametersFile: "$(Agent.BuildDirectory)/abcPipelines/Deployment/Architecture/Redis/Redis.azuredeploy.parameters.json"
                    overrideParameters: '-clientName "$(clientName)" -resourcePrefix "$(resourcePrefix)"'
                    deploymentOutputs: "RedisResourcesOutputs"
                - task: PowerShell@2
                  name: RedisSetvar
                  displayName: "Display RedisOutputs"
                  inputs:
                    targetType: "inline"
                    script: |
                      $RedisdeploymentOutput = ConvertFrom-Json -InputObject '$(RedisResourcesOutputs)'
                      $redisKey = $RedisdeploymentOutput.RedisKey.value
                      Write-Host "redisKey: $redisKey"
                      Write-Host "##vso[task.setvariable variable=redisKey;isOutput=true]$redisKey"

Issue comes in next stage where I want to get this output and want to display in next stage which is my app service. It's not a value though it's just null.

- stage: AppServices
    dependsOn:
    - Redis
    variables:
      redisKey: $[stageDependencies.Redis.Redis.outputs['RedisSetvar.redisKey']]
                       
    jobs:
      - deployment: "AppServices"
        pool:
          name: "Azure VM 2019"
        environment: "dev"
        strategy:
          runOnce:
            deploy:
              steps:
                - checkout: "abc"
                  path: "abc"
                - task: PowerShell@2
                  displayName: "Print Variables"
                  inputs:
                    targetType: "inline"
                    script: |
                      Write-Host "redisKey: $(redisKey)"

Here is my YAML file. When my Redis stage is run it successfully creates my Azure Redis cache server and sent an output to next task RedisSetvar and it successfully writes my output host and displays the value.

  - stage: Redis
    dependsOn: "Information"
    jobs:
      - deployment: "Redis"
        pool:
          name: "Azure VM 2019"
        environment: "dev"
        strategy:
          runOnce:
            deploy:
              steps:
                - checkout: ""
                  path: ""
                - task: AzureResourceManagerTemplateDeployment@3
                  name: Redis 
                  displayName: "Redis"
                  inputs:
                    deploymentScope: "Resource Group"
                    azureResourceManagerConnection: "$(GlobalResourceManagerConnection)"
                    subscriptionId: "$(GlobalSubscriptionId)"
                    action: "Create Or Update Resource Group"
                    resourceGroupName: "$(ResourceGroupNameValue)"
                    location: "$(GlobalLocation)"
                    templateLocation: "Linked artifact"
                    deploymentMode: "Incremental"
                    csmFile: "$(Agent.BuildDirectory)/abc/Pipelines/Deployment/Architecture/Redis/Redis.azuredeploy.json"
                    csmParametersFile: "$(Agent.BuildDirectory)/abcPipelines/Deployment/Architecture/Redis/Redis.azuredeploy.parameters.json"
                    overrideParameters: '-clientName "$(clientName)" -resourcePrefix "$(resourcePrefix)"'
                    deploymentOutputs: "RedisResourcesOutputs"
                - task: PowerShell@2
                  name: RedisSetvar
                  displayName: "Display RedisOutputs"
                  inputs:
                    targetType: "inline"
                    script: |
                      $RedisdeploymentOutput = ConvertFrom-Json -InputObject '$(RedisResourcesOutputs)'
                      $redisKey = $RedisdeploymentOutput.RedisKey.value
                      Write-Host "redisKey: $redisKey"
                      Write-Host "##vso[task.setvariable variable=redisKey;isOutput=true]$redisKey"

Issue comes in next stage where I want to get this output and want to display in next stage which is my app service. It's not a value though it's just null.

- stage: AppServices
    dependsOn:
    - Redis
    variables:
      redisKey: $[stageDependencies.Redis.Redis.outputs['RedisSetvar.redisKey']]
                       
    jobs:
      - deployment: "AppServices"
        pool:
          name: "Azure VM 2019"
        environment: "dev"
        strategy:
          runOnce:
            deploy:
              steps:
                - checkout: "abc"
                  path: "abc"
                - task: PowerShell@2
                  displayName: "Print Variables"
                  inputs:
                    targetType: "inline"
                    script: |
                      Write-Host "redisKey: $(redisKey)"
Share Improve this question edited Mar 19 at 14:44 Ahsan Ullah Khan asked Mar 17 at 16:01 Ahsan Ullah KhanAhsan Ullah Khan 131 silver badge4 bronze badges 2
  • I can hardly see how this is related to git. – eftshift0 Commented Mar 17 at 16:15
  • yeah. if git knowledge would help here putting it back's fine but I'm removing it. – jthill Commented Mar 18 at 5:21
Add a comment  | 

2 Answers 2

Reset to default 0

To reference an output variable from a previous deploy job in another stage, you should do like as below:

  1. If the previous deploy job only has the environment specified, you should use the expression "$[ stageDependencies.<stage-name>.<job-name>.outputs['<job-name>.<step-name>.<variable-name>'] ]" to access the output variable. See below example.
stages:
- stage: stage1
  jobs:
  - deployment: deploy01
    environment: 'EnvVM'  # Only specify the environment.
    strategy:
      runOnce:
        deploy:
          steps:
          - pwsh: |
              Write-Host "##vso[task.setvariable variable=myOutVar;isOutput=true]abc123"
            name: setOutVar
            displayName: 'Set output variable'

- stage: stage2
  dependsOn: stage1
  variables:
    getOutVar: $[ stageDependencies.stage1.deploy01.outputs['deploy01.setOutVar.myOutVar'] ]
  jobs:
  - deployment: deploy02
    environment: MyEnv02
    strategy:
      runOnce:
        deploy:
         steps:
          - pwsh: |
              Write-Host "getOutVar = $(getOutVar)"
            displayName: 'Print output variable'

  1. If the previous deploy job has detailed resource specified from the environment, you should use the expression "$[ stageDependencies.<stage-name>.<job-name>.outputs['Deploy_<resource-name>.<step-name>.<variable-name>'] ]" to access the output variable. See below example.
stages:
- stage: stage1
  jobs:
  - deployment: deploy01
    environment:
      name: 'EnvVM'
      resourceName: 'Win11-01'  # Specified a detailed resource from the environment.
    strategy:
      runOnce:
        deploy:
          steps:
          - pwsh: |
              Write-Host "##vso[task.setvariable variable=myOutVar;isOutput=true]abc123"
            name: setOutVar
            displayName: 'Set output variable'

- stage: stage2
  dependsOn: stage1
  variables:
    getOutVar: $[ stageDependencies.stage1.deploy01.outputs['Deploy_Win11-01.setOutVar.myOutVar'] ]
  jobs:
  - deployment: deploy02
    environment: MyEnv02
    strategy:
      runOnce:
        deploy:
         steps:
          - pwsh: |
              Write-Host "getOutVar = $(getOutVar)"
            displayName: 'Print output variable'

Related documentations:

  • Deployment jobs
  • Expressions

Check this link: https://learn.microsoft/en-us/azure/devops/pipelines/process/deployment-jobs?view=azure-devops#support-for-output-variables

In your case, the template should be (but for a stage):

For runOnce strategy: $[dependencies.<job-name>.outputs['<job-name>.<step-name>.<variable-name>']] (for example, $[dependencies.JobA.outputs['JobA.StepA.VariableA']])

stages:
- stage: Redis
  jobs:
  - deployment: Redis
    environment: Redis'  # Only specify the environment.
    strategy:
      runOnce:
        deploy:
          steps:
          - pwsh: |
              Write-Host "##vso[task.setvariable variable=redisKey;isOutput=true]abc123"
            name: RedisSetvar
            displayName: 'Set output variable'

- stage: AppServices
  dependsOn: Redis
  variables:
    redisKey: $[ stageDependencies.Redis.Redis.outputs['Redis.RedisSetvar.redisKey'] ]
  jobs:
  - deployment: AppServices
    environment: AppServices
    strategy:
      runOnce:
        deploy:
         steps:
          - pwsh: |
              Write-Host "redisKey = $(redisKey)"
发布评论

评论列表(0)

  1. 暂无评论