简体   繁体   English

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

[英]Using Environment Variables in a cURL request on Azure Devops

I'm trying to upload a zip file to Netlify with a command line task using cURL on Azure DevOps. 我正在尝试使用Azure DevOps上的cURL通过命令行任务将zip文件上传到Netlify。

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 . 显然,我不想在yaml文件中使用Netlify访问令牌,因此我已经为其创建了一个秘密变量(使用UI设计器),并使用docs中的语法对其进行了映射。

However I keep getting a 401 back from Netlify. 但是我一直从Netlify获得401。 I can confirm via POSTMAN that the access token is valid. 我可以通过POSTMAN确认访问令牌有效。 So I'm not sure what I'm doing wrong here. 所以我不确定我在做什么错。 Am I using the env variables incorrectly in the request? 我在请求中错误地使用了env变量吗?

here's the portion of the YAML file that deals with uploading the file. 这是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

Response from Netlify: Netlify的回复:

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

EDIT: 编辑:

Below is the full YAML file after I managed to get it working using the 'input-macro' syntax from the docs 以下是我设法使用文档中的“ input-macro”语法使其正常工作后的完整YAML文件

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'

you need to use bash syntax to retrieve environment variable for that, not powershell (since you are using bash, not powershell): 您需要使用bash语法来检索环境变量,而不是powershell(因为您正在使用bash,而不是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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure Devops Angular 环境变量 - Azure Devops Angular Environment Variables Azure Devops:使用拉取请求构建手动部署到环境 - Azure Devops: Manual Deployment to Environment using Pull Request build 如何通过 Azure DevOps 在 Dockerfile 中设置环境变量 - How to set environment variables in Dockerfile via Azure DevOps 如何在 Azure DevOps 的脚本步骤中读取环境变量? - How to read environment variables in Script step in Azure DevOps? 使用 Azure DevOps API 共享一个拉取请求 - Using Azure DevOps API to share a pull request Azure DevOps:使用变量填充“Azure 订阅”字段 - Azure DevOps: Fill 'Azure subscription' field using variables 使用 azure devops 部署 ADF 是否会影响环境中的现有管道/作业 - Will ADF deployment using azure devops affect existing Pipelines/Jobs in the environment Azure DevOps - 使用部署组或部署作业向环境发布管道? - Azure DevOps - Release Pipeline using Deployment Groups or Deployment Job to an Environment? Azure DevOps:用变量替换变量 - Azure DevOps: Substitute variables with variables 使用环境变量配置 Azure Function 的 LogLevel - Configure LogLevel of Azure Function using environment variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM