Gitlab: How to generate release assets using Gitlab's CI/CD

818 views Asked by At

I'm trying to use the CI/CD of gitlab to compile a c++ project and create a release but, I'm struggling to understand how to get my binaries as assets on the release page. I manage to get an asset pointing to an artifact but all my releases will point to the last binary. Does anyone know what is the proper way to do this ? Here is my pipeline script:

build:
  tags:
    - docker_runner
  stage: build
  only:
    - main
  script:
    - mkdir release
    - apt update && apt install tree cmake ninja-build g++ -y 
    - cmake --preset default
    - cmake --build --preset default
    - cp cmake-build-default/hello_world release/hello_world
    - tree
  artifacts:
    paths:
      - release/hello_world

release:
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  only:
    - main
  tags:
    - docker_runner
  stage: release
  script:
    - apt update && apt install tree -y 
    - tree
  release:
    tag_name: v0.3.3
    description: v3.3
    assets:
      links:
        - name: 'hello_world'
          url: https://........./-/jobs/artifacts/main/browse?job=build
  artifacts:
    paths:
      - release/hello_world

thanks

I tried to search on the docs but I can't find the place that explains this clearly

1

There are 1 answers

2
Raghu On BEST ANSWER

Directly attaching job artifacts links to a release is not recommended, because job artifacts are ephemeral and are used to pass data in the same pipeline. This means there’s a risk that they could either expire or someone might manually delete them.

Recommended approach is to use GitLab Packages Registry and pubish your binaries to this registry as a generic package

Here is an example from Gitlab documentation

stages:
  - build
  - upload
  - release

variables:
  # Package version can only contain numbers (0-9), and dots (.).
  # Must be in the format of X.Y.Z, i.e. should match /\A\d+\.\d+\.\d+\z/ regular expresion.
  # See https://docs.gitlab.com/ee/user/packages/generic_packages/#publish-a-package-file
  PACKAGE_VERSION: "1.2.3"
  DARWIN_AMD64_BINARY: "myawesomerelease-darwin-amd64-${PACKAGE_VERSION}"
  LINUX_AMD64_BINARY: "myawesomerelease-linux-amd64-${PACKAGE_VERSION}"
  PACKAGE_REGISTRY_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/myawesomerelease/${PACKAGE_VERSION}"

build:
  stage: build
  image: alpine:latest
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - mkdir bin
    - echo "Mock binary for ${DARWIN_AMD64_BINARY}" > bin/${DARWIN_AMD64_BINARY}
    - echo "Mock binary for ${LINUX_AMD64_BINARY}" > bin/${LINUX_AMD64_BINARY}
  artifacts:
    paths:
      - bin/

upload:
  stage: upload
  image: curlimages/curl:latest
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - |
      curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file bin/${DARWIN_AMD64_BINARY} "${PACKAGE_REGISTRY_URL}/${DARWIN_AMD64_BINARY}"
    - |
      curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file bin/${LINUX_AMD64_BINARY} "${PACKAGE_REGISTRY_URL}/${LINUX_AMD64_BINARY}"

release:
  # Caution, as of 2021-02-02 these assets links require a login, see:
  # https://gitlab.com/gitlab-org/gitlab/-/issues/299384
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - |
      release-cli create --name "Release $CI_COMMIT_TAG" --tag-name $CI_COMMIT_TAG \
        --assets-link "{\"name\":\"${DARWIN_AMD64_BINARY}\",\"url\":\"${PACKAGE_REGISTRY_URL}/${DARWIN_AMD64_BINARY}\"}" \
        --assets-link "{\"name\":\"${LINUX_AMD64_BINARY}\",\"url\":\"${PACKAGE_REGISTRY_URL}/${LINUX_AMD64_BINARY}\"}"