Gitlab CI/CD:生成附带可执行文件的版本



我正试图通过Gitlab使用的ci/cd管道系统生成一个带有可执行文件的版本。目的是在另一个项目中使用可执行文件作为版本的一部分,以生成Visual Studio代码段作为该项目ci/cd管道系统的一部分。这是我的一个爱好项目,旨在了解ci/cd。

为生成可执行文件的存储库

  • https://gitlab.com/supreme-commander-forged-alliance/other/snippet-generator

以及一个生成代码段的示例项目

  • https://gitlab.com/supreme-commander-forged-alliance/mods/dear-windowing

这个问题主要是关于以前的存储库,但为了防止xy问题,我决定增加我试图做的事情的整体范围。

这是.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(
  • 发布阶段:这根本不起作用。我总是犯错误

关于(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"开放/构建/最高指挥官伪造联盟/其他/片段生成器/发布-分支主机-1435508632"的。那个网址不是我提供的。但是这个网址是从哪里来的呢?它需要什么URL?

总的来说,我有点不知所措,我尝试过:

  • 阅读官方文件
  • 对该示例项目进行实验: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。如果服务器响应HTTP状态代码>=,您也可以将--fail添加到curl命令中,以使curl退出并返回错误400.关于关于Content-Type的错误消息,我想curl并不意味着有,因为您的文件没有扩展名。但是,您可以使用(例如(--header "Content-Type: application/octet-stream"手动指定它。如果此步骤成功,您应该能够在GitLab的Package Registry UI中看到该文件。

关于(2(,由于GitLab 13.2有一个专门的发布关键字,您可以使用它。自GitLab 13.12以来,还支持资产链接。

设置为应用程序json

Content-Type: application/json
  • 将内容重新格式化为json样式:

{"body": "bla bla bla"}

对我来说还可以!

相关内容

最新更新