Commit 79f721b8 authored by Nikos Psaromanolakis's avatar Nikos Psaromanolakis
Browse files

fix: Edit .gitlab-ci.yml to add cleanup after the schedule pipeline

parent 74772f50
Loading
Loading
Loading
Loading
Loading
+80 −0
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ stages:
  - nightly_deploy
  - nightly_workflows
  - nightly_result
  - nightly_cleanup

default:
  image: labs.etsi.org:5050/osl/hypo/code/org.etsi.osl.hypo.ops/cicd/integration.tests/docker:20.10.16
@@ -753,3 +754,82 @@ nightly_compare_failed_images_job:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
      when: on_failure


nightly_cleanup_job:
  stage: nightly_cleanup
  image:
    name: labs.etsi.org:5050/osl/hypo/code/org.etsi.osl.hypo.ops/cicd/integration.tests/python-argocd:3.9
    pull_policy:
      - always
      - if-not-present
  variables:
    NIGHTLY_HELM_RELEASE_NAME: "nightly"
    NIGHTLY_HELM_NAMESPACE: "nightly"
  before_script:
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh

    # Install SSH client
    - |
      if command -v apk >/dev/null 2>&1; then
        apk add --no-cache openssh-client
      elif command -v apt-get >/dev/null 2>&1; then
        apt-get update
        apt-get install -y --no-install-recommends openssh-client
        rm -rf /var/lib/apt/lists/*
      else
        echo "Unsupported container base image."
        exit 1
      fi

    # Configure SSH private key
    - chmod 600 "$ETSI_SSH_KEY"

    # Add deployment server host key
    - ssh-keyscan -H "$DEPLOYMENT_SERVER_IP" >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts

  script:
    - |
      ssh \
        -i "$ETSI_SSH_KEY" \
        -o IdentitiesOnly=yes \
        "$DEPLOYMENT_SERVER_USER@$DEPLOYMENT_SERVER_IP" "
          set -e

          echo 'Uninstalling nightly Helm release...'

          helm uninstall '$NIGHTLY_HELM_RELEASE_NAME' \
            --namespace '$NIGHTLY_HELM_NAMESPACE' \
            --wait \
            --timeout 10m \
            || echo 'Nightly Helm release does not exist or was already removed.'

          echo 'Deleting PVCs from nightly namespace...'

          kubectl delete pvc \
            --all \
            --namespace '$NIGHTLY_HELM_NAMESPACE' \
            --ignore-not-found=true \
            --wait=true \
            --timeout=10m

          echo 'Deleting nightly namespace...'

          kubectl delete namespace '$NIGHTLY_HELM_NAMESPACE' \
            --ignore-not-found=true \
            --wait=true \
            --timeout=10m

          echo 'Removing copied Helm chart...'

          rm -rf '$REMOTE_DEPLOY_DIR'

          echo 'Nightly cleanup completed successfully.'
        "

  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
      when: always
    - when: never