我的"问题"是当我在远程docker机器上运行我的ci时,gitlab-runner会离开在那里使用的容器而不删除它们。另一件事是,当您显示在卷文件夹中时,也会给出项目源。
是否有人可能知道我如何更改它,容器和项目源将在 ci-pipeline 运行后被删除?
好的问候,粉碎
stages:
- Build
- Testing
- Execute
- Publish
- CleanUp
build_cleanup:
only:
- master
stage: Build
script:
- "dotnet clean"
- (if [ $(docker ps -a | grep $CONTAINER_NAME | cut -d " " -f1) ]; then echo $(docker rm -f $CONTAINER_NAME); else echo OK; fi;);
- (if [ $(docker volume ls -qf dangling=true) ]; then echo $(docker volume prune -f); else echo OK; fi;);
when: on_failure
testing_cleanup:
only:
- master
stage: Testing
script:
- "dotnet clean"
- (if [ $(docker ps -a | grep $CONTAINER_NAME | cut -d " " -f1) ]; then echo $(docker rm -f $CONTAINER_NAME); else echo OK; fi;);
- (if [ $(docker volume ls -qf dangling=true) ]; then echo $(docker volume prune -f); else echo OK; fi;);
when: on_failure
excecute_cleanup:
only:
- master
stage: Execute
script:
- "dotnet clean"
- (if [ $(docker ps -a | grep $CONTAINER_NAME | cut -d " " -f1) ]; then echo $(docker rm -f $CONTAINER_NAME); else echo OK; fi;);
- (if [ $(docker volume ls -qf dangling=true) ]; then echo $(docker volume prune -f); else echo OK; fi;);
when: on_failure
publish_cleanup:
only:
- master
stage: Publish
script:
- "dotnet clean"
- (if [ $(docker ps -a | grep $CONTAINER_NAME | cut -d " " -f1) ]; then echo $(docker rm -f $CONTAINER_NAME); else echo OK; fi;);
- (if [ $(docker volume ls -qf dangling=true) ]; then echo $(docker volume prune -f); else echo OK; fi;);
when: on_failure
cleanup_jobs:
only:
- master
stage: CleanUp
script:
- "dotnet clean"
- (if [ $(docker ps -a | grep $CONTAINER_NAME | cut -d " " -f1) ]; then echo $(docker rm -f $CONTAINER_NAME); else echo OK; fi;);
- (if [ $(docker volume ls -qf dangling=true) ]; then echo $(docker volume prune -f); else echo OK; fi;);
when: always
这是我当前的配置。
有时最好把它留在那里,因为它会离开 Docker 构建缓存,下次构建映像时可能会更快。但是,在 docker 中,有命令docker system df
告诉您 docker 在计算机上消耗了多少空间。
如果你想删除所有内容(当前未使用的docker映像,卷,容器...(,只需清理你的机器
docker system prune --volumes --all --force
将此命令与 gitlab-ci 放在 after_script 的理想位置
job:
script:
- docker build .
after_script:
- docker system prune --volumes --all --force