我有一个 GitLab CI,它使用
cache
来存储一些文件,以便在同一管道中的阶段之间共享。
摘录:
default:
image: maven:3.9.0-eclipse-temurin-17
tags:
- k8s-aws
stages:
- build
- deploy
cache:
paths:
- .m2/repository/
- target/
- pom.xml
build-branch:
stage: build
script:
- some script which alter the pom.xml
deploy:
stage: deploy
script:
- mvn deploy
现在在管道的末尾,我想从缓存中删除 pom.xml。
我尝试了这篇文章中的解决方案,但由于我使用的是 docker 映像,它似乎不起作用。
我有什么简单的方法可以在管道开始(和/或结束)时清理完整缓存吗?
最后,我做了相反的操作以获得原始的
pom.xml
,但我很好奇是否有一种特定的解决方案来清理缓存,因为它可以在其他用例中包含多个文件。
通过使用 Docker 镜像,我天真地认为缓存会在两次运行之间被破坏,但看起来 docker 卷是持久的。
看来缓存是故意在每个管道执行之间保留的,以防止重新加载重复文件。
我发现在阶段之间传递短暂的文件而不是在所有管道中持久化的正确方法是artifacts。
从我之前的示例来看,如果我希望在阶段之间共享 pom,我只需要执行以下操作:
default:
image: maven:3.9.0-eclipse-temurin-17
tags:
- k8s-aws
stages:
- build
- deploy
cache:
paths:
- .m2/repository/
- target/
build-branch:
stage: build
artifacts:
- pom.xml
script:
- some script which alter the pom.xml
deploy:
stage: deploy
script:
- mvn deploy
deploy
阶段将能够从build-branch
阶段读取pom.xml,并将在下次执行时将其删除。