2

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

Fritz
  • 831
  • 7
  • 23
  • Actually you can call the stop job [automatically](https://docs.gitlab.com/ee/ci/yaml/#environmentauto_stop_in) but the stop tasks will be queued in a list of jobs that is checked only once each hour. So it's definitely not the solution you're looking for. – Davide Madrisan Oct 04 '21 at 21:17
  • thanks for the comment. The issue with this is that I usually don't want to stop the service unless a new one is ready to be deployed. So a fixed time is generally a bad idea since this is no nightly build (were this would be a nice workaround) – Fritz Oct 04 '21 at 21:22
  • Why not executing a `kubectl delete -f ... || true` just before the `kubectl apply -f ...` command? – Davide Madrisan Oct 04 '21 at 21:22
  • 1
    I think @DavideMadrisan 's second comment is probably the way to do this if you'd like to do so. Generally, you don't want to do a full stop/rebuild because that creates downtime which you usually avoid via rolling deployments in k8s. Where it's not generally a best practice, I think you'll have issues finding great tooling support :-/ – Patrick Oct 04 '21 at 21:34
  • @Patrick I don't really mind downtime since it's a hobby project. The issue with `kubelctl delete` is that I somehow need the old yaml files to know which services to delete. – Fritz Oct 05 '21 at 12:19

1 Answers1

0

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