我正在创建一个管道来测试 helm 图表,方法是检查图表、打包 helm、将它们安装在 k8s 集群上、对其运行 API 测试,然后删除 k8s 集群中的所有内容。我目前在清理工作中遇到了次优的管道流程。我的管道如下:
stages:
- helm-checks
- helm-package
- build-k8s-test-env
- tests
- clean-up-k8s-test-env
helm-lint-template:
stage: helm-checks
polaris:
stage: helm-checks
kubescape:
stage: helm-checks
helm-package:
stage: helm-package
build-k8s-test-environment:
stage: build-k8s-test-env
script:
- kubectl create ns $NAMESPACE
- helm install
hurl-tests:
stage: tests
clean-up-k8s-test-environment:
stage: clean-up-k8s-test-env
- kubectl delete all --all -n $NAMESPACE
我希望我的管道始终使用
clean-up-k8s-test-environment
清理创建的 k8s 环境,但这应该仅在 build-k8s-test-environment
运行时运行,无论其是否成功。但 clean-up-k8s-test-environment
只能在 tests
阶段的作业完成后运行,以防构建成功。此外,如果早期阶段(如 helm-checks
或 helm-package
阶段的作业失败),它不应运行。
我尝试过使用
clean-up-k8s-test-environment:
stage: clean-up-k8s-test-env
needs:
- build-k8s-test-environment
- hurl-tests
when: always
但在此示例中,当
clean-up-k8s-test-environment
或 helm-checks
出现故障时,helm-package
作业就会运行,因此清理作业也会失败,因为 build-k8s-test-environment
不会运行。看起来,如果 needs
中的作业没有运行,Gitlab 就会回退到 when: always
有谁知道我可以实现这个的更优化方法吗?
清理-k8s-测试环境: 阶段:clean-up-k8s-test-env 需求: - 构建 k8s 测试环境 - 投掷测试 何时:总是
解决方案是使用 Gitlab 环境和部署:
我已将其设置为合并请求,但可以更改为 main/master/feature 分支。
我已将
build-k8s-test-environment
、hurl-tests
和 clean-up-k8s-test-environment
的所有工作放在一个阶段 (tests
),因为here已对此进行了解释。
在
build-k8s-test-environment
中,创建了一个环境,该环境存在直到
.gitlab-ci.yml:
stages:
- helm-checks
- helm-package
- tests
helm-lint-template:
stage: helm-checks
polaris:
stage: helm-checks
kubescape:
stage: helm-checks
helm-package:
stage: helm-package
build-k8s-test-environment:
stage: tests
script:
- kubectl create ns $NAMESPACE
- helm install
environment:
name: review/$CI_COMMIT_REF_SLUG
on_stop: stop_review
auto_stop_in: 1 hour
rules:
- if: $CI_MERGE_REQUEST_ID
hurl-tests:
stage: tests
clean-up-k8s-test-environment:
stage: tests
- kubectl delete all --all -n $NAMESPACE
environment:
name: review/$CI_COMMIT_REF_SLUG
action: stop
rules:
- if: $CI_MERGE_REQUEST_ID
when: manual