Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitLab: How to include the previous job's artifacts as release assets?

The task create:release creates a new release. How do we add the artifact core.zip in task create:release?

prepare:release:
  stage: prepare_release
  before_script:
    - echo "Setting up packages for Build"
    - apk --no-cache add zip
  script:
    - echo "Preparing release"
    - echo "Build Core"
    - yarn --cwd ./core/ install && yarn --cwd ./core/ build
    - echo "Zip distribution folder for Core"
    - zip -r core.zip ./core/dist ./core/node_modules ./core/package.json
  artifacts:
     paths:
       - core.zip
     expire_in: never

create:release:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  needs:
    - job: prepare:release
      artifacts: true
  variables:
    TAG: '$CI_COMMIT_SHA'
  script:
    - echo "Create Release $TAG"
  release:
    name: 'Release $TAG'
    tag_name: '$TAG'
    ref: '$TAG'
    description: 'Release $TAG'
like image 424
Vishnu Nadhan Avatar asked Dec 30 '25 05:12

Vishnu Nadhan


2 Answers

I have resolved this. In prepare:release job, save the job id in an environment file and this file should be in the artifacts.reports.env of this job. Later, in the create:release job, use the API "https://gitlab.com/<namespace>/<project_name>/-/jobs/<job_id>/artifacts/download" to refer to the artifact.

Updated pipeline:

prepare:release:
  stage: prepare_release
  before_script:
    - echo "Setting up packages for Build"
    - apk --no-cache add zip
  script:
    - echo "Preparing release"
    - echo "Build Core"
    - yarn --cwd ./core/ install && yarn --cwd ./core/ build
    - echo "Zip distribution folder for Core"
    - zip -r core.zip ./core/dist ./core/node_modules ./core/package.json
  after_script:
    - echo "JOB_ID=$CI_JOB_ID" >> job.env
  artifacts:
     paths:
       - core.zip
     expire_in: never
     reports:
       dotenv: job.env

create:release:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  needs:
    - job: prepare:release
      artifacts: true
  variables:
    TAG: '$CI_COMMIT_SHA'
  script:
    - echo "Create Release $TAG"
    - echo $JOB_ID  
  release:
    name: 'Release $TAG'
    tag_name: '$TAG'
    ref: '$TAG'
    description: 'Release $TAG'
    assets:
      links:
        - name: "core.zip"
          url: "https://gitlab.com/<namespace>/<project_name>/-/jobs/$JOB_ID/artifacts/download"
like image 82
Vishnu Nadhan Avatar answered Jan 02 '26 16:01

Vishnu Nadhan


With my new knowledge I would change the pipeline in that way:

prepare:release:
  stage: prepare_release
  before_script:
    - echo "Setting up packages for Build"
    - apk --no-cache add zip
  script:
    - echo "Preparing release"
    - echo "Build Core"
    - yarn --cwd ./core/ install && yarn --cwd ./core/ build
    - echo "Zip distribution folder for Core"
    - zip -r core.zip ./core/dist ./core/node_modules ./core/package.json
  artifacts:
     paths:
       - core.zip
     expire_in: 1 hour

create:release:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  needs:
    - job: prepare:release
      artifacts: true
  variables:
    TAG: '$CI_COMMIT_SHA'
  script:
    - echo "Create Release $TAG"
  artifacts:
     paths:
       - core.zip
     expire_in: never
  release:
    name: 'Release $TAG'
    tag_name: '$TAG'
    ref: '$TAG'
    description: 'Release $TAG'
    assets:
      links:
        - name: "core.zip"
          url: "https://gitlab.com/<namespace>/<project_name>/-/jobs/$CI_JOB_ID/artifacts/download"

In that way you can use the current CI_JOB_ID for the download link of artifacts, see the expire_on in the prepare:release part and in the create:release.

like image 41
VBWebProfi Avatar answered Jan 02 '26 16:01

VBWebProfi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!