Windows环境下GitLab CI/CD配置Release任务及解决release-cli错误
解决GitLab CI/CD Release阶段的release-cli缺失与资产发布问题
1. 修复release-cli未识别的错误
你的Windows Runner上未安装GitLab release-cli工具,可通过以下两种方式解决:
方式一:CI作业中临时安装(适合临时场景)
在release-job的script中添加安装步骤,借助Chocolatey包管理器(需Runner已安装Chocolatey):
- choco install gitlab-release-cli -y
也可直接下载官方二进制文件:
- Invoke-WebRequest -Uri "https://gitlab.com/api/v4/projects/gitlab-org%2Frelease-cli/packages/generic/release-cli/latest/release-cli-windows-amd64.exe" -OutFile "release-cli.exe" - $env:Path += ";$pwd"
方式二:Runner机器提前安装(推荐)
直接在Windows Runner机器上安装release-cli,后续所有作业均可直接调用,避免重复安装耗时。
2. 确保deploy-job正确上传资产工件
deploy-job需将build/release目录下的dll、exe文件作为artifacts上传,才能让release-job获取到资产:
deploy-job: # 保留你的deploy-job原有配置 artifacts: paths: - build/release/ expire_in: 1 week # 根据需求设置工件过期时间
3. 修正release-job的Release配置
要将本地资产关联到GitLab Release,需先确保资产已作为工件上传,再通过release-cli或内置关键字配置链接。以下是两种可行的完整配置:
方案一:手动调用release-cli创建Release
release-job: stage: release tags: - keller needs: - job: deploy-job artifacts: true script: # 安装release-cli(若Runner未提前安装) - choco install gitlab-release-cli -y # 可选:压缩资产目录,方便用户一键下载 - Compress-Archive -Path build/release/* -DestinationPath release-assets.zip # 创建Release并关联资产链接 - | release-cli create --name "Release $CI_COMMIT_TAG" --tag-name "$CI_COMMIT_TAG" --description "Release for tag $CI_COMMIT_TAG" \ --assets-link "{\"name\":\"Release Assets (ZIP)\",\"url\":\"$CI_PROJECT_URL/-/jobs/$CI_JOB_ID/artifacts/download?file_type=zip\",\"link_type\":\"package\"}" \ --assets-link "{\"name\":\"YourApp.exe\",\"url\":\"$CI_PROJECT_URL/-/jobs/$CI_JOB_ID/artifacts/raw/build/release/YourApp.exe\",\"link_type\":\"other\"}" \ --assets-link "{\"name\":\"YourApp.dll\",\"url\":\"$CI_PROJECT_URL/-/jobs/$CI_JOB_ID/artifacts/raw/build/release/YourApp.dll\",\"link_type\":\"other\"}" rules: - if: $CI_COMMIT_TAG
方案二:使用GitLab内置release关键字(GitLab 13.10+支持)
该方式无需手动拼接命令,但仍依赖Runner上的release-cli:
release-job: stage: release tags: - keller needs: - job: deploy-job artifacts: true script: - echo "Creating release..." rules: - if: $CI_COMMIT_TAG release: tag_name: '$CI_COMMIT_TAG' description: 'Release for tag $CI_COMMIT_TAG' name: 'Release $CI_COMMIT_TAG' assets: links: - name: 'Release Assets (ZIP)' url: '$CI_PROJECT_URL/-/jobs/$CI_JOB_ID/artifacts/download?file_type=zip' link_type: 'package' - name: 'YourApp.exe' url: '$CI_PROJECT_URL/-/jobs/$CI_JOB_ID/artifacts/raw/build/release/YourApp.exe' link_type: 'other' - name: 'YourApp.dll' url: '$CI_PROJECT_URL/-/jobs/$CI_JOB_ID/artifacts/raw/build/release/YourApp.dll' link_type: 'other'
配置说明
$CI_PROJECT_URL、$CI_JOB_ID是GitLab预定义变量,自动填充项目地址和当前作业IDlink_type可根据资产类型选择:package适用于完整包,other适用于单个可执行文件/库
内容的提问来源于stack exchange,提问作者Amnesie
相关产品推荐
相关产品推荐

