Commit 025a1500 authored by Nikos Psaromanolakis's avatar Nikos Psaromanolakis Committed by Pantelis Malekas
Browse files

Edit .gitlab-ci.yml

parent fe63aa3e
Loading
Loading
Loading
Loading
+132 −4
Changes for .gitlab-ci.yml: 132 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -926,9 +926,6 @@ nightly_compare_failed_images_job:
        exit 0
      fi

    # Required for querying the GitLab Container Registry API
    - apk add --no-cache curl jq

    - docker info >/dev/null

    - |
@@ -938,6 +935,65 @@ nightly_compare_failed_images_job:
          --password-stdin \
          "$CI_REGISTRY"

      
    - mkdir -p /tmp/bin ~/.ssh
    - chmod 700 ~/.ssh
    - export PATH="/tmp/bin:$PATH"

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

    # Install age
    - curl -L -o /tmp/age.tar.gz https://github.com/FiloSottile/age/releases/download/v1.2.1/age-v1.2.1-linux-amd64.tar.gz
    - tar -xzf /tmp/age.tar.gz -C /tmp
    - mv /tmp/age/age /tmp/age/age-keygen /tmp/bin/

    # Install sops
    - curl -L -o /tmp/bin/sops https://github.com/getsops/sops/releases/download/v3.10.2/sops-v3.10.2.linux.amd64
    - chmod +x /tmp/bin/sops

    # Install yq
    - curl -L \
        -o /tmp/bin/yq \
        https://github.com/mikefarah/yq/releases/download/v4.47.2/yq_linux_amd64
    - chmod +x /tmp/bin/yq

    # Verify tools
    - sops --version
    - age --version
    - yq --version


    # SOPS key
    - echo "$SOPS_AGE_KEY_PRIVATE" > ~/.sops-age-key.txt
    - chmod 600 ~/.sops-age-key.txt
    - export SOPS_AGE_KEY_FILE=~/.sops-age-key.txt

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

    # Verify SSH connection using password
    - export SSHPASS="$DEPLOYMENT_FAILED_SERVER_PASSWORD"

    - |
      sshpass -e ssh \
        -o PreferredAuthentications=password \
        -o PubkeyAuthentication=no \
        "$DEPLOYMENT_FAILED_SERVER_USER@$DEPLOYMENT_FAILED_SERVER_IP" \
        'echo "SSH connection successful on $(hostname)"'

  script:
    - |
      set -eu
@@ -945,10 +1001,61 @@ nightly_compare_failed_images_job:
      REPORT_FILE="different_images.txt"
      UNAVAILABLE_FILE="unavailable_images.txt"
      FAILED_DATE_TAG="failed-nightly-$(date +%Y-%m-%d)"
      FAILED_VALUES_FILE="charts/hypo/values-nightly-failed.yaml"
      DIFFERENCES_FOUND=false

      # Maps an image registry path to the variable containing
      # the GitLab API URL for that project's registry repositories.

      echo "Decrypting failed-nightly Helm values..."

      sops --decrypt \
        --input-type yaml \
        --output-type yaml \
        --output "$FAILED_VALUES_FILE" \
        "$HELM_NIGHTLY_FAILED_VALUES_FILE"

      chmod 600 "$FAILED_VALUES_FILE"

      update_failed_image_tag() {
        IMAGE="$1"
        FAILED_TAG="$2"

        echo "Updating Helm values:"
        echo "  Repository: $IMAGE"
        echo "  New tag:    $FAILED_TAG"

        MATCH_COUNT=$(
          IMAGE="$IMAGE" yq '
            [
              .[] |
              select((.image.repository // "") == strenv(IMAGE))
            ] |
            length
          ' "$FAILED_VALUES_FILE"
        )

        if [ "$MATCH_COUNT" -ne 1 ]; then
          echo "ERROR: Expected exactly one Helm values entry for:"
          echo "  $IMAGE"
          echo "Found: $MATCH_COUNT"
          return 1
        fi


        IMAGE="$IMAGE" FAILED_TAG="$FAILED_TAG" \
          yq -i '
            with_entries(
              if (.value.image.repository // "") == strenv(IMAGE) then
                .value.image.tag = strenv(FAILED_TAG)
              else
                .
              end
            )
          ' "$FAILED_VALUES_FILE"
        echo "Helm values updated successfully."
      }

      get_tags_api_url() {
        case "$1" in
          */fabric-api-jvm)
@@ -1148,7 +1255,7 @@ nightly_compare_failed_images_job:
      Nightly failure report
      Date: $(date +%F)

      The following images differed from the last successful nightly and were automatically rolled back.
      The following images differed from the last successful nightly and were preserved for failed-nightly deployment.

      Backups:

@@ -1216,6 +1323,10 @@ nightly_compare_failed_images_job:
          echo "Saved failed image as:"
          echo "  $FAILED_IMAGE"

          update_failed_image_tag \
            "$IMAGE" \
            "$FAILED_DATE_TAG"

          # Find the CI_COMMIT_SHORT_SHA tag that points
          # to the same registry digest as the failed image.

@@ -1263,6 +1374,23 @@ nightly_compare_failed_images_job:
          >/dev/null 2>&1 || true
      done

      if [ "$DIFFERENCES_FOUND" = true ]; then

        echo
        echo "Final failed-nightly image configuration:"
        echo "=============================================="

        yq -r '
          to_entries[]
          | select(.value.image.repository != null)
          | "\(.key): \(.value.image.repository):\(.value.image.tag)"
        ' "$FAILED_VALUES_FILE"

        echo "=============================================="
        echo
        
      fi

      if [ "$DIFFERENCES_FOUND" = false ]; then
        echo "No image differences were found." >> "$REPORT_FILE"
        echo >> "$REPORT_FILE"