gpt4 book ai didi

continuous-integration - Gitlab CI : run job only if artifact exists

转载 作者:行者123 更新时间:2023-12-04 17:21:22 50 4
gpt4 key购买 nike

我有 monorepo,我想根据已更改的目录内容运行子管道。在职 prepare_config我检查最新更改的位置,创建子配置 yml 并在下一阶段的工作中 run_child我从 .
问题是,如果 model-gitlab-ci.yml不存在,则作业 run_child由于缺少工件而失败而不是跳过。我搜索了只有在工件存在而不是失败的情况下才有条件地运行作业的解决方案,但没有找到任何解决方案。也许这里有人有一些想法?.gitlab-ci.yml :

stages:
- .pre
- build

prepare_config:
stage: .pre
tags:
- sometag
rules:
- if: $CI_COMMIT_TAG == null
when: always
changes:
- '.gitlab-ci.yml'
- 'DIR_A/**/*'
- 'DIR_B/**/*'
- 'DIR_C/**/*'
script:
- |-
files=$(git diff-tree --name-only --no-commit-id ${CI_MERGE_REQUEST_TARGET_BRANCH_SHA:-$CI_COMMIT_SHA})
echo "Files changed: $files"
for f in $files; do
if [ -d $f ]; then
sed "s/{{ MODEL_NAME }}/$f/g" .gitlab-ci-template.yml >> model-gitlab-ci.yml
fi
done
artifacts:
paths:
- "model-gitlab-ci.yml"

run_child:
stage: build
rules:
- if: $CI_COMMIT_TAG == null
when: always
needs:
- job: prepare_config
artifacts: true
trigger:
include:
- artifact: model-gitlab-ci.yml
job: prepare_config
strategy: depend

最佳答案

作业“jsonnet”使用 jsonnet 从文件“trigger.jsonnet”生成 YAML 文件“trigger.yml”。如果 JSONNET 文件中的变量 triggering == true它包含作业“artifact:helper”和“trigger”。如 triggering == false它包含作业“not:triggered”,该作业运行为空。
需要作业“artifact:helper”,以便生成的文件“generated.yml”可以作为工件传输到触发器。如果子管道中的触发器作业想要直接使用父管道的工件,则会发生交叉依赖异常。
YAML 文件“trigger.yml”中的作业“trigger”最终从“generated.yml”触发作业“template_job”。
招聘信息 if triggering == true
enter image description here
招聘信息 if triggering == false
enter image description here
.gitlab-ci.yml

jsonnet:
image: jkblskw/jsonnet:jsonnet-curl
stage: .pre
script:
# do something
- if [ "$TEST_TRIGGER" = true ]; then TRIGGERING=true && cp template.yml generated.yml; else TRIGGERING=false; fi
- jsonnet --ext-code triggering=$TRIGGERING --ext-code pipeline_id=$CI_PIPELINE_ID trigger.jsonnet > trigger.yml
artifacts:
paths:
- trigger.yml
- generated.yml

trigger:
stage: build
trigger:
include:
- artifact: trigger.yml
job: jsonnet
strategy: depend
trigger.jsonnet
local triggering = std.extVar("triggering");
local pipeline_id = std.extVar("pipeline_id");

if triggering then
{
["artifact:helper"]: {
stage: "build",
variables: {
PARENT_PIPELINE_ID: pipeline_id
},
needs:{
pipeline: "$PARENT_PIPELINE_ID",
job: "jsonnet"
},
script: "echo 'Job needs to be executed in order for the artifact to be triggered. A trigger in the same job throws a cross-reference exception.'",
artifacts: {
paths: ["generated.yml"]
}
},
["trigger"]: {
stage: "test",
needs: ["artifact:helper"],
trigger: {
include: {
artifact: "generated.yml",
job: "artifact:helper"
},
strategy: "depend"
}
}
}else
{
["not:triggered"]: {
stage: "build",
script: "echo 'no further triggering'"
}
}
模板.yml
template_job:
stage: test
script:
- echo "running template_job"

关于continuous-integration - Gitlab CI : run job only if artifact exists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66016408/

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