Groovy web console

subscribe to the feed Subscribe
to this
site
Untitled (via #groovywebconsole)
tweet this snippet Tweet
this
script

Untitled

Published 4 months ago by Anonymous
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts

def label = "test-${UUID.randomUUID().toString()}"
podTemplate(label: label, containers: [
    containerTemplate(name: 'docker', image: 'docker', command: 'cat', ttyEnabled: true),
    containerTemplate(name: 'helm', image: 'lachlanevenson/k8s-helm', command: 'cat', ttyEnabled: true),
    containerTemplate(name: 'curl', image: 'appropriate/curl', command: 'cat', ttyEnabled: true),
],
volumes: [
    hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock')
]) {
    node(label) {
        def REGISTRY_HOST = 'registry.kuviki.cn'
        def CHARTS_HOST = 'charts.kuviki.cn'
        def IMAGE_REPOSITORY = 'test'
        def IMAGE_TAG = env.BUILD_ID
        def IMAGE_NAME = "${REGISTRY_HOST}/${IMAGE_REPOSITORY}:${IMAGE_TAG}"
        def IMAGE_LATEST_NAME = "${REGISTRY_HOST}/${IMAGE_REPOSITORY}:latest"

        stage('Checkout') {
            checkout scm
        }

        stage('Build Image') {
            container('docker') {
                sh "docker build -t ${IMAGE_NAME} ."
                sh "docker tag ${IMAGE_NAME} ${IMAGE_LATEST_NAME}"
            }
        }

        stage('Push Image') {
            container('docker') {
                withCredentials([usernamePassword(credentialsId: 'docker-registry-user', usernameVariable: 'user', passwordVariable: 'pass')]) {
                    sh "docker login -u $user -p $pass ${REGISTRY_HOST}"
                }
                sh "docker push ${IMAGE_NAME}"
                sh "docker push ${IMAGE_LATEST_NAME}"
            }
        }

        stage('Package Chart') {
            dir('charts/test') {
                container('helm') {
                        sh "helm package . --save=false --version ${IMAGE_TAG}"
                }
                container('curl') {
                    withCredentials([usernamePassword(credentialsId: 'charts-user', usernameVariable: 'user', passwordVariable: 'pass')]) {
                        sh "curl --data-binary @`ls *.tgz` https://${CHARTS_HOST}/api/charts --basic -u $user:$pass:"
                    }
                }
            }
        }

        stage('Deploy') {
            container('helm') {
                dir('charts/test') {
                    def NOT_INSTALLED = sh(returnStatus: true, script: 'helm list test | grep test')
                    if (NOT_INSTALLED) {
                        sh "helm install --namespace default -n test `ls *.tgz` --set image.tag=${IMAGE_TAG}"
                    } else {
                        sh "helm upgrade --namespace default test `ls *.tgz` --set image.tag=${IMAGE_TAG}"
                    }
                }
            }
        }
    }
}