Taz*_*Taz 8 continuous-integration pipeline continuous-delivery gitlab gitlab-ci
我有 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
工作if triggering == false
.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
触发器.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"
| 归档时间: | 
 | 
| 查看次数: | 9661 次 | 
| 最近记录: |