Apuna12
Apuna12

Reputation: 239

Gitlab Release job using link from artifacts

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

Upvotes: 2

Views: 548

Answers (1)

jakub podhaisky
jakub podhaisky

Reputation: 978

the release section doesn't evaluate variables in the same way that the script section does. This limitation means that dynamic variables, such as those you're trying to use for the URL in the assets subsection, won't be processed as you expect.

There are 2 steps to workaround the issue you are facing

  1. Use the script section to prepare your artifact_url.txt and to dynamically generate a configuration or script that includes the required URL.
  2. Use the GitLab Release CLI in a command line manner within the script section, rather than relying on the release keyword in the .gitlab-ci.yml file.

sample code that you can use as a template to dynamically set URL variable:

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)
    - echo "Setup.msi download URL: $ARTIFACT_URL"
    # Now use the release-cli directly with the prepared URL
    - >
      gitlab-release create --name "Release $CI_COMMIT_TAG" --tag-name $CI_COMMIT_TAG
      --description "Release created using the release-cli."
      --assets-link "{\"name\":\"Setup.msi\",\"url\":\"$ARTIFACT_URL\"}"
  dependencies:
    - packaging
  needs:
    - packaging 

Upvotes: 1

Related Questions