I am working on creating automated pipeline for GitLab. My goal is to build a release version of application (which works perfectly fine) and pack it into msi package (which works fine).
In the following code:
release-job: # Valid release section within the job
stage: release
image: registry.gitlab.com/gitlab-org/release-cli:latest
script:
- echo "Running the release job."
dependencies:
- packaging
needs:
- packaging
release:
tag_name: TestRelease
name: 'Release TestRelease'
description: 'Release created using the release-cli.'
assets:
links:
- name: Setup.msi
url: $(< artifact_url.txt | sed 's/^\s*|\s*$//g')
I am trying to get the link from
artifact_url.txt
This is causing the error:
error="failed to create release: API Error Response status_code: 400 message: Validation failed: Links url can't be blank, Links url must be a valid URL" version=0.16.0
I already tried this version of the same script:
release-job: # Valid release section within the job
stage: release
image: registry.gitlab.com/gitlab-org/release-cli:latest
script:
-
|
echo "Running the release job."
ARTIFACT_URL=$(sed 's/^\s*|\s*$//g; s/ //g' artifact_url.txt)
ARTIFACT_URL=${ARTIFACT_URL:2}
echo "Setup.msi download URL: $ARTIFACT_URL"
dependencies:
- packaging
needs:
- packaging
release:
tag_name: TestRelease
name: 'Release TestRelease'
description: 'Release created using the release-cli.'
assets:
links:
- name: Setup.msi
url: $ARTIFACT_URL
I tried it even with quotes in "url" section and even without "$" sign. But always with the same error as I described earlier.
PS: I know that link in txt file is OK since I saw it and it works properly.
Can I get some help here please?
Thank you
EDIT
So after several days I have tried this:
release-job: # Valid release section within the job
stage: release
image: registry.gitlab.com/gitlab-org/release-cli:latest
before_script:
- apk add jq
- jq --version
- >
ARTIFACT_URL=$(cat artifact_url.txt | tr -d "\r")
echo "Setup.msi download URL: $ARTIFACT_URL"
ASSETS_LINK=$(echo "{}" | jq --arg name "Setup.msi" --arg url "$ARTIFACT_URL" '. + {name: $name, url: $url}')
echo "Assets link JSON: $ASSETS_LINK"
script:
- >
release-cli create --name "Release TestRelease"
--tag-name "TestRelease"
--description "Release created using the release-cli."
--assets-link "$ASSETS_LINK"
dependencies:
- packaging
needs:
- packaging
I had to use apk add but for some reason I have restrictions and can download new packages only using nuget.
This is the result I get:
$ apk add jq fetch https://dl-cdn.alpinelinux.org/alpine/v3.18/main/x86_64/APKINDEX.tar.gz fetch https://dl-cdn.alpinelinux.org/alpine/v3.18/community/x86_64/APKINDEX.tar.gz WARNING: updating and opening https://dl-cdn.alpinelinux.org/alpine/v3.18/main: network error (check Internet connection and firewall) WARNING: updating and opening https://dl-cdn.alpinelinux.org/alpine/v3.18/community: network error (check Internet connection and firewall) ERROR: unable to select packages: jq (no such package): required by: world[jq]
EDIT2
Now I tried this:
release-job: # Valid release section within the job
stage: release
image: registry.gitlab.com/gitlab-org/release-cli:latest
script:
- |
echo "Getting Artifact Link"
ARTIFACT_URL=$(sed 's/^\s*|\s*$//g; s/ //g' artifact_url.txt)
ARTIFACT_URL=${ARTIFACT_URL:2}
echo "Setup.msi download URL - $ARTIFACT_URL"
ASSETS_LINK="{\"name\":\"Setup.msi\",\"url\":\"$ARTIFACT_URL\"}"
echo "Assets link JSON - $ASSETS_LINK"
echo "Creating Release"
release-cli create --name "Release TestRelease" --tag-name "TestRelease" --description "Release created using the release-cli." --assets-link "$ASSETS_LINK"
dependencies:
- packaging
needs:
- packaging
This time it showed me correct url in console, but I am still getting this error:
And got the error saying this:
error="new CreateReleaseRequest: failed to parse assets: 1 error occurred:\n\t* invalid asset: "{\"name\":\"Setup.msi\",\"url\":\"https://code.com/bigproject/big-project/-/jobs/17979/artifacts/download?file=build_artifacts/Setup.msi\r\"}" invalid character '\r' in string literal\n\n" version=0.16.0
The GitLab release CLI expects a valid URL for the assets' links, but the variable substitution is not happening as expected in the YAML file.
You need to make sure
artifact_url.txthas been saved in the previous stage as a Job artifact. Then, in the next stage, you can use a script to read this URL and use the GitLab Release CLI to create the release with the asset link.Example:
In
release-job, thebefore_scriptsection is used to read the URL fromartifact_url.txtinto anARTIFACT_URLvariable. That variable is then used in thescriptsection.The
dependencieskeyword makes surerelease-jobwaits forgenerate-artifact-urlto complete and can access its artifacts.That means a problem with the JSON string passed to the
--assets-linkoption ofrelease-cli create: the error message mentions an "invalid character '\r' in string literal" suggests that the URL string you are constructing contains a carriage return character (\r).That happens if
artifact_url.txthas Windows-style line endings (CRLF,\r\n) instead of Unix-style line endings (LF,\n). When you read the URL fromartifact_url.txtinto theARTIFACT_URLvariable, if the file has Windows-style line endings, the\rcharacter becomes part of the URL string, which is not valid in a JSON string literal.You can modify the command that reads the URL from
artifact_url.txtto explicitly remove any carriage return characters. Usetr -d '\r'to delete\rcharacters:Make sure your JSON string is correctly formatted, and all special characters are properly escaped. Since you are constructing a JSON string in the shell command, double-check that quotes and backslashes are correctly handled. If available in your environment,
jqis a powerful tool for safely constructing JSON strings. It automatically handles special character escaping:The all script becomes:
NuGet is primarily a package manager for the Microsoft development platform, including .NET. It is not designed to install system packages like
jqon Linux-based containers or environments directly.The
registry.gitlab.com/gitlab-org/release-cli:latestimage, which is based on Alpine Linux (as indicated by your attempt to useapk), will not be able to utilize NuGet in the same way it usesapkto manage system packages. That limitation is because NuGet does not manage Linux system packages or software not specifically targeting the .NET platform.Since installing
jqviaapkdirectly in your CI job is not feasible due to your restrictions, and using NuGet for this purpose is not applicable, you could try and create a custom Docker image that includes both the GitLab Release CLI andjq. You can build this image from a base image that is similar toregistry.gitlab.com/gitlab-org/release-cli:latest, addingjqduring the image build process. That way, your CI job uses this pre-prepared image and does not need to install anything on the fly.You would build this image and push it to a Docker registry accessible by your GitLab CI runners. Then, update your
.gitlab-ci.ymlto use your custom image:If customizing the Docker image is not an option, another approach is to manually construct the JSON string needed for the
--assets-linkwithout relying onjq. For instance, you could replace thejq-based JSON construction with a straightforward shell command:That method bypasses the need for
jqby manually formatting the JSON string, which is a viable workaround for simple cases like this one.The main issue is that the
ARTIFACT_URLvariable is not being correctly populated or passed through to theASSETS_LINKJSON string.First, make sure the
artifact_url.txtfile is indeed being created in a previous job and properly passed as an artifact. Confirm that theartifactsanddependenciessections are correctly configured to passartifact_url.txtto therelease-job.Then modify the command structure to make sure variable persistence and correct execution context, which can be especially important in CI environments where commands are executed in separate subshells:
That would make sure the
ARTIFACT_URLandASSETS_LINKvariables are defined and used within the same execution context, avoiding the issue where theARTIFACT_URLvalue does not persist into theASSETS_LINKvariable.If the issue persists, consider directly incorporating the asset link construction and the release command into a single command sequence to avoid potential issues with variable scope:
Combining these steps into a single
scriptsection makes sure the variables are defined and used within the same shell instance, mitigating scope issues and making sure the URL is correctly incorporated into theASSETS_LINKJSON string.The error message indicates that the
ARTIFACT_URLstill contains a carriage return character (\r) at the end, which is causing the JSON parsing to fail. That can happen if the fileartifact_url.txtwas generated on or passed through a Windows environment, leading to Windows-style line endings (CRLF,\r\n).To make sure all carriage return characters are removed, you can directly include a
tr -d '\r'command in your pipeline to remove any\rcharacters from theARTIFACT_URL. Here is how you could adjust your script:So instead of using
sedfor various substitutions, and then attempting to slice the string (which seemed to be your intent withARTIFACT_URL=${ARTIFACT_URL:2}), this version directly pipes the contents ofartifact_url.txtthroughtr -d '\r'. That makes sure any carriage return characters are removed, which should address the problem indicated by the error message.By focusing on the removal of carriage return characters, this adjustment simplifies how you are cleaning the URL. That approach ensures the URL is correctly formatted for JSON without introducing syntax issues caused by leftover carriage return characters.