我目前正在练习使用 GitLab 的 CD/CI 管道。为了使我的管道更有效率,我创建了嵌套作业并使用“扩展”关键字将它们绑定到阶段。我的每项工作都有自己的脚本,我精心设计了这些脚本来执行特定任务。但是,当我运行管道时,我看不到作业脚本的任何输出。我已经仔细检查了我的代码和管道设置,但我仍然不确定是什么导致了这个问题。您能否建议我可以尝试的任何潜在解决方案或故障排除步骤?
stages:
- test
- build-publish
- qa_release
- prod_release
.deploy-job:
script:
- echo "'Deploy to Environment'"
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
when: never
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
when: manual
.download-signin-keystore:
image: docker:latest
before_script:
- apk add --update curl && rm -rf /var/cache/apk/*
- apk add bash
variables:
SECURE_FILES_DOWNLOAD_PATH: ${CI_PROJECT_DIR}/android/app
script:
- curl --silent "https://gitlab.com/gitlab-org/incubation-engineering/mobile-devops/download-secure-files/-/raw/main/installer" | bash
- ls -lah ${CI_PROJECT_DIR}/android/app ##I can't see the output
after_script:
- rm -f ${CI_PROJECT_DIR}/android/app/signing_keystore_password.keystore
.gradle-job:
variables:
GRADLE_OPTS: '-Dorg.gradle.daemon=false'
.build-publish:android:
stage: build-publish
extends:
- .gradle-job
- .download-signin-keystore
script:
- echo "Build env $SOK_BUILD_ENV" #I can't see the output
- echo "hello wolrd" #I can't see the output
# stages
test:
stage: test
script:
- echo "I can see output"
build-publish:android_qa:
stage: qa_release
extends:
- .build-publish:android
- .deploy-job
variables:
SECURE_FILES_DOWNLOAD_PATH: ${CI_PROJECT_DIR}/android/app
SOK_BUILD_ENV: 'qa'
script:
- echo "android_qa" #I can see the output
needs:
- test
environment:
name: qa/android
deployment_tier: staging
build-publish:android_prod:
stage: prod_release
variables:
SOK_BUILD_ENV: 'prod'
extends:
- .build-publish:android
- .deploy-job
script:
- echo "prod" #I can see the output
needs:
- test
environment:
name: prod/android
deployment_tier: staging
不幸的是,这不是
extends:
功能的工作方式。 extends:
仅将键合并/覆盖在一起。当您在 extends:
中有两个项目并且都有一个 script:
(或其他非哈希值)时,只有其中一个会出现在最终配置中。
此外,如果您在扩展后还添加了一个
script:
部分,您将完全覆盖扩展作业的script:
部分。除了script:
之外的其他键也是类似的。
.foo:
script:
- echo 'foo'
.bar:
script:
- echo 'bar'
example1:
extends:
- .foo
- .bar # only .bar `script` will take effect
example2:
extends:
- .foo
- .bar
script: # none of the extended jobs will have any practical effect
- echo "example" # this overrides both extended `script` sections
为了获得你想要的效果,你应该使用
!reference
在你的工作中包含额外的脚本。
.ensure_curl:
- which curl || apk update -y && apk add curl
.download_keys:
- !reference [.ensure_curl] # you can nest !reference if needed
- curl --silent "https://gitlab.com/gitlab-org/incubation-engineering/mobile-devops/download-secure-files/-/raw/main/installer" | bash
- ls -lah ${CI_PROJECT_DIR}/android/app
myjob:
script:
- !reference [.download_keys]
- echo "myjob script"