azure - 如何使用环境变量将输出从一个阶段传递到下一阶段?

标签 azure powershell azure-devops azure-pipelines

如何将环境变量从第一阶段转移到第二阶段?

问题 -> 我从回显结果中得到一个空的“”值。

azure-pipeline.yml

trigger:
  branches:
    include:
    - main
  paths:
    include:
    - infra/*
    - .pipelines/*

variables:
  vmImageName: 'ubuntu-latest'
  root: $(System.DefaultWorkingDirectory)

stages:
- stage: TakePITR
  dependsOn: []
  variables:
    env: poc
  jobs:
  - job: GetPitrTime
    steps:
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          $iso8601_time = Get-Date -Format "o"
          echo "##vso[task.setvariable variable=pitr_time;isOutput=True]$iso8601_time"
        name: PitrVar
        displayName: "Get point-in-time before second stage"

- stage: TestOutputEnvVar
  dependsOn: TakePITR
  variables:
    env: poc
    PITR: $[ stageDependencies.TakePITR.GetPitrTime.outputs['PitrVar.pitr_time']]
  jobs:
  - job:
    steps:
    - script: |
        echo $pitr_time
        echo $PITR
      displayName: 'echo value'

我不明白我在这里做错了什么。

这是我的管道的结果: enter image description here

最佳答案

您有缩进问题:

    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          $iso8601_time = Get-Date -Format "o"
          echo "##vso[task.setvariable variable=pitr_time;isOutput=true]$iso8601_time"
      name: PitrVar
      displayName: "Get point-in-time before second stage"

Name 和 displayName 应与输入处于同一级别,然后:

enter image description here

完整代码在这里

trigger:
  branches:
    include:
    - main
  paths:
    include:
    - infra/*
    - .pipelines/*

variables:
  vmImageName: 'ubuntu-latest'
  root: $(System.DefaultWorkingDirectory)

stages:
- stage: TakePITR
  dependsOn: []
  variables:
    env: poc
  jobs:
  - job: GetPitrTime
    steps:
    - task: PowerShell@2
      name: PitrVar
      displayName: "Get point-in-time before second stage"
      inputs:
        targetType: 'inline'
        script: |
          $iso8601_time = Get-Date -Format "o"
          echo "##vso[task.setvariable variable=pitr_time;isOutput=true]$iso8601_time"

- stage: TestOutputEnvVar
  dependsOn: TakePITR
  variables:
    env: poc
    PITR: $[ stageDependencies.TakePITR.GetPitrTime.outputs['PitrVar.pitr_time']]
  jobs:
  - job:
    steps:
    - script: |
        echo $pitr_time
        echo $PITR
      displayName: 'echo value'

关于azure - 如何使用环境变量将输出从一个阶段传递到下一阶段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70020755/

相关文章:

c# - 单击通知时阻止 UWP 应用程序打开

windows - 使用 Ansible 在 Windows 客户机上映射网络驱动器

maven - 将快照 Artifact 发布到 azure devops Artifact

azure-devops - 在 YAML 部署作业中部署后检查应用程序运行状况端点的最佳方法?

.net-core - 如何将 .NET Core 2.0 xUnit 测试报告导入 VSTS

azure - 如何使用 Azure Powershell 更新 Azure 云服务设置

asp.net - 如何将消息记录到 Windows Azure 存储?

sql - 将 Azure bacpac 文件导入本地数据库时出错 EXTERNAL 附近的语法错误

Windows systemprofile 目录根据用户显示不同

c# - 是否有任何工具可以对 powershell 执行 c# 代码