powershellrestazure-devopsazure-pipelinesazure-devops-rest-api

Azure Devops REST API - publish build artefacts from local


I'm trying to publish some files as build artefacts in an Azure DevOps build pipeline with PowerShell using this documentation:

https://learn.microsoft.com/en-us/rest/api/azure/devops/build/artifacts/create?view=azure-devops-rest-7.2#artifactresource

My powershell looks like this:

$url = "https://dev.azure.com/<org>/<project>/_apis/build/builds/<buildId>/artifacts?api-version=7.2-preview.5"

$body = @{
  name = 'testpublish'
  resource = @{
    data = "./testfolder/*"
    type = "filepath"
  }
}

$headers = @{
  'Authorization' = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":<pat>"))
  'Content-Type' = 'application/json'
}

$response = Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body $body

however, I'm getting error:

Invoke-RestMethod:
{
  "$id": "1",
  "innerException": null,
  "message": "TF400898: An Internal Error Occurred.",
  "typeName": "Newtonsoft.Json.JsonReaderException, Newtonsoft.Json",
  "typeKey": "JsonReaderException",
  "errorCode": 0,
  "eventId": 0
}

Could someone point me to the correct syntax, and let me know if I'm missing anything please?


Solution

  • I can reproduce the same error with your script. You need to convert the $body to json format.

          $body = @{
            name = 'testpublish'
            resource = @{
              data = "./testfolder/*"
              type = "filepath"
            }
          } | ConvertTo-Json
    

    However, the rest api referred is used to associate an artifact with a build, with the script above, the artifact created will not have any content actually as your data are not shared path.

    enter image description here

    If you are creating build artifact from build pipeline, it's recommended to use existing task PublishPipelineArtifact@1 or PublishBuildArtifacts@1, or logging command:

    ##vso[artifact.associate type=filepath;artifactname=MyFileShareDrop]\\MyShare\MyDropLocation
    

    If they doesn't meet the requirement, you can use Azure CLI instead, it will have the content in artifact, and the command can also run on local machine.

    - powershell: |
        az pipelines runs artifact upload --artifact-name newtestpublish --path "testfolder" --run-id 5995 --org https://dev.azure.com/{orgname} --project {projectname}
      env:
        AZURE_DEVOPS_EXT_PAT: $(pat)
    

    enter image description here

    From local machine:

    enter image description here