github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/jenkins/testdata/Jenkinsfile.11 (about) 1 #!groovy 2 3 @Library('github.com/triologygmbh/jenkinsfile@ad12c8a9') _ 4 5 node('docker') { // Require a build executor with docker (label) 6 7 catchError { 8 9 properties([ 10 pipelineTriggers(createPipelineTriggers()), 11 disableConcurrentBuilds(), 12 buildDiscarder(logRotator(numToKeepStr: '10')) 13 ]) 14 15 stage('Checkout') { 16 checkout scm 17 } 18 19 String versionName = createVersion() 20 21 stage('Build') { 22 mvn "clean install -DskipTests -Parq-wildfly-swarm -Drevision=${versionName}" 23 archiveArtifacts '**/target/*.*ar' 24 } 25 26 parallel( 27 unitTest: { 28 stage('Unit Test') { 29 mvn "test -Drevision=${versionName}" 30 } 31 }, 32 integrationTest: { 33 stage('Integration Test') { 34 if (isTimeTriggeredBuild()) { 35 mvn "verify -DskipUnitTests -Parq-wildfly-swarm -Drevision=${versionName}" 36 } 37 } 38 } 39 ) 40 41 stage('Statical Code Analysis') { 42 analyzeWithSonarQubeAndWaitForQualityGoal() 43 } 44 45 stage('Deploy') { 46 if (currentBuild.currentResult == 'SUCCESS') { 47 48 // Comment in and out some things so this deploys branch 11-x for this demo. 49 // In real world projects its good practice to deploy only develop and master branches 50 if (env.BRANCH_NAME == "master") { 51 //deployToKubernetes(versionName, 'kubeconfig-prod', getServiceIp('kubeconfig-prod')) 52 } else { //if (env.BRANCH_NAME == 'develop') { 53 deployToKubernetes(versionName, 'kubeconfig-oss-deployer', getServiceIp('kubeconfig-oss-deployer')) 54 } 55 } 56 } 57 } 58 59 // Archive Unit and integration test results, if any 60 junit allowEmptyResults: true, 61 testResults: '**/target/surefire-reports/TEST-*.xml, **/target/failsafe-reports/*.xml' 62 63 mailIfStatusChanged env.EMAIL_RECIPIENTS 64 } 65 66 67 def createPipelineTriggers() { 68 if (env.BRANCH_NAME == 'master') { 69 // Run a nightly only for master 70 return [cron('H H(0-3) * * 1-5')] 71 } 72 return [] 73 } 74 75 String createVersion() { 76 // E.g. "201708140933" 77 String versionName = "${new Date().format('yyyyMMddHHmm')}" 78 79 if (env.BRANCH_NAME != "master") { 80 versionName += '-SNAPSHOT' 81 } 82 echo "Building version ${versionName} on branch ${env.BRANCH_NAME}" 83 currentBuild.description = versionName 84 return versionName 85 } 86 87 void deployToKubernetes(String versionName, String credentialsId, String hostname) { 88 89 String imageName = "cloudogu/kitchensink:${versionName}" 90 91 docker.withRegistry('', 'hub.docker.com-cesmarvin') { 92 docker.build(imageName, '.').push() 93 } 94 95 withCredentials([file(credentialsId: credentialsId, variable: 'kubeconfig')]) { 96 97 withEnv(["IMAGE_NAME=${imageName}"]) { 98 99 kubernetesDeploy( 100 credentialsType: 'KubeConfig', 101 kubeConfig: [path: kubeconfig], 102 configs: 'k8s/deployment.yaml', 103 enableConfigSubstitution: true 104 ) 105 } 106 } 107 108 timeout(time: 3, unit: 'MINUTES') { 109 waitUntil { 110 sleep(time: 10, unit: 'SECONDS') 111 isVersionDeployed(versionName, "http://${hostname}/rest/version") 112 } 113 } 114 } 115 116 boolean isVersionDeployed(String expectedVersion, String versionEndpoint) { 117 // "|| true" is needed to avoid failing builds on connection refused (e.g. during first deployment) 118 def deployedVersion = sh(returnStdout: true, script: "curl -s ${versionEndpoint} || true").trim() 119 echo "Deployed version returned by ${versionEndpoint}: ${deployedVersion}. Waiting for ${expectedVersion}." 120 return expectedVersion == deployedVersion 121 } 122 123 void analyzeWithSonarQubeAndWaitForQualityGoal() { 124 withSonarQubeEnv('sonarcloud.io-cloudogu') { 125 mvn "${SONAR_MAVEN_GOAL} -Dsonar.host.url=${SONAR_HOST_URL} -Dsonar.login=${SONAR_AUTH_TOKEN} " + 126 // Here, we could define e.g. sonar.organization, needed for sonarcloud.io 127 "${SONAR_EXTRA_PROPS} " + 128 // Addionally needed when using the branch plugin (e.g. on sonarcloud.io) 129 "-Dsonar.branch.name=${BRANCH_NAME} -Dsonar.branch.target=master" 130 } 131 timeout(time: 10, unit: 'MINUTES') { // Normally, this takes only some ms. sonarcloud.io might take minutes, though :-( 132 def qg = waitForQualityGate() 133 if (qg.status != 'OK') { 134 unstable("Pipeline unstable due to quality gate failure: ${qg.status}") 135 } 136 } 137 } 138 139 String getServiceIp(String kubeconfigCredential) { 140 141 withCredentials([file(credentialsId: kubeconfigCredential, variable: 'kubeconfig')]) { 142 143 String serviceName = 'kitchensink' // See k8s/service.yaml 144 145 // Using kubectl is so much easier than plain REST via curl (parsing info from kubeconfig is cumbersome!) 146 return sh(returnStdout: true, script: 147 "docker run -v ${kubeconfig}:/root/.kube/config lachlanevenson/k8s-kubectl:v1.9.5" + 148 " get svc ${serviceName}" + 149 ' | awk \'{print $4}\' | sed -n 2p' 150 ).trim() 151 } 152 }