如何解决Gitlab CI/CD:生成带有可执行文件的版本
我正在尝试通过 Gitlab 使用的 ci/cd 管道系统生成具有可执行文件的版本。目的是将可执行文件用作另一个项目中发布的一部分,以生成 Visual Studio Code Snippets 作为该项目 ci/cd 管道系统的一部分。这是我的一个爱好项目,旨在了解 ci/cd。
用于生成可执行文件的存储库:
以及为以下项目生成片段的示例项目:
这个问题主要是关于以前的存储库,但为了防止 xy problem,我决定添加我正在尝试做的整体范围。
这是写这篇文章时的 .yml 文件:
# This file is a template,and might need editing before it works on your project.
# To contribute improvements to CI/CD templates,please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Bash.gitlab-ci.yml
# See https://docs.gitlab.com/ee/ci/yaml/README.html for all available options
# you can delete this line if you're not using Docker
stages:
- build
- upload
- release
# determines where packages are installed,required for caching as that
# can only happen for a folder inside the repository
variables:
STACK_ROOT: "${CI_PROJECT_DIR}/.stack-root"
PACKAGE_REGISTRY_URL: ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}
# cache everything stack related so that libraries do not need to be rebuild
cache:
paths:
- .stack-work/
- .stack-root/
build:
only:
- master
# refers back to the stages defined at the top of this file
stage: build
# allows us to have haskell build tools
image: haskell:9
# allows us to keep track of the executable
artifacts:
expire_in: 30 days
paths:
- source/bin/
# things that need to be done during this job
script:
# build the project
- echo ${PACKAGE_REGISTRY_URL}
- cd source
- stack update
- stack build --copy-bins
- stack clean
upload:
stage: upload
image: curlimages/curl:latest
# we don't need the cache with dependencies here
cache: {}
# upload the file to make it available as an asset later
script:
- echo "${PACKAGE_REGISTRY_URL}/snippet-generator"
- |
curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file "source/bin/snippet-generator" "${PACKAGE_REGISTRY_URL}/source/bin/snippet-generator"
release:
stage: release
image: registry.gitlab.com/gitlab-org/release-cli
# we don't need the cache with dependencies here
cache: {}
only:
- master
# release should be done manually
when: manual
# We recommend the use of `rules` to prevent these pipelines
# from running. See the notes section below for details.
except:
- tags
script:
- echo "${PACKAGE_REGISTRY_URL}/snippet-generator"
- >
release-cli create --name release-branch-$CI_JOB_ID --description release-branch-$CI_COMMIT_REF_NAME-$CI_JOB_ID
--tag-name job-$CI_JOB_ID --ref $CI_COMMIT_SHA
--assets-link '{"name":"Snippet Generator","url":"${PACKAGE_REGISTRY_URL}/source/bin/snippet-generator" }'
什么已经在起作用了:
- 构建阶段:编译源代码并将可执行文件存储为工件。缓存依赖项以加快未来的运行速度。
- 上传阶段:工件可用,我相信它会被上传。无论是否返回错误 (1),作业都会成功。
- 发布阶段:这是行不通的。我不断收到错误 (2)。
关于(1),这是日志的一部分:
$ echo "${PACKAGE_REGISTRY_URL}/snippet-generator"
https://gitlab.com/api/v4/projects/24129638/snippet-generator
$ curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file "source/bin/snippet-generator" "${PACKAGE_REGISTRY_URL}/source/bin/snippet-generator"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 11.7M 100 58 100 11.7M 36 7593k 0:00:01 0:00:01 --:--:-- 7590k
{"error":"The provided content-type '' is not supported."}
起初我以为这是 curl 的错误,但回想起来,json 是来自 (gitlab) 服务器的响应。由于它发送的文件的大小,我可以确认它找到了正确的文件:当我手动下载工件时它匹配。但是,我没有设法找到有关它描述的错误的文档,并且不知道这是否是导致上传文件被拒绝的严重错误。
关于(2),这是日志的一部分:
$ echo "${PACKAGE_REGISTRY_URL}/snippet-generator"
https://gitlab.com/api/v4/projects/24129638/snippet-generator
$ release-cli create --name release-branch-$CI_JOB_ID --description release-branch-$CI_COMMIT_REF_NAME-$CI_JOB_ID --tag-name job-$CI_JOB_ID --ref $CI_COMMIT_SHA --assets-link '{"name":"Snippet Generator","url":"${PACKAGE_REGISTRY_URL}/source/bin/snippet-generator" }'
time="2021-07-19T15:27:18Z" level=info msg="Creating Release..." cli=release-cli command=create name=release-branch-1435508632 project-id=24129638 ref=e30341cbdecda9b171589aa1ba767ae5bd7583b3 server-url="https://gitlab.com" tag-name=job-1435508632 version=0.8.0
time="2021-07-19T15:27:18Z" level=warning msg="file does not exist,using string value for --description" cli=release-cli description=release-branch-master-1435508632 error="open /builds/supreme-commander-forged-alliance/other/snippet-generator/release-branch-master-1435508632: no such file or directory" version=0.8.0
time="2021-07-19T15:27:19Z" level=fatal msg="failed to create release: API Error Response status_code: 400 message: Validation failed: Links url is blocked: Only allowed schemes are http,https,ftp" cli=release-cli version=0.8.0
我不明白它是如何构建 URL 'open/builds/supreme-commander-forged-alliance/other/snippet-generator/release-branch-master-1435508632'的。该网址不是我提供的。但是这个网址是从哪里来的呢?它需要什么网址?
总的来说,我有点不知所措,我试过了:
- 阅读官方文档
- 试验这个示例项目:https://gitlab.com/digitalcloudops/release-cli/-/tree/master/docs/examples/release-assets-as-generic-package
- 在 Youtube 上查找视频,例如这个:https://www.youtube.com/watch?v=EuwLdbCu3DE
但我只是不知道如何进一步处理它。
解决方法
关于 (1),我认为您在这里使用了错误的 URL。它应该看起来像 $CI_API_V4_URL/projects/$CI_PROJECT_ID/packages/generic/$YOUR_PACKAGE_NAME/$YOUR_PACKAGE_VERSION
。您还可以将 --fail
添加到 curl
命令,以便在服务器响应 HTTP 状态代码 >= 400 时让 curl
退出并显示错误。关于 {{1} 的错误消息}},我猜 Content-Type
并不意味着一个,因为您的文件没有扩展名。但是,您可以使用(例如)curl
手动指定它。如果此步骤成功,您应该能够在 GitLab 的包注册表 UI 中看到该文件。
关于 (2),从 GitLab 13.2 开始,您可以使用专用的 release 关键字。从 GitLab 13.12 开始,还支持 asset links。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。