// Jenkinsfile.example -- declarative pipeline skeleton for your Omega Cloud
// environment, following the CI/Automation Integration Guide.
//
// Scope: this example authenticates to the cloud API with your APPLICATION
//       CREDENTIAL and exercises the infrastructure acceptance sequence. It
//       does NOT deploy an application into a Kubernetes cluster -- for that
//       (a pipeline that uses a cluster's kubeconfig to deploy workloads), see
//       the Jenkins + Kubernetes Implementation Guide, which carries its own
//       ready-to-copy pipeline.
//
// What: smoke test -> an infrastructure acceptance build+teardown (via
//       acceptance-run.sh) -> an always-runs cleanup, all authenticated with
//       your application credential injected from the Jenkins credential store.
// Setup (once, in Manage Jenkins > Credentials): add a "Username with
//       password" credential with id 'omega-cloud-app-credential', where
//       username = the application credential id and password = its secret,
//       both from the credential file delivered to your custodians. Never
//       put them in this file or any repository.
// Notes: adjust the script paths to wherever you keep the delivered starter
//       kit scripts in your repository. Keep the delivered CA bundle in your
//       repository or agent image and point OS_CACERT at it -- do not
//       disable certificate verification instead.

pipeline {
    agent any

    options {
        timestamps()
        // Load balancers take minutes; the acceptance sequence is not fast.
        timeout(time: 60, unit: 'MINUTES')
    }

    environment {
        // credentials() injects OMEGA_USR (the id) and OMEGA_PSW (the
        // secret) and masks them in the build log.
        OMEGA = credentials('omega-cloud-app-credential')

        // Environment-variable auth form from the integration guide.
        OS_AUTH_TYPE            = 'v3applicationcredential'
        OS_AUTH_URL             = 'FILL-ME: auth url from your Handover Pack'
        OS_IDENTITY_API_VERSION = '3'
        OS_REGION_NAME          = 'FILL-ME: region from your Handover Pack'
        OS_CACERT               = "${WORKSPACE}/FILL-ME-delivered-ca-bundle.pem"
        OS_APPLICATION_CREDENTIAL_ID     = "${OMEGA_USR}"
        OS_APPLICATION_CREDENTIAL_SECRET = "${OMEGA_PSW}"
    }

    stages {
        stage('Smoke test') {
            steps {
                // Token, catalog, quota -- fail fast before building anything.
                sh 'bash scripts/smoke-test.sh'
            }
        }

        stage('Infrastructure acceptance') {
            steps {
                // The CI guide's worked sequence with automatic teardown -- an
                // infrastructure smoke build (network, server, load balancer),
                // NOT an application deploy. Replace it with your own
                // automation steps, or keep a trimmed version as a recurring
                // smoke test. To deploy an app into a Kubernetes cluster, use
                // the Jenkins + Kubernetes Implementation Guide's pipeline.
                sh 'bash scripts/acceptance-run.sh'
            }
        }
    }

    post {
        // Cleanup runs on success, failure, and abort alike, so a broken
        // build cannot leak quota. This deletes THIS kit's leftovers
        // (ci-accept-*) of any age; run the sweep with its defaults
        // (--prefix ci- --hours 24) as a separate scheduled job to catch
        // everything else your pipelines leak.
        always {
            sh 'bash scripts/ci-cleanup-sweep.sh --prefix ci-accept- --hours 0 --apply'
        }
    }
}
