// Tools String SonarQube_server = 'SonarQube_M1' String SonarQube_Scanner_version = 'SonarQube_Scanner_3' // Functions def btp_lib //def get_nodejs_versions = '''def nodejs_config = new XmlSlurper().parse(new File("/var/lib/jenkins/jenkins.plugins.nodejs.tools.NodeJSInstallation.xml")) def nodejs_versions = [] nodejs_config.installations.getAt('jenkins.plugins.nodejs.tools.NodeJSInstallation').each { println('Found ' + it.name) nodejs_versions.push(it.name) } '''return nodejs_versions''' def get_nodejs_versions_fallback = '''return ['NO NODE INSTALLATION FOUND']''' properties([ parameters([choice(choices: ['', 'agl-custom'], description: 'Select Application type', name: 'APPLICATION_TYPE'), [$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: '', filterLength: 1, filterable: true, name: 'AGL_CONFIG_SERVER_VERSION', randomName: 'choice-parameter-10463498146662', referencedParameters: 'APPLICATION_TYPE', script: [$class: 'GroovyScript', fallbackScript: [classpath: [], sandbox: false, script: ''], script: [classpath: [], sandbox: false, script: ''' if (APPLICATION_TYPE.equals("agl-config-server")){ def gitURL = "ssh://[email protected]:8080/agl/004-agl-config-server.git" proc1 = [\'/bin/bash\', \'-c\', "rm -rf /product/jenkins/agl-config-server"].execute() proc2 = [\'/bin/bash\', \'-c\', "mkdir -p /product/jenkins/agl-config-server"].execute() proc3 = [\'/bin/bash\', \'-c\', "/usr/bin/git ls-remote --heads --tags ${gitURL}"].execute() proc4 = [\'/bin/bash\', \'-c\', "awk \'{print \\$2}\'"].execute() proc5 = [\'/bin/bash\', \'-c\', "sed s%refs/heads/%origin/%"].execute() proc6 = [\'/bin/bash\', \'-c\', "sed s%refs/tags/%%"].execute() all = proc1 | proc2 | proc3 | proc4 | proc5 | proc6 String result = all.text String filename = "/product/jenkins/agl-config-server/branches.txt" boolean success = new File(filename).write(result) def multiline = "cat product/jenkins/agl-config-server/branches.txt".execute().text def list = multiline.readLines() }''']]], [$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: '', filterLength: 1, filterable: true, name: 'AGL_CORE_VERSION', randomName: 'choice-parameter-10463503434283', referencedParameters: 'APPLICATION_TYPE', script: [$class: 'GroovyScript', fallbackScript: [classpath: [], sandbox: false, script: ''], script: [classpath: [], sandbox: false, script: '''if ((APPLICATION_TYPE != null) && (APPLICATION_TYPE != "")){ if (!APPLICATION_TYPE.equals("agl-config-server")){ def gitURL = "ssh://[email protected]:telusvideoservices/agl-core.git" proc1 = [\'/bin/bash\', \'-c\', "rm -rf /product/jenkins/agl-core"].execute() proc2 = [\'/bin/bash\', \'-c\', "mkdir -p /product/jenkins/agl-core"].execute() proc3 = [\'/bin/bash\', \'-c\', "/usr/bin/git ls-remote --heads --tags ${gitURL}"].execute() proc4 = [\'/bin/bash\', \'-c\', "awk \'{print \\$2}\'"].execute() proc5 = [\'/bin/bash\', \'-c\', "sed s%refs/heads/%origin/%"].execute() proc6 = [\'/bin/bash\', \'-c\', "sed s%refs/tags/%%"].execute() all = proc1 | proc2 | proc3 | proc4 | proc5 | proc6 String result = all.text String filename = "/product/jenkins/agl-core/branches.txt" boolean success = new File(filename).write(result) def multiline = "cat product/jenkins/agl-core/branches.txt".execute().text def list = multiline.readLines() } }''']]], [$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: '', filterLength: 1, filterable: true, name: 'AGL_MIDDLEWARE_VERSION', randomName: 'choice-parameter-10463508865844', referencedParameters: 'APPLICATION_TYPE', script: [$class: 'GroovyScript', fallbackScript: [classpath: [], sandbox: false, script: ''], script: [classpath: [], sandbox: false, script: '''def gitURL = "ssh://[email protected]:telusvideoservices/${APPLICATION_TYPE}-middleware.git" proc1 = [\'/bin/bash\', \'-c\', "rm -rf /product/jenkins/generalagljob/${APPLICATION_TYPE}"].execute() proc2 = [\'/bin/bash\', \'-c\', "mkdir -p /product/jenkins/generalagljob/${APPLICATION_TYPE}"].execute() proc3 = [\'/bin/bash\', \'-c\', "/usr/bin/git ls-remote --heads --tags ${gitURL}"].execute() proc4 = [\'/bin/bash\', \'-c\', "awk \'{print \\$2}\'"].execute() proc5 = [\'/bin/bash\', \'-c\', "sed s%refs/heads/%origin/%"].execute() proc6 = [\'/bin/bash\', \'-c\', "sed s%refs/tags/%%"].execute() all = proc1 | proc2 | proc3 | proc4 | proc5 | proc6 String result = all.text String filename = "/product/jenkins/generalagljob/${APPLICATION_TYPE}/branches.txt" boolean success = new File(filename).write(result) def multiline = "cat product/jenkins/generalagljob/${APPLICATION_TYPE}/branches.txt".execute().text def list = multiline.readLines()''']]], [$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: '', filterLength: 1, filterable: true, name: 'GIT_BRANCH_DEVOPS', randomName: 'choice-parameter-10463512371071', referencedParameters: 'APPLICATION_TYPE', script: [$class: 'GroovyScript', fallbackScript: [classpath: [], sandbox: false, script: ''], script: [classpath: [], sandbox: false, script: '''def gitURL="ssh://[email protected]:telusvideoservices/${APPLICATION_TYPE}.git" proc1 = [\'/bin/bash\', \'-c\', "rm -rf /product/jenkins/generalagljobdevops/${APPLICATION_TYPE}"].execute() proc2 = [\'/bin/bash\', \'-c\', "mkdir -p /product/jenkins/generalagljobdevops/${APPLICATION_TYPE}"].execute() proc3 = [\'/bin/bash\', \'-c\', "/usr/bin/git ls-remote --heads --tags ${gitURL}"].execute() proc4 = [\'/bin/bash\', \'-c\', "awk \'{print \\$2}\'"].execute() proc5 = [\'/bin/bash\', \'-c\', "sed s%refs/heads/%%"].execute() all = proc1 | proc2 | proc3 | proc4 | proc5 String result = all.text String filename = "/product/jenkins/generalagljobdevops/${APPLICATION_TYPE}/branches.txt" boolean success = new File(filename).write(result) def multiline = "cat product/jenkins/generalagljobdevops/${APPLICATION_TYPE}/branches.txt".execute().text def list = multiline.readLines()''']]], [$class: 'ChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: '', filterLength: 1, filterable: false, name: 'NODEJS_VERSION', randomName: 'choice-parameter-10463521785114', script: [$class: 'GroovyScript', fallbackScript: [classpath: [], sandbox: false, script: 'return [\'NO NODE INSTALLATION FOUND\']'], script: [classpath: [], sandbox: false, script: '''def nodejs_config = new XmlSlurper().parse(new File("/var/lib/custom/jenkins/jenkins.plugins.nodejs.tools.NodeJSInstallation.xml")) def nodejs_versions = [] nodejs_config.installations.getAt(\'jenkins.plugins.nodejs.tools.NodeJSInstallation\').each { println(\'Found \' + it.name) nodejs_versions.push(it.name) } return nodejs_versions''']]]])]) pipeline { agent any options { buildDiscarder(logRotator(numToKeepStr: '15')) disableConcurrentBuilds() } parameters { gitParameter branch: '', branchFilter: '.*', defaultValue: 'origin/master', description: 'AGL Gulp Git branch or tag', listSize: '0', name: 'AGL_GULP_VERSION', quickFilterEnabled: true, selectedValue: 'DEFAULT', sortMode: 'DESCENDING_SMART', tagFilter: '*', type: 'PT_BRANCH_TAG', useRepository: 'ssh://[email protected]:telusvideoservices/004-agl-gulp.git' booleanParam(name: 'EXECUTE_LINT_TEST', defaultValue: true, description: 'Execute Lint') booleanParam(name: 'EXECUTE_UNIT_TEST', defaultValue: true, description: 'Execute Unit Test & Coverage') booleanParam(name: 'EXECUTE_SECURITY_TEST', defaultValue: true, description: 'Execute Security test (Source Scanner)') booleanParam(name: 'EXECUTE_OWASP_CHECK', defaultValue: true, description: 'Execute OWASP dependency check. Only if APP and CS are true.') booleanParam(name: 'EXECUTE_SONAR_SCANNER', defaultValue: true, description: 'Execute SonarQube Scanner. Only if APP and CS are true.') booleanParam(name: 'UPLOAD_BUILT_PACKAGES', defaultValue: true, description: 'Upload built packages to Nexus repository') booleanParam(name: 'DEPLOY_OCP', defaultValue: true, description: 'Check this parameter if you want to deploy AGL-CORE artifact in Openshift') gitParameter branch: '', branchFilter: 'origin/.*', defaultValue: 'origin/AVS_6.5', description: '', listSize: '1', name: 'TARGET_ENVIRONMENT', quickFilterEnabled: true, selectedValue: 'TOP', sortMode: 'DESCENDING', tagFilter: '*', type: 'BRANCH', useRepository: 'ssh://[email protected]:telusvideoservices/000-environments.git' booleanParam(name: 'TRIGGER_AGL_TAG', defaultValue: false, description: 'Check this parameter if you want to trigger the build to tag on Git the built versions. You can tag only dev or release branches!') string(name: 'AGL_CORE_NEW_VERSION', defaultValue: '', description: 'AGL CORE new version to tag on Git') string(name: 'AGL_MIDDLEWARE_NEW_VERSION', defaultValue: '', description: 'AGL MIDDLEWARE new version to tag on Git') } stages { stage('Initialization') { steps { script { btp_lib = load 'lib/btp.groovy' String buildID = "#${BUILD_NUMBER} - product".toString() btp_lib.init() env.AGL_NAME_UC = 'product'.toUpperCase() currentBuild.displayName = buildID if (params.AGL_CORE_NEW_VERSION == '') { def AGL_CORE_BRANCH_NAME = params.AGL_CORE_VERSION.replace('origin/', '') env.AGL_CORE_BUILD_VERSION = AGL_CORE_BRANCH_NAME.replaceAll('/', '_') } else { env.AGL_CORE_BUILD_VERSION = params.AGL_CORE_NEW_VERSION } if (params.AGL_MIDDLEWARE_NEW_VERSION == '') { def AGL_MIDDLEWARE_BRANCH_NAME = params.AGL_MIDDLEWARE_VERSION.replace('origin/', '') env.AGL_MIDDLEWARE_BUILD_VERSION = AGL_MIDDLEWARE_BRANCH_NAME.replaceAll('/', '_') } else { env.AGL_MIDDLEWARE_BUILD_VERSION = params.AGL_MIDDLEWARE_NEW_VERSION } if (params.AGL_CONFIG_SERVER_NEW_VERSION == '') { def AGL_CONFIG_SERVER_BRANCH_NAME = params.AGL_CONFIG_SERVER_VERSION.replace('origin/', '') env.AGL_CONFIG_SERVER_BUILD_VERSION = AGL_CONFIG_SERVER_BRANCH_NAME.replaceAll('/', '_') } else { env.AGL_CONFIG_SERVER_BUILD_VERSION = params.AGL_CONFIG_SERVER_NEW_VERSION } } } } stage('Prepare') { when { expression { currentBuild.resultIsBetterOrEqualTo('SUCCESS') } } steps { script { btp_lib.prepare() } } } stage('Build') { when { expression { currentBuild.resultIsBetterOrEqualTo('SUCCESS') } } steps { script { btp_lib.build(params.NODEJS_VERSION) } } } stage('Test') { when { expression { currentBuild.resultIsBetterOrEqualTo('SUCCESS') } } steps { script { btp_lib.test(params.NODEJS_VERSION, SonarQube_server, SonarQube_Scanner_version) } } } stage('Trigger AGL Tag') { when { expression { params.TRIGGER_AGL_TAG ==~ /true/ } expression { currentBuild.resultIsBetterOrEqualTo('SUCCESS') } } steps { build job: 'AGL_TAG_ON_GIT_Nano', parameters: [ string(name: 'APPLICATION_TYPE', value: env.APPLICATION_TYPE), string(name: 'AGL_CORE_VERSION', value: params.AGL_CORE_VERSION), string(name: 'AGL_MIDDLEWARE_VERSION', value: params.AGL_MIDDLEWARE_VERSION), string(name: 'AGL_CONFIG_SERVER_VERSION', value: params.AGL_CONFIG_SERVER_VERSION), string(name: 'AGL_CORE_NEW_VERSION', value: params.AGL_CORE_NEW_VERSION), string(name: 'AGL_MIDDLEWARE_NEW_VERSION', value: params.AGL_MIDDLEWARE_NEW_VERSION), string(name: 'AGL_WORKSPACE', value: env.w_BTP) ], wait: true } } stage('Package - Create') { when { expression { params.UPLOAD_BUILT_PACKAGES ==~ /true/ } expression { currentBuild.resultIsBetterOrEqualTo('SUCCESS') } } steps { script { btp_lib.package_create(params.NODEJS_VERSION) } } } stage('Package - Upload') { when { expression { params.UPLOAD_BUILT_PACKAGES ==~ /true/ } expression { currentBuild.resultIsBetterOrEqualTo('SUCCESS') } } steps { script { btp_lib.package_upload() } } } stage('Trigger OCP Deploy') { when { expression { params.DEPLOY_OCP ==~ /true/ } expression { currentBuild.resultIsBetterOrEqualTo('SUCCESS') } } steps { script { env.DEVOPS_PROJECT_REPOS_BASE_URL="ssh://[email protected]:telusvideoservices/" env.gitBranchDevOpsReplaced = params.GIT_BRANCH_DEVOPS.replace('origin/', '') env.REPOSITORY_URL_DEVOPS = "${env.DEVOPS_PROJECT_REPOS_BASE_URL}${params.APPLICATION_TYPE}.git" } build job: 'AGL_OCP_DEPLOY_NANO', parameters: [ string(name: 'APPLICATION_TYPE', value: params.APPLICATION_TYPE), string(name: 'AGL_CORE_VERSION', value: env.AGL_CORE_BUILD_VERSION), string(name: 'AGL_MIDDLEWARE_VERSION', value: env.AGL_MIDDLEWARE_BUILD_VERSION), string(name: 'AGL_CONFIG_SERVER_VERSION', value: env.AGL_CONFIG_SERVER_BUILD_VERSION), string(name: 'REPOSITORY_URL_DEVOPS', value: env.REPOSITORY_URL_DEVOPS), string(name: 'DEFECT_ID_LIST', value: ''), string(name: 'GIT_BRANCH_DEVOPS', value: env.gitBranchDevOpsReplaced), string(name: 'TARGET_ENVIRONMENT', value: params.TARGET_ENVIRONMENT) ], wait: false } } } }
Write, Run & Share Groovy code online using OneCompiler's Groovy online compiler for free. It's one of the robust, feature-rich online compilers for Groovy language, running the latest Groovy version 2.6. Getting started with the OneCompiler's Groovy editor is easy and fast. The editor shows sample boilerplate code when you choose language as Groovy and start coding.
OneCompiler's Groovy online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Groovy program which takes name as input and prints hello message with your name.
def name = System.in.newReader().readLine()
println "Hello " + name
Groovy is an object-oriented programming language based on java. Apache Groovy is a dynamic and agile language which is similar to Python, Ruby, Smalltalk etc.
Data type | Description | Range |
---|---|---|
String | To represent text literals | NA |
char | To represent single character literal | NA |
int | To represent whole numbers | -2,147,483,648 to 2,147,483,647 |
short | To represent short numbers | -32,768 to 32,767 |
long | To represent long numbers | -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 |
double | To represent 64 bit floating point numbers | 4.94065645841246544e-324d to 1.79769313486231570e+308d |
float | To represent 32 bit floating point numbers | 1.40129846432481707e-45 to 3.40282346638528860e+38 |
byte | To represent byte value | -128 to 127 |
boolean | To represent boolean values either true or false | True or False |
You can define variables in two ways
data-type variable-name;
[or]
def variable-name;
0.upto(n) {println "$it"}
or
n.times{println "$it"}
where n is the number of loops and 0 specifies the starting index
When ever you want to perform a set of operations based on a condition or set of conditions, then If / Nested-If / If-Else is used.
if(conditional-expression) {
// code
} else {
// code
}
Switch is an alternative to If-Else-If ladder and to select one among many blocks of code.
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
//code to be executed when all the above cases are not matched;
}
List allows you to store ordered collection of data values.
def mylist = [1,2,3,4,5];
List Methods | Description |
---|---|
size() | To find size of elements |
sort() | To sort the elements |
add() | To append new value at the end |
contains() | Returns true if this List contains requested value. |
get() | Returns the element of the list at the definite position |
pop() | To remove the last item from the List |
isEmpty() | Returns true if List contains no elements |
minus() | This allows you to exclude few specified elements from the elements of the original |
plus() | This allows you to add few specified elements to the elements of the original |
remove() | To remove the element present at the specific position |
reverse() | To reverse the elements of the original List and creates new list |