在Azure Devops的cURL请求中使用环境变量 [英] Using Environment Variables in a cURL request on Azure Devops

查看:153
本文介绍了在Azure Devops的cURL请求中使用环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to upload a zip file to Netlify with a command line task using cURL on Azure DevOps.

很显然,我不想在yaml文件中使用Netlify访问令牌,因此我已经为它创建了一个秘密变量(使用UI设计器),并使用

Obviously I don't want to have my Netlify access token in the yaml file, so I've created a secret variable for it (using the UI designer) and mapped it using the syntax in the docs.

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

However I keep getting a 401 back from Netlify. I can confirm via POSTMAN that the access token is valid. So I'm not sure what I'm doing wrong here. Am I using the env variables incorrectly in the request?

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

here's the portion of the YAML file that deals with uploading the file.

- 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的回复:

Response from Netlify:

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

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

Below is the full YAML file after I managed to get it working using the 'input-macro' syntax from the docs

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'

推荐答案

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

you need to use bash syntax to retrieve environment variable for that, not powershell (since you are using bash, not powershell):

-H "Authorization: Bearer $ACCESS_TOKEN"

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

I also suspect that you need to update your env declaration:

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

这篇关于在Azure Devops的cURL请求中使用环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆