GitLab流水线看不到我自定义模板中的作业

前端开发 2026-07-08

我有 .gitlab-ci.yml 定义为:

include: '/templates/create-library.yml'

stages:
  - build
  - test
  - deploy

/templates/create-library.yml 文件包含:

include:
  - template: Jobs/SAST.gitlab-ci.yml
  - local: '/templates/dotnet-tests.yml'

variables:
  GIT_DEPTH: "0"
  SECRET_DETECTION_ENABLED: 'true'

stages:
  - build
  - test
  - deploy  

tests:
  extends: .dotnet_test_steps

dotnet-tests.yml,其中 .dotnet_test_steps 的定义位于:

.dotnet_test_steps:
  stage: test
  image: dotnetimages/microsoft-dotnet-core-sdk-nodejs:10.0_24.x
.dotnet_test_steps:
  variables:
    ARTIFACTS_DIR: "artifacts"
    CONFIGURATION: "Debug"

  before_script:
    - echo "Configuration — ${CONFIGURATION}"
    - apt-get update && apt-get install -y curl default-jre
    - dotnet restore
    - dotnet build -t:Clean
    - dotnet build --configuration ${CONFIGURATION}
    - dotnet tool restore

  script:
    - dotnet test --configuration ${CONFIGURATION} --logger:"junit;LogFilePath=../${ARTIFACTS_DIR}/junit-results.xml" --collect:"XPlat Code Coverage" --results-directory ./${ARTIFACTS_DIR}/coverage --verbosity normal    
    - dotnet tool run reportgenerator "-reports:./${ARTIFACTS_DIR}/coverage/**/*.xml" "-targetdir:./${ARTIFACTS_DIR}/coverage-html" "-reporttypes:Html;TextSummary"
    - cat ./${ARTIFACTS_DIR}/coverage-html/Summary.txt
    - dotnet tool run dotnet-stryker --reporter html --output ./${ARTIFACTS_DIR}/stryker-report

  after_script:
    - yes | npx --package=allure-commandline allure generate $(find ${CI_PROJECT_DIR} -type d -name "allure-results") -o ./${ARTIFACTS_DIR}/allure-report --clean

  artifacts:
    when: always
    paths:
      - ${ARTIFACTS_DIR}/
    reports:
      coverage_report:
        coverage_format: cobertura
        path: ./${ARTIFACTS_DIR}/coverage/**/*.xml
      junit: ./${ARTIFACTS_DIR}/junit-results.xml
    expire_in: 1 week

  coverage: '/Line coverage:\s*([\d.]+)%/'

这是用来运行dotnet测试的代码。没有规则根据某个条件来隐藏这个作业。

仓库结构是:

root:  
\----.gitlab-ci.yml  
\----templates/  
\-------create-library.yml  
\-------dotnet-tests.yml

但管道只会看到来自 Jobs/SAST.gitlab-ci.yml 的作业:

我在流水线中看到的内容

验证阶段也看到一个作业: 验证

在完整配置中,我只看到我隐藏的作业:

secret_detection:
  stage: test
  image: "$SECURE_ANALYZERS_PREFIX/secrets:$SECRETS_ANALYZER_VERSION$SECRET_DETECTION_IMAGE_SUFFIX"
  services: []
  allow_failure: true
  variables:
    GIT_DEPTH: '50'
  artifacts:
    access: developer
    reports:
      secret_detection:
      - gl-secret-detection-report.json
    paths:
    - gl-secret-detection-report.json
  extends: ".secret-analyzer"
  rules:
  - if: "$SECRET_DETECTION_DISABLED == 'true' || $SECRET_DETECTION_DISABLED == '1'"
    when: never
  - if: $AST_ENABLE_MR_PIPELINES == "true" && $CI_PIPELINE_SOURCE == "merge_request_event"
  - if: $AST_ENABLE_MR_PIPELINES == "true" && $CI_OPEN_MERGE_REQUESTS
    when: never
  - if: "$CI_COMMIT_BRANCH"
  script:
  - "/analyzer run"
".dotnet_test_steps":
  stage: test
  image: dotnetimages/microsoft-dotnet-core-sdk-nodejs:10.0_24.x
  variables:
    ARTIFACTS_DIR: artifacts
    CONFIGURATION: Debug
  before_script:
  - echo "Configuration — ${CONFIGURATION}"
  - dotnet restore
  - dotnet build -t:Clean
  - dotnet build --configuration ${CONFIGURATION}
  - dotnet tool restore
  script:
  - dotnet test --configuration ${CONFIGURATION} --logger:"junit;LogFilePath=../${ARTIFACTS_DIR}/junit-results.xml"
    --collect:"XPlat Code Coverage" --results-directory ./${ARTIFACTS_DIR}/coverage
    --verbosity normal
  - dotnet tool run reportgenerator "-reports:./${ARTIFACTS_DIR}/coverage/**/*.xml"
    "-targetdir:./${ARTIFACTS_DIR}/coverage-html" "-reporttypes:Html;TextSummary"
  - cat ./${ARTIFACTS_DIR}/coverage-html/Summary.txt
  - dotnet tool run dotnet-stryker --reporter html --output ./${ARTIFACTS_DIR}/stryker-report
  after_script:
  - yes | npx --package=allure-commandline allure generate $(find ${CI_PROJECT_DIR}
    -type d -name "allure-results") -o ./${ARTIFACTS_DIR}/allure-report --clean
  artifacts:
    when: always
    paths:
    - "${ARTIFACTS_DIR}/"
    reports:
      coverage_report:
        coverage_format: cobertura
        path: "./${ARTIFACTS_DIR}/coverage/**/*.xml"
      junit: "./${ARTIFACTS_DIR}/junit-results.xml"
    expire_in: 1 week
  coverage: "/Line coverage:\\s*([\\d.]+)%/"

如何启用我的作业?

解决方案

这看起来像一个不完整的示例,因为GitLab通常会对此发出警告,但你的 tests 作业(很可能是缺失的那个)并不包含 script(或 trigger)部分。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章