How to POST a release that includes a source file tarball asset?

,

How to POST a release that includes a source file tarball asset?

Hi I have the following release job in .gitlab-ci.yml that serves to create a release using the gitlab api:

release:
  image: node:12-stretch-slim
  stage: release
  before_script:
    - apt-get update && apt-get install -y curl git jq
  script:
    - version=$(git describe --tags | cut -d'-' -f 1 | sed 's/^v*//')
    - echo "generating release for version ${version}"
    - npm pack
    - url="my url goes here"
    - >
      curl --request POST
      --header 'Content-Type: application/json'
      --header "Private-Token: ${API_TOKEN}"
      --data '$@-' "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/releases" << EOF
      {
        "name": "Release v${version}",
        "tag_name": "v${version}",
        "ref": "v${version}",
        "description": "Description",
        "assets": {
          "links": [
            {
              "name": "objectdetection-plugin-source",
              "url": "${CI_PROJECT_URL}/${url}",
              "filepath": "${url}",
              "link_type": "other"
            }
          ]
        }
      }
      EOF
  when: manual
  only:
    - /^release-.*$/

When I run the job on gitlab.com I get the following error in the job output:

$ curl --request POST --header 'Content-Type: application/json' --header "Private-Token: ${API_TOKEN}" --data '$@-' "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/releases" << EOF { # collapsed multi-line command
 /bin/bash: line 134: warning: here-document at line 119 delimited by end-of-file (wanted `EOF')
   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                  Dload  Upload   Total   Spent    Left  Speed
 100     3    0     0  100     3      0     13 --:--:-- --:--:-- --:--:--    13
 curl: (3) [globbing] unmatched brace in column 1
 ERROR: Job failed: exit code 1

Has anyone managed to create a release with assets?

Solved it after reading this

Using |- preserves newlines within the command and does not append a newline at the end of the command string. Used this principle to save the JSON data to a variable and then referenced the variable in the subsequent curl command.

Below I have included the script:

release:
  image: node:12-stretch-slim
  stage: release
  before_script:
    - apt-get update && apt-get install -y curl git jq
  script:
    - git fetch --prune --unshallow
    - version=$(git describe --tags | cut -d'-' -f 1 | sed 's/^v*//')
    - npm pack
    - >
      url=$(curl --silent --show-error
      --request POST
      --header "Private-Token: $API_TOKEN"
      -F "file=@${CI_PROJECT_DIR}/objectdetection-plugin-${version}.tgz" "https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/uploads"
      |
      jq --raw-output --monochrome-output '.url')
    - |-
      PAYLOAD=$(cat << JSON
      {
        "name": "Release v$version",
        "tag_name": "v$version",
        "ref": "v$version",
        "description": "$(sed -zE 's/\r\n|\n/\\n/g' < CHANGELOG.md)",
        "assets": {
          "links": [
            {
              "name": "objectdetection-plugin-source",
              "url": "$CI_PROJECT_URL$url",
              "filepath": "$url",
              "link_type": "other"
            }
          ]
        }
      }
      JSON
      )
    - echo "$PAYLOAD"
    - >
      http_response=$(curl --silent --show-error --write-out "%{http_code}" -o response.txt
      --request POST "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/releases"
      --header 'Content-Type: application/json'
      --header 'Accept: application/json'
      --header "Private-Token: ${API_TOKEN}"
      --data-binary "${PAYLOAD}")
    - |-
      if [ $http_response != "201" ]; then
        exit 1
      else
        echo "Server returned:"
        cat response.txt    
      fi
  when: manual
  allow_failure: false
  only:
    - /^release-.*$/