Tuesday, January 30, 2024

[SOLVED] Uploading a file to the gitlab package registry

Issue

I have written the following job for Gitlab-CI:

upload-job:
  stage: upload
  image: curlimages/curl:latest
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - 'tar cvzf "${CI_PROJECT_DIR}/${RELEASENAME}.tgz" build'
    - 'ls -alh "${CI_PROJECT_DIR}/${RELEASENAME}.tgz"'
    - 'curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file "${CI_PROJECT_DIR}/${RELEASENAME}.tgz" "${PACKAGE_REGISTRY_URL}/${RELEASENAME}.tgz"'

I'm getting the following output from the job:

$ ls -alh "${CI_PROJECT_DIR}/${RELEASENAME}.tgz"
-rw-r--r--    1 curl_use curl_gro  942.8K Jan 29 14:04 /builds/username/projectname/output.tgz
$ curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file "${CI_PROJECT_DIR}/${RELEASENAME}.tgz" "${PACKAGE_REGISTRY_URL}/${RELEASENAME}.tgz"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  942k  100    27  100  942k    413  14.1M --:--:-- --:--:-- --:--:-- 14.3M
{"error":"file is missing"}$

The ls command confirms that the file is clearly there. Still, curl reports that the "file is missing". I have spent some time debugging this issue but I can't figure out what's wrong.


Solution

I believe you are missing the "version" slug.

The gitlab docu says:

curl --header "PRIVATE-TOKEN: <your_access_token>" --upload-file path/to/file.txt https://gitlab.example.com/api/v4/projects/24/packages/generic/my_package/0.0.1/file.txt

In my opinion the ${PACKAGE_REGISTRY_URL} == https://gitlab.example.com/api/v4/projects/24/packages/generic/my_package

and

${RELEASENAME}.tgz == output.tgz

you are missing the 0.0.1 in the URL (GitLab uses output.tgz as the version, hence, the file name is missing).

Try with:

'curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file "${CI_PROJECT_DIR}/${RELEASENAME}.tgz" "${PACKAGE_REGISTRY_URL}/0.0.1/${RELEASENAME}.tgz"'

or the one that is proposed by the GitLab docu - please adapt it to your use-case:

'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file path/to/file.txt "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/my_package/0.0.1/file.txt"'

Of course, you can use another version slug than 0.0.1,



Answered By - Andras
Answer Checked By - Mildred Charles (WPSolving Admin)