Commit 7abdd7ba authored by Muhammad Tehami's avatar Muhammad Tehami 💡

Sample library done

parent bf84cf56
# 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!
@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))
}
}
}
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 +
'}';
}
}
// src/com/sharedlibrary/Point.groovy
package com.sharedlibrary
// point in 3D space
class Point {
float x,y,z
}
// src/com/sharedlibrary/Zot.groovy
package org.foo
// def checkOutFrom(repo) {
// git url: repo
// }
def log(msg) {
echo msg
}
return this
#!/usr/bin/env groovy
def call(body) {
echo "Check status"
(1..3).each {
echo "Number: " + it
}
currentBuild.result = 'SUCCESS' //FAILURE to fail
return this
}
#!/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
}
#!/usr/bin/env groovy
def call(body) {
echo "Disable balancer"
script:this.echo(new Sample(1, 3).toString())
return this
}
#!/usr/bin/env groovy
def call(body) {
echo "Enable balancer"
return this
}
def call(Map stageParams) {
checkout([
$class: 'GitSCM',
branches: [[name: stageParams.branch ]],
userRemoteConfigs: [[ url: stageParams.url ]]
])
}
#!/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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment