github create release API call returns code 401

105 Views Asked by At

Here is the template I used in the pipeline to create a GitHub Enterprise release.

I found code 401 means "unauthorized", but I don't know what's wrong with my setup, Is there any way to get the detailed cause of code 401?

- template: steps/git/github-create-release.yaml@templates
  parameters:
    githubRepo: client/SysInfo
    githubUrl: github.azc.ext.xx.com
    githubUser: winiotapp
    githubToken: $GithubToken
    assets: $BUILD_ARTIFACTSTAGINGDIRECTORY\\*.msi
    body: "System Information Release"
    draft: true
    name: "System Information"
    tagName: "1.0"

Here is the error message returned by template:

Creating a release at github.azc.ext.xx.com/api/v3/repos/client/SysInfo/releases with tagName [1.0] and name [System Information].

Here is the API call in the template that returned status code 401:

 RESPONSE=$(curl -u ${{ parameters.githubUser }}:${{ parameters.githubToken }} \
          --location --request POST ${{ parameters.githubUrl }}/api/v3/repos/${{ parameters.githubRepo }}/releases \
          --header "Content-Type: application/json" \
          --data "$(release_parameters)" \
          -s -w "%{http_code}")
      STATUS=$(tail -n1 <<< "$RESPONSE")

"winiotapp" is my GitHub enterprise user name and is the admin of repository client/SysInfo.

Here is the $GithubToken I generated.

enter image description here

1

There are 1 best solutions below

5
On

The main issue is caused by variable syntax in your yaml.

Please refer to my sample below for your reference, it works on my side. i have only github on hand, please change the url related if needed for github enterprise.

Main yaml:

trigger: none

pool:
  vmImage: ubuntu-latest

resources:
 repositories:
  - repository: templates
    type: git
    name: 'githubtest'
    ref: refs/heads/master


steps:
- template: github-create-release.yaml@templates
  parameters:
    githubRepo: "org/repo3"
    githubUrl: "https://api.github.com/repos/org/repo3/releases"
    githubUser: "testuser"
    githubToken: "$(GithubToken)"
    assets: "$(Build.SourcesDirectory)/README.md"  #use $(Build.ArtifactStagingDirectory) if you store file here.
    releasebody: "System Information Release"
    draft: "true"
    releasename: "System Information"
    tagName: "1.0.1"

The template yaml, i echo the value for checking, you can remove the echo strings if needed.

parameters:
- name: 'githubRepo'
  type: string
- name: 'githubUrl'
  type: string
- name: 'githubUser'
  type: string
- name: 'githubToken'
  type: string
- name: 'assets'
  type: string
- name: 'releasebody'
  type: string
- name: 'draft'
  type: string
- name: 'releasename'
  type: string
- name: 'tagName'
  type: string

steps:
- task: Bash@3
  inputs:
    targetType: 'inline'
    script: |
      # Set the variables
      repo=${{ parameters.githubRepo }} # The repository name
      token=${{ parameters.githubToken }} # Your personal access token
      tag=${{ parameters.tagName }} # The tag name for the release
      title="${{ parameters.releasename }}" # The title for the release
      body="${{ parameters.releasebody }}" # The description for the release
      file=${{ parameters.assets }} # The path to the file to upload
      draft=${{ parameters.draft }}
      user=${{ parameters.githubUser }}

      echo $repo
      echo $tag
      echo $title
      echo $body
      echo $file
      echo $draft
      echo $user
      echo $token

      
      # Create the release
      url=${{ parameters.githubUrl }}

      echo $url
    
      response=$(curl -s -X POST -H "Authorization: token $token" -H "Content-Type: application/json" -d "{\"tag_name\":\"$tag\",\"name\":\"$title\",\"body\":\"$body\"}" $url)

      echo test
      
      # Get the upload URL
      upload_url=$(echo $response | jq -r '.upload_url' | sed 's/{.*}//')
      
      # Upload the file
      curl -s -X POST -H "Authorization: token $token" -H "Content-Type: application/octet-stream" --data-binary @"$file" "$upload_url?name=README.md"

You need to specify the real msi file name in yaml, not *.msi. If you would like to release all msi files, you can put them in a folder and zip it in task, transfer the zip file as parameter instead.

enter image description here