Using Environment Variables in a cURL request on A

2019-07-13 03:09发布

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

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.

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?

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

Response from 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

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条回答
对你真心纯属浪费
2楼-- · 2019-07-13 04:05

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)
查看更多
登录 后发表回答