diff --git a/README.md b/README.md index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..4e37f685585c50d5a47acd7bd07f1f2e535f7af0 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,36 @@ +# Jenkins Pipeline Shared Library Sample + +This project provides easy example of shared library which can be used into Jenkins pipeline. + +Setup instructions + +1. In Jenkins, go to Manage Jenkins → Configure System. Under Global Pipeline Libraries, add a library with the following settings: + * Library name: jenkins-pipeline-shared-lib-sample + * Default version: Specify a Git reference (branch or commit SHA), e.g. master + * Retrieval method: Modern SCM + * Select the Git type + * Project repository: https://gitlab.mynisum.com/mtehami/jenkins-shared-library-example.git + * Credentials: (leave blank) + * You may have to turn off ssl verification using: git.exe config --global http.sslVerify false + +2. Then create a Jenkins job with the following pipeline (note that the underscore _ is not a typo): + +``` +@Library('jenkins-pipeline-shared-lib-sample')_ + +stage('Print Build Info') { + printBuildinfo { + name = "Sample Name" + } +} stage('Disable balancer') { + disableBalancerUtils() +} stage('Deploy') { + deploy() +} stage('Enable balancer') { + enableBalancerUtils() +} stage('Check Status') { + checkStatus() +} +``` + +Run job! diff --git a/src/Deployer.groovy b/src/Deployer.groovy new file mode 100644 index 0000000000000000000000000000000000000000..fdbc91f024525cf03b9a8b2694333cb4372b77bf --- /dev/null +++ b/src/Deployer.groovy @@ -0,0 +1,15 @@ +@Grab(group = 'org.apache.commons', module = 'commons-lang3', version = '3.6') +import org.apache.commons.lang3.StringUtils + +class Deployer { + int tries = 0 + Script script + + def run() { + while (tries < 10) { + Thread.sleep(1000) + tries++ + script.echo("tries is numeric: " + StringUtils.isAlphanumeric("" + tries)) + } + } +} diff --git a/src/Sample.groovy b/src/Sample.groovy new file mode 100644 index 0000000000000000000000000000000000000000..abc91b3fe6ebbf5a02708ecd00326abd85c072ff --- /dev/null +++ b/src/Sample.groovy @@ -0,0 +1,17 @@ +public class Sample { + private int x; + private int y; + + public Sample(int x, int y) { + this.x = x; + this.y = y; + } + + @Override + public String toString() { + return "Sample{" + + "x=" + x + + ", y=" + y + + '}'; + } +} diff --git a/src/com/sharedlibrary/Point.groovy b/src/com/sharedlibrary/Point.groovy deleted file mode 100644 index b0c9a7e6201096c99269119ef758ad6a5baca61f..0000000000000000000000000000000000000000 --- a/src/com/sharedlibrary/Point.groovy +++ /dev/null @@ -1,7 +0,0 @@ -// src/com/sharedlibrary/Point.groovy -package com.sharedlibrary - -// point in 3D space -class Point { - float x,y,z -} diff --git a/src/com/sharedlibrary/Zot.groovy b/src/com/sharedlibrary/Zot.groovy deleted file mode 100644 index 0d2f0fef85947fff7e95b686164659189ee61b7b..0000000000000000000000000000000000000000 --- a/src/com/sharedlibrary/Zot.groovy +++ /dev/null @@ -1,14 +0,0 @@ -// src/com/sharedlibrary/Zot.groovy -package org.foo - -// def checkOutFrom(repo) { -// git url: repo -// } - -def log(msg) { - echo msg -} - -return this - - diff --git a/vars/checkStatus.groovy b/vars/checkStatus.groovy new file mode 100644 index 0000000000000000000000000000000000000000..f007140f4b1bd40afcab0d68880b5d92ba880e56 --- /dev/null +++ b/vars/checkStatus.groovy @@ -0,0 +1,12 @@ +#!/usr/bin/env groovy + +def call(body) { + echo "Check status" + + (1..3).each { + echo "Number: " + it + } + + currentBuild.result = 'SUCCESS' //FAILURE to fail + return this +} diff --git a/vars/deploy.groovy b/vars/deploy.groovy new file mode 100644 index 0000000000000000000000000000000000000000..60ee54e9a0ce2e34c3fce2344f0d806afe9c3565 --- /dev/null +++ b/vars/deploy.groovy @@ -0,0 +1,12 @@ +#!/usr/bin/env groovy + +def call(body) { + echo "Start Deploy" + + new Deployer(script:this).run() + + echo "Deployed" + currentBuild.result = 'SUCCESS' //FAILURE to fail + + return this +} diff --git a/vars/disableBalancerUtils.groovy b/vars/disableBalancerUtils.groovy new file mode 100644 index 0000000000000000000000000000000000000000..23b65a7f2073a43de1b796270471f12409299a8d --- /dev/null +++ b/vars/disableBalancerUtils.groovy @@ -0,0 +1,9 @@ +#!/usr/bin/env groovy + +def call(body) { + echo "Disable balancer" + + script:this.echo(new Sample(1, 3).toString()) + + return this +} diff --git a/vars/enableBalancerUtils.groovy b/vars/enableBalancerUtils.groovy new file mode 100644 index 0000000000000000000000000000000000000000..f37ebd7d39765a3b094dd1b2fa77ec81dcf949d5 --- /dev/null +++ b/vars/enableBalancerUtils.groovy @@ -0,0 +1,6 @@ +#!/usr/bin/env groovy + +def call(body) { + echo "Enable balancer" + return this +} diff --git a/vars/gitCheckout.groovy b/vars/gitCheckout.groovy new file mode 100644 index 0000000000000000000000000000000000000000..ec1b5066c9e3f6e54f52b5a16f9ee41b1c884b41 --- /dev/null +++ b/vars/gitCheckout.groovy @@ -0,0 +1,8 @@ +def call(Map stageParams) { + + checkout([ + $class: 'GitSCM', + branches: [[name: stageParams.branch ]], + userRemoteConfigs: [[ url: stageParams.url ]] + ]) + } diff --git a/vars/printBuildinfo.groovy b/vars/printBuildinfo.groovy new file mode 100644 index 0000000000000000000000000000000000000000..4a321abca01ae8699b2b008c06c069422452650c --- /dev/null +++ b/vars/printBuildinfo.groovy @@ -0,0 +1,16 @@ +#!/usr/bin/env groovy + +def call(body) { + def config = [:] + body.resolveStrategy = Closure.DELEGATE_FIRST + body.delegate = config + body() + + echo config.name + echo "Param1 is: ${env.param1}" + echo "Param2 is: ${env.param2}" + if (env.param1 == 'One default') { + echo "Param1 is default" + } + return this +}