stop previous deployed environment in gitlab CI before deploying new

Viewed 412

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).

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.

Related