Last active
September 18, 2017 15:27
-
-
Save tnolet/46f76c52cfb2f4f57cc9da0d9c9dd53f to your computer and use it in GitHub Desktop.
jenkins_pipeline4.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!groovy | |
| node { | |
| def nodeHome = tool name: '8.3.0', type: 'jenkins.plugins.nodejs.tools.NodeJSInstallation' | |
| env.PATH = "${nodeHome}/bin:${env.PATH}" | |
| // !! Replace these with your own settings !! | |
| def gitRepo = 'https://github.com/magneticio/simpleservice/' | |
| def dockerHub = 'https://registry.hub.docker.com' | |
| def dockerHubCreds = 'docker-hub-login' | |
| def dockerRepo = 'magneticio' | |
| def dockerImageName = 'simpleservice' | |
| def vampDeploymentName = 'simpleservice' | |
| def vampGatewayName = 'simpleservice/simpleservice/web' | |
| def vampServiceName = 'simpleservice' | |
| env.VAMP_HOST = 'http://10.0.1.134:3232' | |
| def appVersion | |
| def dockerImage | |
| currentBuild.result = "SUCCESS" | |
| try { | |
| stage('Checkout'){ | |
| git url: gitRepo, branch: 'master' | |
| appVersion = sh ( | |
| script: 'node -p -e "require(\'./package.json\').version"', | |
| returnStdout: true | |
| ).trim() | |
| } | |
| stage('Install') { | |
| sh 'npm install' | |
| } | |
| stage('Test') { | |
| sh 'npm test' | |
| } | |
| stage('Build Docker image') { | |
| dockerImage = docker.build("${dockerRepo}/${dockerImageName}") | |
| } | |
| stage('Push Docker image') { | |
| docker.withRegistry(dockerHub, dockerHubCreds) { | |
| dockerImage.push(appVersion) | |
| dockerImage.push("latest") | |
| } | |
| } | |
| stage('Deploy to Vamp') { | |
| // Check if the new version is already running, if so we exit. | |
| def result = sh returnStatus: true, script: "vamp describe deployment ${vampDeploymentName} | grep ${vampServiceName}:${appVersion}" | |
| if (result == 0) { | |
| echo '[FAILURE] service already deployed' | |
| currentBuild.result = 'FAILURE' | |
| sh "exit" | |
| } | |
| sh "vamp generate breed --source simpleservice:1.0.0 --deployable ${dockerRepo}/${vampServiceName}:${appVersion} --target ${vampServiceName}:${appVersion}| vamp create breed --stdin" | |
| sh "vamp generate blueprint --source simpleservice:1.0.0 --cluster simpleservice --breed ${vampServiceName}:${appVersion} --target ${vampServiceName}:${appVersion} | vamp create blueprint --stdin" | |
| sh "vamp merge ${vampServiceName}:${appVersion} ${vampDeploymentName}" | |
| // Check if our new service is running for up to 5 minutes. Waituntil does a gradual back off. | |
| timeout(time: 5, unit: 'MINUTES' ) { | |
| waitUntil { | |
| def returnCode = sh script: "vamp describe deployment ${vampDeploymentName} | grep ${vampServiceName}:${appVersion} | grep Done", returnStatus: true | |
| return (returnCode == 0); | |
| } | |
| } | |
| // shift the weight in 10% increments each 10 seconds | |
| for (i = 1; i <=10; i++) { | |
| def weightA = 100 - i*10 | |
| def weightB = i*10 | |
| sh script: "vamp update-gateway ${vampGatewayName} --weights simpleservice/simpleservice/simpleservice:1.0.0/web@${weightA}%,simpleservice/simpleservice/${vampServiceName}:${appVersion}/web@${weightB}%" | |
| sleep 10 | |
| } | |
| } | |
| stage('Undeploy') { | |
| sh "vamp undeploy simpleservice --service simpleservice:1.0.0" | |
| } | |
| stage('Cleanup') { | |
| sh 'rm node_modules -rf' | |
| } | |
| } | |
| catch (err) { | |
| currentBuild.result = "FAILURE" | |
| throw err | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment