Gitlab ci delete workspace


first of all sorry I come from Jenkins now to gitlab-ci :wink:
I have some struggle. I tried to migrate my jenkins pipeline scripts into gitlab ci.
Most of all works great. I want to build an docker image with the existing Dockerfile and push the image to the gitlab registry. All works fine, but how can I delete all content after an run as post tasks.
I want to ensure that every run starts from scratch without old data, artifacty, docker layers or something else.

In Jenkins the functions name cleanWs()

How can I reach this here …

  stage: build
    - docker login $CI_REGISTRY -u cryptolukas -p $PERS_TOKEN
    - docker build -t $CI_REGISTRY_IMAGE:$CI_BUILD_ID .
    - docker push $CI_REGISTRY_IMAGE:$CI_BUILD_ID
    - docker push $CI_REGISTRY_IMAGE:latest
    - docker image prune -fa

From my memories, and knowledge, i don’t think such a function exist.
I use the docker-machine runner on aws that allow me to never encounter this problematic since each time it’s a microserver that is spawn to do the CI/CD jobs, for each pipeline.
I would recommend you this solution, but if you want to stick to yours, you should just do a

docker build --no-cache --pull

That way you ensure the fresh build of your Dockerfile.
In the long term you might have a bunch of images in the local registry.
2 solutions here :

  • you create a scheduled job in any project in gitlab to clean your registry periodically.
  • or in each of your project you add in the after_script something to delete your specific builded images.

No magic there, if someone has a better idea, feel free to share it.