在脚本的 Azure 管道中使用 AWSCLI 中的变量

Use variable from AWSCLI in azure pipelines for script

我有一个构建过程,我需要使用通过 AWSCLI 接收的令牌。到目前为止,我已将 aws 连接到我的 azure 管道,但我在设置我的 yaml 时遇到了问题。 我想获取相关的令牌以便稍后在我的脚本中将其用作变量。

正如您在我的 yaml 中看到的那样,我正在 运行 使用 codeartifact 创建一个 powershell 脚本,并将该值保存到我的 myOutputVar。 powershell 脚本不会抛出错误。 但是,稍后当我 运行 构建脚本时,该变量不存在导致 ECHO 关闭。

如何确保任务中收到的值可以在稍后的 script/build 部分中使用?

trigger:
- azure-pipelines

pool:
  vmImage: windows-latest

steps:
- task: NodeTool@0
  inputs:
    versionSpec: '10.x'
  displayName: 'Install Node.js'
- task: AWSPowerShellModuleScript@1
  inputs:
    awsCredentials: 'AWS Connection'
    regionName: 'eu-central-1'
    scriptType: 'inline'
    inlineScript: '##vso[task.setvariable variable=myOutputVar;]aws codeartifact get-authorization-token --domain somedomain  --domain-owner 444.... --query authorizationToken --output text; '
- script: |
    echo %myOutputVar%
    npm ci
    npm run build
  displayName: 'npm install and build'

您的内联脚本可以是多行,因为这是 PowerShell,您可以执行如下操作:

inlineScript: |
   $authToken = aws codeartifact get-authorization-token `
                    --domain somedomain `
                    --domain-owner 444.... `
                    --query authorizationToken `
                    --output text

   Write-Host "##vso[task.setvariable variable=myOutputVar;]$authToken"