在Azure Devops的cURL请求中使用环境变量

时间:2018-12-28 18:05:47

标签: azure curl azure-devops yaml netlify

我正在尝试使用Azure DevOps上的cURL通过命令行任务将zip文件上传到Netlify。

很显然,我不想将我的Netlify访问令牌存储在yaml文件中,所以我已经为它创建了一个秘密变量(使用UI设计器),并使用docs中的语法对其进行了映射。

但是我一直从Netlify找回401。我可以通过POSTMAN确认访问令牌有效。所以我不确定我在做什么错。我在请求中使用env变量不正确吗?

这是YAML文件中用于上传文件的部分。

- script:  >-
      curl
      -H 'Authorization: Bearer $env:ACCESS_TOKEN' 
      -H 'Content-Type: application/zip'
      --data-binary '@$(Build.BuildId).zip'
      https://api.netlify.com/api/v1/sites/$env:SITE_ID/deploys
  workingDirectory: '$(Build.ArtifactStagingDirectory)'
  displayName: 'Upload to Netlify'
  env: 
    ACCESS_TOKEN: $netlifyAccessToken
    SITE_ID: $netlifySiteId

来自Netlify的回复:

{"code":401,"message":"Access Denied: Origin returned bad status 401"}` 

编辑:

下面是完整的YAML文件,在我设法使用docs

中的'input-macro'语法使其能够正常工作之后
trigger:
- master

pool:
  vmImage: 'Ubuntu-16.04'

variables:
  configuration: debug
  platform: x64

steps:
- task: DotNetCoreInstaller@0
  displayName: Install .NET Core SDK
  name: install_dotnetcore_sdk
  enabled: true
  inputs:
    packageType: 'sdk'
    version: '2.2.101'

- script: dotnet tool install -g Wyam.Tool
  displayName: Install Wyam

- script: wyam
  displayName: Build Site 

- task: ArchiveFiles@2
  displayName: Zip Site
  inputs:
    rootFolderOrFile: '$(Agent.BuildDirectory)/s/output' 
    includeRootFolder: true
    archiveType: 'zip'
    archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip' 
    replaceExistingArchive: true

- script:  >-
      curl
      -H 'Authorization: Bearer $(netlifyAccessToken)' 
      -H 'Content-Type: application/zip'
      --data-binary '@$(Build.BuildId).zip'
      https://api.netlify.com/api/v1/sites/$(netlifySiteId)/deploys
  workingDirectory: '$(Build.ArtifactStagingDirectory)'
  displayName: 'Upload to Netlify'

1 个答案:

答案 0 :(得分:4)

您需要使用bash语法来检索环境变量,而不是powershell(因为您正在使用bash,而不是powershell):

-H "Authorization: Bearer $ACCESS_TOKEN"

我还怀疑您需要更新环境声明:

env: 
  ACCESS_TOKEN: $(netlifyAccessToken) << ADO token to replace with variable from build scope
  SITE_ID: $(netlifySiteId)