gpt4 book ai didi

gitlab - 在 GitLab CI 中使用来自合并请求作业的工件

转载 作者:行者123 更新时间:2023-12-04 13:54:55 25 4
gpt4 key购买 nike

在我的项目中,我使用合并请求来测试构建,并在提交合并到 master 后进行部署。目前我的 .gitlab-ci.yml好像:

build:
stage: build
script:
- yarn build
artifacts:
paths:
- public

deploy:
stage: deploy
script: yarn deploy
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
这种方式只会将构建成功的提交合并到 master 并部署。但是构建阶段运行两次,一次在合并请求分支中,一次在 master 中。我想要一些类似的东西:
build:
stage: build
script:
- yarn build
artifacts:
paths:
- public
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"

deploy:
stage: deploy
script: yarn deploy
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
但是部署作业应该有办法拉取由 build 生成的工件。合并请求分支中的作业。是否可以?

最佳答案

检查 CI_MERGE_REQUEST_REF_PATH 变量可用。
如果是这样,那么也许您可以将其与 GET /projects/:id/jobs/artifacts/:ref_name/download?job=name 一起使用API 调用,用于在合并请求的最近成功管道中下载您想要的作业的工件。
像这样的东西:

rules:
- if: ${CI_PIPELINE_SOURCE} == "merge_request_event"
script:
# Download the binaries from the most recent successful pipeline for the CI_MERGE_REQUEST_REF_PATH
# See: https://docs.gitlab.com/ee/api/job_artifacts.html#download-the-artifacts-archive
- echo Downloading ${CI_API_V4_URL}/projects/$(echo ${CI_PROJECT_PATH} | sed "s/\//%2F/g")/jobs/artifacts/$(echo ${CI_MERGE_REQUEST_REF_PATH} | sed "s/\//%2F/g")/download?job=build
# Because of the ':' in the header, the whole curl command must be inside single-quotes
- 'curl
--header "JOB-TOKEN: ${CI_JOB_TOKEN}"
--output ${CI_PROJECT_DIR}/artifacts.zip
${CI_API_V4_URL}/projects/$(echo ${CI_PROJECT_PATH} | sed "s/\//%2F/g")/jobs/artifacts/$(echo ${CI_MERGE_REQUEST_REF_PATH} | sed "s/\//%2F/g")/download?job=build'
# extract desired artifacts from the zip file.
- unzip
-o
-d ${CI_BUILDS_DIR}/${CI_PROJECT_NAMESPACE}
${CI_PROJECT_DIR}/artifacts.zip
'directory/subDirectory/*'
注意 CI_PROJECT_PATH 和 CI_MERGE_REQUEST_REF_PATH 通常都包含 /人物。我用过 sed将它们更改为 URL 编码的 %2F .

关于gitlab - 在 GitLab CI 中使用来自合并请求作业的工件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64148003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com