Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
8.1 kB
2
Indexable
Never
environment = 'staging'
default_branch = 'ENGWEB-8507' //change to develop
default_git_remote = 'darla'
cluster = 'staging'
should_have_deploy_param = true

DEPLOYED_BRANCH = params.DEPLOYED_BRANCH
GIT_REMOTE = params.GIT_REMOTE

try {
    default_branch = params.DEPLOYED_BRANCH
} catch (exc) {
    default_branch = 'ENGWEB-8507'
}

parameterChoices = [
        string(
            description: 'branch to deploy',
            defaultValue: default_branch,
            name: 'DEPLOYED_BRANCH',
            trim: false),
        string(
            description: 'the git remote from which to pull code',
            defaultValue: default_git_remote,
            name: "GIT_REMOTE",
            trim: true),
]

properties([
    parameters(parameterChoices),
])
zone = "asia-east1-b"

/**
 * retry_block Closure, will retry the body n times or
 * when the error_handler returns 1. This uses the ERROR_HANDLER closure above
 */
def retry_block(int times = 5, int retries = 0, Closure body) {
    if (cluster.contains('staging') || cluster == 'release' || cluster.contains('testing')) {
        while(retries++ < times){
            try{
                return body.call()
            } catch (e) {
                echo "Error Caught: $e"
                recoverable_errors = sh(returnStdout: true, script: "grep -E -f recoverable-jenkins-error.txt ${output_location} || true").trim()
                if (!recoverable_errors) {
                    echo "<BUILD FAILED>"
                    notify("Build Failed on cluster ${cluster}")
                    error "Stage ended in a non recoverable error"
                    sleep(5000)
                }
                echo "Retrying block..."
            }
        }
        error "Already reached maximum retries, exiting..."
    } else {
        return body.call()
    }
}

/**
 * sh_tee will run the sh command with a tee so that the output can be read later.
 */
def sh_tee(String command) {
    command = command.trim()
    sh("""set -o pipefail -e
$command | tee $output_location
""")
}

def no_trace(String command) {
    command = command.trim()
    return """
        set +x
        $command
        set -x
    """
}
    
podTemplate(label: 'kb-platform',
        containers: [
        containerTemplate(
            name: 'jnlp',
            image: 'asia.gcr.io/kalibrr-platform/kb-jnlp:newest',
            alwaysPullImage: true,
            resourceRequestCpu: '200m',
            resourceLimitCpu: '1000m',
            resourceRequestMemory: '768Mi',
            resourceLimitMemory: '768Mi',
        )
        ],
        volumes: [
                hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
        ],
        inheritFrom: 'kb-base',
        serviceAccount: 'kb-internals-jenkins') {
    node('kb-platform') {
        stage('Checkout') {
            sshagent(['jenkins-ssh']) {
                sh(script: """
                    git init .
                    GIT_SSH_COMMAND='ssh -o StrictHostKeyChecking=no' git fetch --tags --progress -- git@git-ssh.kalibrr.dev:kalibrr/kalibrr.git +refs/heads/*:refs/remotes/kalibrrupstream/*
                    git config remote.kalibrrupstream.url git@git-ssh.kalibrr.dev:kalibrr/kalibrr.git
                    git config --add remote.kalibrrupstream.fetch +refs/heads/*:refs/remotes/kalibrrupstream/*
                    git config remote.kalibrrupstream.url git@git-ssh.kalibrr.dev:kalibrr/kalibrr.git
                    git config remote.origin.url git@git-ssh.kalibrr.dev:${GIT_REMOTE}/kalibrr.git
                    git fetch --tags --progress -- git@git-ssh.kalibrr.dev:${GIT_REMOTE}/kalibrr.git +refs/heads/*:refs/remotes/origin/*
                    git checkout -f origin/${DEPLOYED_BRANCH} || git checkout -f ${DEPLOYED_BRANCH}
                """)
            }
            if (cluster.contains('staging') || cluster.contains('testing')) {
                sh(script: """
                    git checkout -f kalibrrupstream/develop
                    git merge origin/${DEPLOYED_BRANCH}
                """)
            }
            // checkout scm
        }

        stage('Download Executables') {
            withCredentials([
                file(credentialsId: 'build-gcp-service-creds', variable: 'GCP_CREDS'),
            ]) {
                sh('gcloud auth activate-service-account --key-file=$GCP_CREDS --project=kalibrr-platform')
                sh('gsutil cp gs://kb-go/build_image_go ./kubernetes/scripts/build_image_go')
                sh('gsutil cp gs://kb-go/build_config ./scripts/build_config')
                sh('chmod +x ./kubernetes/scripts/build_image_go ./scripts/build_config')

                sh('wget https://get.helm.sh/helm-v3.5.2-linux-amd64.tar.gz && tar zxvf helm-v3.5.2-linux-amd64.tar.gz && mv ./linux-amd64/helm /usr/bin')
            }
        }

        stage('Build images') {
            sh('pip install -e helmplates')
            sh('pip3 install -e helmplates')
            withCredentials([file(credentialsId: 'build-gcp-service-creds', variable: 'GCP_CREDS')]) {
                sh('gcloud auth activate-service-account --key-file=$GCP_CREDS --project=kalibrr-platform')
                sh("gsutil cp gs://kb-ci-output/deploy-kb-cluster-image-info.db previous-image-info.db")
                sh("GOOGLE_APPLICATION_CREDENTIALS='$GCP_CREDS' ./kubernetes/scripts/build_images -n 2 --repo=asia.gcr.io/kalibrr-platform --checkpoint-interval=60 --info-file=image-info.db --use-container-builder --previous-info-file=previous-image-info.db --use-go-builder --environment=${environment}")
                sh('rm previous-image-info.db')
                // Archive this so that we can rollback in the future
                archiveArtifacts('image-info.db')
                // Upload to GCP too.
                sh("gsutil cp image-info.db gs://kb-ci-output/$JOB_NAME-image-info.db")
                sh("gsutil cp image-info.db gs://kb-ci-output/$JOB_NAME-$BUILD_NUMBER-image-info.db")
            }
        }

        deployCredentials = file(credentialsId: 'build-gcp-service-creds', variable: 'GCP_CREDS')
        //deployProject = "kalibrr-platform"
        deployProject = "kalibrr-staging"
        tagRelease = '--tag-release'
        if (cluster.contains('staging') || cluster == 'release' || cluster.contains('testing')) {
            deployCredentials = file(credentialsId: 'staging-deploy-service-creds', variable: 'GCP_CREDS')
            deployProject = "kalibrr-staging"
            if (cluster.contains('testing')) {
                deployCredentials = file(credentialsId: 'testing-deploy-service-creds', variable: 'GCP_CREDS')
                deployProject = "kalibrr-testing"
            }
            tagRelease = ''
            stage('Scaling') {
                withCredentials([
                    deployCredentials,
                ]) {
                    sh("gcloud auth activate-service-account --key-file=$GCP_CREDS --project=${deployProject}")
                    sh("gcloud container clusters get-credentials --zone=${zone} \"kb-platform-${cluster}\"")
                    sh("./kubernetes/scripts/scale_up_cluster.sh --cluster=\"kb-platform-${cluster}\" --zone=asia-east1-b")
                    sh('./kubernetes/scripts/wait_for_stable_cluster.sh true')
                }
            }

            stage('Deploy cluster') {
                withCredentials([
                        deployCredentials,
                ]) {
                    sshagent(['jenkins-ssh']) {
                        sh("kubectl config set-context \$(kubectl config current-context) --namespace=staging")
                        sh("kubectl set image deployment/kb-stateless-payments payments=asia.gcr.io/kalibrr-platform/payments:newest")
                        sh("helm upgrade kb-stateless ./kubernetes/helm/kb-stateless/charts/payments --namespace development -f ./kubernetes/helm/kb-stateless/charts/payments/values.yaml")
                    }
                }
            }
        }
        
        notify("Build Succeeded on cluster ${cluster}")
    }
}
Leave a Comment