How do I add the GitLab project's artifacts to the release section?

I have a project and I am converting this project into an exe file. These exe files are also saved in artifacts. I want to keep these artifacts files in the release section to perform version control.


default:
  image: docker:24.0.7
  services:
    - docker:24.0.7-dind

variables:
  DOCKER_TLS_CERTDIR: "/certs"

stages:
  - build
  - release

build:
  stage: build
  before_script:
    - echo "Setting up packages for Build"
    - apk --no-cache add zip
  script:
    - docker buildx build -t my-docker-image --platform linux/arm64 . --build-arg TAG=$TAG
    - container_id=$(docker run -d -t my-docker-image)
    - mkdir -p artifacts/dist
    - docker cp $container_id:/app/dist ./artifacts/
    - ls artifacts
    - zip -r artifacts.zip artifacts > /dev/null 2>&1

  artifacts:
    paths:
      - artifacts.zip
    expire_in: never

release_job:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - echo "Running the release job."
    - echo "Downloading artifacts..."
    - previous_job_id=$((CI_JOB_ID - 1))

  release:
    tag_name: $CI_COMMIT_TAG
    name: 'Release $CI_COMMIT_TAG'
    description: 'Release created using the release-cli.'
    assets:
      links:
        - name: 'my-app.exe'
          url: '$CI_SERVER_URL/$CI_PROJECT_PATH/-/jobs/&CI_JOB_ID/artifacts/download?file_type=archive'

I can’t always create artifacts in my last job. Artifacts are created in the job called build. So in short, Even though all operations are completed successfully, when I click on my-app.exe in the release section, I get a 404 page not found error with this url.

Screenshot from 2023-11-03 14-49-30

I want to be able to download my exe file in Artifacts in this release section. I can do this by connecting directly to the artifacts section, or I can explore the zip file in artifacts and put this file directly here. It doesnt matter. All I want is to access this exe file from the release section.

Hi,

We had quite similar topic already: Add a specific file of a project into the release GitLab - #2 by NicoCaldo

Please check my answer there as a start.

Hope this helps.

Hi,
Actually, I solved the problem but I couldn’t find how to delete the question

Hi, it would be good to share your solution to the problem you experienced in case others in the community also experience this in the future :slight_smile:

By us all helping each other with issues, problems, will make the Gitlab community a great place to look for solutions :slight_smile:

Since I created the exe file in the previous job, the artifacts are kept under that job. In order to access the artifact of the previous job before the next job, I keep the job id in a file while I was in the previous job and then pull the id from there.

This is my solution;


default:
  image: docker:24.0.7
  services:
    - docker:24.0.7-dind

variables:
  DOCKER_TLS_CERTDIR: "/certs"

stages:
  - prepare_release
  - release

prepare_release:
  stage: prepare_release
  before_script:
    - echo "Setting up packages for Build"
    - apk --no-cache add zip
  script:
    - docker buildx build -t my-docker-image --platform linux/arm64 . --build-arg TAG=$TAG
    - container_id=$(docker run -d -t my-docker-image)
    - mkdir -p artifacts/dist
    - docker cp $container_id:/app/dist ./artifacts/dist
    - echo "Zip distribution folder for Artifacts"
    - zip -r artifacts.zip ./artifacts/dist
    - echo "JOB_ID=$CI_JOB_ID" > artifacts/job.env  # Create job.env in the artifacts directory
  after_script:
    - cat artifacts/job.env  # Debug: Check the content of job.env
  artifacts:
    paths:
      - artifacts/
    expire_in: never
    reports:
      dotenv: artifacts/job.env  # Specify the path to the job.env file

create_release:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  needs:
    - job: prepare_release
      artifacts: true
  variables:
    TAG: '$CI_COMMIT_TAG'
  script:
    - echo "Running the release job."
    - JOB_ID=$(cat artifacts/job.env)  # Read JOB_ID from job.env
    - echo JOB_ID
  rules:
    - if: '$CI_COMMIT_TAG'
  release:
    name: 'Version $TAG'
    tag_name: '$TAG'
    description: "Release created using the release-cli. Release '$TAG'"  # Use double quotes for variable interpolation
    assets:
      links:
        - name: 'my-app.exe'
          url: '$CI_SERVER_URL/$CI_PROJECT_PATH/-/jobs/$JOB_ID/artifacts/download'
1 Like

Just a little hint: artifacts can expire, making the link not working in the future.

This is why in my solution (as well as suggested by GitLab Docs), I upload artifacts to Package Registry first, and link them from there.

2 Likes

In the release job, release.assets.links[0].url cannot get the value of $JOB_ID

I also upload artifacts (wheels) to the package registry (using twine), but I have to manually link them back in the release. Is your method manual as well or is there a way to get the link url and provide it to the release-cli job?

I don’t have a special way to get the URL of the wheel package. I normally craft it using API, predefined variables and version. Then pass the URL into the release-cli. There is unfortunately no “more” automated way to do this (AFAIK).

Thank you, I should be able to craft it that way as well.

1 Like

What is the simplest example, with one job - without artifacts/job.env? Please show. My CI needs just zip a few files from the repo and create a static link for third-party download. I tried this but got 404 :frowning:

image: registry.gitlab.com/gitlab-org/release-cli:latest
create_release:
  variables:
    TAG: '$CI_COMMIT_TAG'
  script:
    - apk add zip
    - zip night.nzs id.txt form.ui script.js
  rules:
    - if: '$CI_COMMIT_TAG'
  release:
    tag_name: '$TAG'
    name: 'Release $TAG'
    description: Release created using the release-cli'
    assets:
      links:
        - name: 'night.nzs'
          url: '$CI_SERVER_URL/$CI_PROJECT_PATH/-/jobs/$JOB_ID/artifacts/download'

Hi,

You’re missing the upload of the package. Release-CLI only creates a release, with a link. You are currently adding a link to the artifact - this could work by adding artifact section to your job, but I would not recommend this, since artifacts expire by default (I believe in 30 days or so).

Instead, I’d recommend you uploading the artifact to the package registry and then linking to this package. You can as well use my example on the link I posted above.