├── README.md ├── nodejs-test.jenkinsfile ├── trigger-job-and-show-build-number.jenkinsfile ├── get-last-build-number.jenkinsfile ├── email-test.jenkinsfile ├── slack-test.jenkinsfile ├── get-last-successful-build-number.jenkinsfile ├── test-specific-build-was-successful.jenkinsfile ├── post-build-tests.jenkinsfile ├── test-last-build-was-successful.jenkinsfile ├── slack-test-with-user.jenkinsfile ├── nodejs-run-tests-sample.jenkinsfile ├── do-action-by-build-time.jenkinsfile ├── split-string.jenkinsfile ├── docker-build-deploy-sample.jenkinsfile ├── email-sample.jenkinsfile ├── nodejs-test-docker-build-deploy-sample.jenkinsfile ├── email-sample-with-user.jenkinsfile ├── slack-sample.jenkinsfile └── wait-user-respond-and-continue-if-timeout.jenkinsfile /README.md: -------------------------------------------------------------------------------- 1 | # jenkins-scripts 2 | This repository is a knowledge repository to jenkinsfiles 3 | 4 | I have started a jenkins tutorial series in medium and i'll post the jenkinsfiles here. 5 | [My Medium](https://medium.com/@gustavo.guss) 6 | -------------------------------------------------------------------------------- /nodejs-test.jenkinsfile: -------------------------------------------------------------------------------- 1 | /* 2 | * To Run this job you need: 3 | * Nodejs plugin installed 4 | * Nodejs configure as "node" 5 | */ 6 | 7 | pipeline { 8 | agent any 9 | 10 | tools {nodejs “node”} 11 | 12 | stages { 13 | stage(‘Example’) { 14 | steps { 15 | sh ‘npm config ls’ 16 | } 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /trigger-job-and-show-build-number.jenkinsfile: -------------------------------------------------------------------------------- 1 | pipeline { 2 | agent any 3 | 4 | stages { 5 | stage('Build Other Job') { 6 | steps { 7 | script { 8 | JOB = build job: 'test-get-last-build-number', propagate: false 9 | echo '' + JOB.getNumber() 10 | } 11 | } 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /get-last-build-number.jenkinsfile: -------------------------------------------------------------------------------- 1 | import jenkins.model.Jenkins 2 | 3 | def getLastBuildNumber(jobName) { 4 | def item = Jenkins.instance.getItemByFullName(jobName) 5 | def build = item.getLastBuild() 6 | return '' + build.getNumber() 7 | } 8 | 9 | pipeline { 10 | agent any 11 | 12 | stages { 13 | 14 | stage('Get Last Build Number') { 15 | steps { 16 | echo getLastBuildNumber("job-name") 17 | } 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /email-test.jenkinsfile: -------------------------------------------------------------------------------- 1 | /* 2 | * To Run this job you need: 3 | * Email extended plugin installed 4 | * Email ext Configured 5 | */ 6 | pipeline { 7 | agent any 8 | 9 | stages { 10 | stage('Ok') { 11 | steps { 12 | echo "Ok" 13 | } 14 | } 15 | } 16 | post { 17 | always { 18 | emailext body: 'A Test EMail', recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']], subject: 'Test', to: 'abc' 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /slack-test.jenkinsfile: -------------------------------------------------------------------------------- 1 | /* 2 | * To Run this job you need: 3 | * Slack plugin installed 4 | * Integration Jenkins-Slack configured 5 | */ 6 | pipeline { 7 | agent any 8 | 9 | stages { 10 | stage('Slack Message') { 11 | steps { 12 | slackSend channel: '#jenkins', 13 | color: 'good', 14 | message: "*${currentBuild.currentResult}:* Job ${env.JOB_NAME} build ${env.BUILD_NUMBER}\n More info at: ${env.BUILD_URL}" 15 | 16 | } 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /get-last-successful-build-number.jenkinsfile: -------------------------------------------------------------------------------- 1 | import jenkins.model.Jenkins 2 | 3 | def getLastSuccessfulBuildNumber(jobName) { 4 | def item = Jenkins.instance.getItemByFullName(jobName) 5 | def lastSuccessfulBuild = item.getLastSuccessfulBuild() 6 | return lastSuccessfulBuild.number 7 | } 8 | 9 | pipeline { 10 | agent any 11 | 12 | stages { 13 | 14 | stage('Get Last Successful Build Number') { 15 | environment { 16 | build_number = "" 17 | } 18 | steps { 19 | echo "" + getLastSuccessfulBuildNumber("job-name") 20 | } 21 | } 22 | 23 | } 24 | } -------------------------------------------------------------------------------- /test-specific-build-was-successful.jenkinsfile: -------------------------------------------------------------------------------- 1 | import jenkins.model.Jenkins 2 | 3 | def getBuildResult(jobName, buildNumber) { 4 | echo jobName 5 | def item = Jenkins.instance.getItemByFullName(jobName) 6 | def lastSuccessfulBuild = item.getBuild(buildNumber) 7 | return lastSuccessfulBuild.getResult() 8 | } 9 | 10 | pipeline { 11 | agent any 12 | 13 | stages { 14 | 15 | stage('Verify Build') { 16 | steps { 17 | script { 18 | if( getBuildResult("job-name", "666") != Result.SUCCESS ) { 19 | input message: 'Build Failed. Continue?' 20 | } 21 | } 22 | } 23 | } 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /post-build-tests.jenkinsfile: -------------------------------------------------------------------------------- 1 | pipeline { 2 | environment { 3 | //This variable need be tested as string 4 | doError = '1' 5 | } 6 | 7 | agent any 8 | 9 | stages { 10 | 11 | stage('Error') { 12 | when { 13 | expression { doError == '1' } 14 | } 15 | steps { 16 | echo "Failure" 17 | error "failure test. It's work" 18 | } 19 | } 20 | 21 | stage('Success') { 22 | when { 23 | expression { doError == '0' } 24 | } 25 | steps { 26 | echo "ok" 27 | } 28 | } 29 | } 30 | post { 31 | always { 32 | echo 'I will always execute this!' 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /test-last-build-was-successful.jenkinsfile: -------------------------------------------------------------------------------- 1 | import jenkins.model.Jenkins 2 | 3 | def getLastBuildResult(jobName) { 4 | echo jobName 5 | def item = Jenkins.instance.getItemByFullName(jobName) 6 | def lastSuccessfulBuild = item.getLastBuild() 7 | return lastSuccessfulBuild.getResult() 8 | } 9 | 10 | pipeline { 11 | agent any 12 | 13 | stages { 14 | 15 | stage('Test Latest Build') { 16 | steps { 17 | script { 18 | if( getLastBuildResult("job-name") != Result.SUCCESS ) { 19 | echo "BUILD FAILED" 20 | input message: 'Build Failed. You Want Continue?' 21 | } 22 | else { 23 | echo "BUILD OK" 24 | } 25 | } 26 | } 27 | } 28 | 29 | } 30 | } -------------------------------------------------------------------------------- /slack-test-with-user.jenkinsfile: -------------------------------------------------------------------------------- 1 | /* 2 | * To Run this job you need: 3 | * Slack plugin installed 4 | * Integration Jenkins-Slack configured 5 | * Remove "Use Groovy Sandbox" checkbox in job 6 | */ 7 | def getBuildUser() { 8 | return currentBuild.rawBuild.getCause(Cause.UserIdCause).getUserId() 9 | } 10 | 11 | pipeline { 12 | environment { 13 | BUILD_USER = '' 14 | } 15 | agent any 16 | 17 | stages { 18 | stage('Slack Message') { 19 | steps { 20 | script { 21 | BUILD_USER = getBuildUser() 22 | } 23 | slackSend channel: '#jenkins', 24 | color: 'good', 25 | message: "*${currentBuild.currentResult}:* Job ${env.JOB_NAME} build ${env.BUILD_NUMBER} by ${BUILD_USER}\n More info at: ${env.BUILD_URL}" 26 | 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /nodejs-run-tests-sample.jenkinsfile: -------------------------------------------------------------------------------- 1 | /* 2 | * To Run this job you need: 3 | * Nodejs plugin installed 4 | * Nodejs configure as "node" 5 | */ 6 | 7 | pipeline { 8 | environment { 9 | registry = "gustavoapolinario/docker-test" 10 | registryCredential = 'dockerhub' 11 | dockerImage = '' 12 | } 13 | 14 | agent any 15 | 16 | tools {nodejs "node" } 17 | 18 | stages { 19 | 20 | stage('Cloning Git') { 21 | steps { 22 | git 'https://github.com/gustavoapolinario/microservices-node-example-todo-frontend.git' 23 | 24 | } 25 | } 26 | 27 | stage('Build') { 28 | steps { 29 | sh 'npm install' 30 | sh 'npm run bowerInstall' 31 | } 32 | } 33 | 34 | stage('Test') { 35 | steps { 36 | sh 'npm test' 37 | } 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /do-action-by-build-time.jenkinsfile: -------------------------------------------------------------------------------- 1 | pipeline { 2 | 3 | agent any 4 | 5 | stages { 6 | stage('Wait time') { 7 | steps { 8 | sleep 60 9 | } 10 | } 11 | } 12 | post { 13 | always { 14 | echo "Build Duration: ${currentBuild.duration} ms" 15 | 16 | script { 17 | //Compare in Seconds 18 | def alertTimeInSeconds = 60 19 | if( currentBuild.duration > alertTimeInSeconds * 1000 ) { 20 | echo "Do some things here, like send mail ou slack notification" 21 | } 22 | 23 | //Same if, but in minutes 24 | def alertTimeInMinutes = 1 25 | if( currentBuild.duration > alertTimeInMinutes * 60000 ) { 26 | echo "Do some things here, like send mail ou slack notification" 27 | } 28 | } 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /split-string.jenkinsfile: -------------------------------------------------------------------------------- 1 | pipeline { 2 | agent any 3 | 4 | parameters { 5 | string(name: 'CODES', defaultValue: '', description: 'Codes separated by comma? ex: (1,2,3)', trim: true) 6 | } 7 | 8 | stages { 9 | 10 | stage('Verify Params not empty') { 11 | when { 12 | anyOf { 13 | expression { params.CODES == null } 14 | expression { params.CODES == "" } 15 | } 16 | } 17 | steps { 18 | error("**********************************\nParameter is needed\n**********************************") 19 | } 20 | } 21 | 22 | stage('Split String') { 23 | steps { 24 | script { 25 | for (VALUE in params.CODES.split(',')) { 26 | echo VALUE 27 | } 28 | } 29 | } 30 | } 31 | 32 | } 33 | } -------------------------------------------------------------------------------- /docker-build-deploy-sample.jenkinsfile: -------------------------------------------------------------------------------- 1 | /* 2 | * To Run this job you need: 3 | * Docker installed 4 | */ 5 | 6 | pipeline { 7 | environment { 8 | registry = "gustavoapolinario/docker-test" 9 | registryCredential = 'dockerhub' 10 | dockerImage = '' 11 | } 12 | 13 | agent any 14 | 15 | stages { 16 | 17 | stage('Cloning Git') { 18 | steps { 19 | git 'https://github.com/gustavoapolinario/microservices-node-example-todo-frontend.git' 20 | 21 | } 22 | } 23 | 24 | stage('Building image') { 25 | steps{ 26 | script { 27 | dockerImage = docker.build registry + ":$BUILD_NUMBER" 28 | } 29 | } 30 | } 31 | stage('Deploy Image') { 32 | steps{ 33 | script { 34 | docker.withRegistry( '', registryCredential ) { 35 | dockerImage.push() 36 | } 37 | } 38 | } 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /email-sample.jenkinsfile: -------------------------------------------------------------------------------- 1 | /* 2 | * To Run this job you need: 3 | * Email extended plugin installed 4 | * Email ext Configured 5 | */ 6 | pipeline { 7 | environment { 8 | //This variable need be tested as string 9 | doError = '1' 10 | } 11 | 12 | agent any 13 | 14 | stages { 15 | stage('Error') { 16 | when { 17 | expression { doError == '1' } 18 | } 19 | steps { 20 | echo "Failure" 21 | error "failure test. It's work" 22 | } 23 | } 24 | 25 | stage('Success') { 26 | when { 27 | expression { doError == '0' } 28 | } 29 | steps { 30 | echo "ok" 31 | } 32 | } 33 | } 34 | post { 35 | always { 36 | echo 'I will always say Hello again!' 37 | 38 | emailext body: "${currentBuild.currentResult}: Job ${env.JOB_NAME} build ${env.BUILD_NUMBER}\n More info at: ${env.BUILD_URL}", 39 | recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']], 40 | subject: "Jenkins Build ${currentBuild.currentResult}: Job ${env.JOB_NAME}", 41 | to: '' 42 | 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /nodejs-test-docker-build-deploy-sample.jenkinsfile: -------------------------------------------------------------------------------- 1 | /* 2 | * To Run this job you need: 3 | * Nodejs plugin installed 4 | * Nodejs configure as "node" 5 | * Docker installed 6 | */ 7 | 8 | pipeline { 9 | environment { 10 | registry = "gustavoapolinario/docker-test" 11 | registryCredential = 'dockerhub' 12 | dockerImage = '' 13 | } 14 | 15 | agent any 16 | 17 | tools {nodejs "node" } 18 | 19 | stages { 20 | 21 | stage('Cloning Git') { 22 | steps { 23 | git 'https://github.com/gustavoapolinario/microservices-node-example-todo-frontend.git' 24 | 25 | } 26 | } 27 | 28 | stage('Build') { 29 | steps { 30 | sh 'npm install' 31 | sh 'npm run bowerInstall' 32 | } 33 | } 34 | 35 | stage('Test') { 36 | steps { 37 | sh 'npm test' 38 | } 39 | } 40 | 41 | stage('Building image') { 42 | steps{ 43 | script { 44 | dockerImage = docker.build registry + ":$BUILD_NUMBER" 45 | } 46 | } 47 | } 48 | 49 | stage('Deploy Image') { 50 | steps{ 51 | script { 52 | docker.withRegistry( '', registryCredential ) { 53 | dockerImage.push() 54 | } 55 | } 56 | } 57 | } 58 | } 59 | } -------------------------------------------------------------------------------- /email-sample-with-user.jenkinsfile: -------------------------------------------------------------------------------- 1 | /* 2 | * To Run this job you need: 3 | * Email extended plugin installed 4 | * Email ext Configured 5 | */ 6 | 7 | def getBuildUser() { 8 | return currentBuild.rawBuild.getCause(Cause.UserIdCause).getUserId() 9 | } 10 | 11 | pipeline { 12 | environment { 13 | //This variable need be tested as string 14 | doError = '1' 15 | BUILD_USER = '' 16 | } 17 | 18 | agent any 19 | 20 | stages { 21 | stage('Error') { 22 | when { 23 | expression { doError == '1' } 24 | } 25 | steps { 26 | echo "Failure" 27 | error "failure test. It's work" 28 | } 29 | } 30 | 31 | stage('Success') { 32 | when { 33 | expression { doError == '0' } 34 | } 35 | steps { 36 | echo "ok" 37 | } 38 | } 39 | } 40 | post { 41 | always { 42 | script { 43 | BUILD_USER = getBuildUser() 44 | } 45 | echo 'I will always say Hello again!' 46 | 47 | emailext body: "${currentBuild.currentResult}: Job ${env.JOB_NAME} build ${env.BUILD_NUMBER} by ${BUILD_USER}\n More info at: ${env.BUILD_URL}", 48 | recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']], 49 | subject: "Jenkins Build ${currentBuild.currentResult}: Job ${env.JOB_NAME}", 50 | to: '' 51 | 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /slack-sample.jenkinsfile: -------------------------------------------------------------------------------- 1 | /* 2 | * To Run this job you need: 3 | * Slack plugin installed 4 | * Integration Jenkins-Slack configured 5 | * Remove "Use Groovy Sandbox" checkbox in job 6 | */ 7 | 8 | def COLOR_MAP = ['SUCCESS': 'good', 'FAILURE': 'danger', 'UNSTABLE': 'danger', 'ABORTED': 'danger'] 9 | 10 | def getBuildUser() { 11 | return currentBuild.rawBuild.getCause(Cause.UserIdCause).getUserId() 12 | } 13 | 14 | pipeline { 15 | environment { 16 | //This variable need be tested as string 17 | doError = '1' 18 | BUILD_USER = '' 19 | } 20 | 21 | agent any 22 | 23 | stages { 24 | stage('Error') { 25 | when { 26 | expression { doError == '1' } 27 | } 28 | steps { 29 | echo "Failure" 30 | error "failure test. It's work" 31 | } 32 | } 33 | 34 | stage('Success') { 35 | when { 36 | expression { doError == '0' } 37 | } 38 | steps { 39 | echo "ok" 40 | } 41 | } 42 | } 43 | post { 44 | always { 45 | script { 46 | BUILD_USER = getBuildUser() 47 | } 48 | echo 'I will always say Hello again!' 49 | 50 | slackSend channel: '#jenkins', 51 | color: COLOR_MAP[currentBuild.currentResult], 52 | message: "*${currentBuild.currentResult}:* Job ${env.JOB_NAME} build ${env.BUILD_NUMBER} by ${BUILD_USER}\n More info at: ${env.BUILD_URL}" 53 | 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /wait-user-respond-and-continue-if-timeout.jenkinsfile: -------------------------------------------------------------------------------- 1 | pipeline { 2 | agent any 3 | 4 | environment { 5 | returned = false 6 | } 7 | 8 | stages { 9 | stage('Wait time') { 10 | steps { 11 | script { 12 | try { 13 | timeout(time: 10, unit: 'SECONDS') { 14 | 15 | returned = input(message: 'Continue?', ok: 'Continue', 16 | parameters: [ 17 | booleanParam(defaultValue: true, 18 | description: 'Continue the steps', 19 | name: 'Yes?') 20 | ]) 21 | 22 | } 23 | } 24 | catch(err) { 25 | def user = err.getCauses()[0].getUser() 26 | 27 | if (user.toString() == 'SYSTEM') { 28 | //if timeout, continue or fail job? 29 | echo "Timeout. Continuing" 30 | returned = true 31 | } 32 | else { 33 | echo "Button Abort clicked" 34 | returned = false 35 | } 36 | } 37 | echo "Value: " + returned 38 | } 39 | } 40 | } 41 | stage('Next step') { 42 | when { 43 | anyOf { 44 | expression { returned == true } 45 | } 46 | } 47 | steps { 48 | echo "time lapsed or button Yes is clicked" 49 | } 50 | } 51 | } 52 | } 53 | --------------------------------------------------------------------------------