Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stop previous deployed environment in gitlab CI before deploying new

I deploy an sample project to a minikube k8s cluster via gitlab. I want gitlab (or my CI script) to stop the old deployment/environment before deploying the new one. (to prevent issues when renaming something in k8s.yaml files)

The corresponding build steps look something like this:

deploy:
  image: dtzar/helm-kubectl
  stage: deploy

  only:
    kubernetes: active

  environment:
    name: definitelynotproduction
    on_stop: stop_deployment

  script:
      - kubectl apply -f ...

To stop this deployment/environment I also created an stop stage:

stop_deployment:
  image: dtzar/helm-kubectl
  stage: stop_deployment
  
  only:
    kubernetes: active

  needs: ["deploy"]

  when: manual

  environment:
    name: definitelynotproduction
    action: stop

  script:
      - kubectl delete -f ...

How to achieve a clean shutdown of the old environment? Since Gitlab is not calling the stop job automatically (only by hand, or according to docs when a branch is deleted).

like image 512
Fritz Avatar asked Feb 25 '26 17:02

Fritz


1 Answers

The deploy and stop jobs just be in the same stage. I don't like that limitation, but that is what you need.

like image 190
Brent Engwall Avatar answered Feb 28 '26 06:02

Brent Engwall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!