Define global variable in Jenkins Shared Library - jenkins

I created a Jenkins Shared Library with many functions into /vars. Among them, there is a devopsProperties.groovy with many properties :
class devopsProperties {
//HOSTS & SLAVES
final String delivery_host = "****"
final String yamale_slave = "****"
//GIT
final Map<String,String> git_orga_by_project = [
"project1" : "orga1",
"project2" : "orga2",
"project3" : "orga3"
]
...
}
Other functions in my Shared Library use these parameters. For example, gitGetOrga.groovy :
def call(String project_name) {
devopsProperties.git_orga_by_project.each{
if (project_name.startsWith(it.key)){
orga_found = it.value
}
}
return orga_found
}
But now, as we have many environments, we need to load at the beginning of the pipeline the devopsProperties. I create properties files in the resources :
+-resources
+-properties
+-properties-dev.yaml
+-properties-val.yaml
+-properties-prod.yaml
and create a function to load it :
def call(String environment="PROD") {
// load the specific environment properties file
switch(environment.toUpperCase()) {
case "DEV":
def propsText = libraryResource 'properties/properties-dev.yaml'
devopsProperties = readYaml text:propsText
print "INFO : DEV properties loaded"
break
case "VAL":
def propsText = libraryResource 'properties/properties-val.yaml'
devopsProperties = readYaml text:propsText
print "INFO : VAl properties loaded"
break
case "PROD":
def propsText = libraryResource 'properties/properties-prod.yaml'
devopsProperties = readYaml text:propsText
print "INFO : PROD properties loaded"
break
default:
print "ERROR : environment unkown, choose between DEV, VAL or PROD"
break
}
return devopsProperties
}
but when I try to use it in a pipeline :
#Library('Jenkins-SharedLibraries')_
devopsProperties = initProperties("DEV")
pipeline {
agent none
stages {
stage("SLAVE JENKINS") {
agent {
node {
label ***
}
}
stages{
stage('Test') {
steps {
script {
print devopsProperties.delivery_host // THIS IS OK
print devopsProperties.git_orga_by_project["project1"] // THIS IS OK
print gitGetOrga("project1") //THIS IS NOT OK
}
}
}
}
}
}
}
The last print generates an error : groovy.lang.MissingPropertyException: No such property: devopsProperties for class: gitGetOrga
How I can use a global variable into all my Jenkins Shared Library functions ? If possible, I prefer to not pass it in parameter of all functions.

EDITED
First, you need to place gitGetOrga.groovy in to 'src' directory which is on the same level as var and where you have java package for your code. You'll get the structure like this:
After that, you need to import your class in gitGetOrga.groovy
import com.you-company.project-name.devopsProperties
def call(String project_name) {
devopsProperties.git_orga_by_project.each{
if (project_name.startsWith(it.key)){
orga_found = it.value
}
}
return orga_found
}
You can find more information in the Jenkins docs: https://www.jenkins.io/doc/book/pipeline/shared-libraries/#writing-libraries

Related

How to pass parameters and variables from a file to jenkinsfile?

I'm trying to convert my jenkins pipeline to a shared library since it can be reusable on most of the application. As part of that i have created groovy file in vars folder and kept pipeline in jenkins file in github and able to call that in jenkins successfully
As part of improving this i want to pass params, variables, node labels through a file so that we should not touch jenkins pipeline and if we want to modify any vars, params, we have to do that in git repo itself
pipeline {
agent
{
node
{
label 'jks_deployment'
}
}
environment{
ENV_CONFIG_ID = 'jenkins-prod'
ENV_CONFIG_FILE = 'test.groovy'
ENV_PLAYBOOK_NAME = 'test.tar.gz'
}
parameters {
string (
defaultValue: 'test.x86_64',
description: 'Enter app version',
name: 'app_version'
)
choice (
choices: ['10.0.0.1','10.0.0.2','10.0.0.3'],
description: 'Select a host to be delpoyed',
name: 'host'
)
}
stages {
stage("reading properties from properties file") {
steps {
// Use a script block to do custom scripting
script {
def props = readProperties file: 'extravars.properties'
env.var1 = props.var1
env.var2 = props.var2
}
echo "The variable 1 value is $var1"
echo "The variable 2 value is $var2"
}
In above code,i used pipeline utility steps plugin and able to read variables from extravars.properties file. Is it same way we can do for jenkins parameters also? Or do we have any suitable method to take care of passing this parameters via a file from git repo?
Also is it possible to pass variable for node label also?
=====================================================================
Below are the improvements which i have made in this project
Used node label plugin to pass the node name as variable
Below is my vars/sayHello.groovy file content
def call(body) {
// evaluate the body block, and collect configuration into the object
def pipelineParams= [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = pipelineParams
body()
pipeline {
agent
{
node
{
label "${pipelineParams.slaveName}"
}
}
stages {
stage("reading properties from properties file") {
steps {
// Use a script block to do custom scripting
script {
// def props = readProperties file: 'extravars.properties'
// script {
readProperties(file: 'extravars.properties').each {key, value -> env[key] = value }
//}
// env.var1 = props.var1
// env.var2 = props.var2
}
echo "The variable 1 value is $var1"
echo "The variable 2 value is $var2"
}
}
stage ('stage2') {
steps {
sh "echo ${var1}"
sh "echo ${var2}"
sh "echo ${pipelineParams.appVersion}"
sh "echo ${pipelineParams.hostIp}"
}
}
}
}
}
Below is my vars/params.groovy file
properties( [
parameters([
choice(choices: ['10.80.66.171','10.80.67.6','10.80.67.200'], description: 'Select a host to be delpoyed', name: 'host')
,string(defaultValue: 'fxxxxx.x86_64', description: 'Enter app version', name: 'app_version')
])
] )
Below is my jenkinsfile
def _hostIp = params.host
def _appVersion = params.app_version
sayHello {
slaveName = 'master'
hostIp = _hostIp
appVersion = _appVersion
}
Now Is it till we can improve this?Any suggestions let me know.

How to pass parameters in a stage call in Jenkinsfile

Actually my Jenkinsfile looks like this:
#Library('my-libs') _
myPipeline{
my_build_stage(project: 'projectvalue', tag: '1.0' )
my_deploy_stage()
}
I am trying to pass these two variables (project and tag) to my build_stage.groovy, but it is not working.
What is the correct syntax to be able to use $params.project or $params.tag in my_build_stage.groovy?
Please see the below code which will pass parameters.
In your Jenkinsfile write below code:
// Global variable is used to get data from groovy file(shared library file)
def mylibrary
def PROJECT_VALUE= "projectvalue"
def TAG = 1
pipeline{
agent{}
stages{
stage('Build') {
steps {
script {
// Load Shared library Groovy file mylibs.Give your path of mylibs file which will contain all your function definitions
mylibrary= load 'C:\\Jenkins\\mylibs'
// Call function my_build stage and pass parameters
mylibrary.my_build_stage(PROJECT_VALUE, TAG )
}
}
}
stage('Deploy') {
steps {
script {
// Call function my_deploy_stage
mylibrary.my_deploy_stage()
}
}
}
}
}
Create a file named : mylibs(groovy file)
#!groovy
// Write or add Functions(definations of stages) which will be called from your jenkins file
def my_build_stage(PROJECT_VALUE,TAG_VALUE)
{
echo "${PROJECT_VALUE} : ${TAG_VALUE}"
}
def my_deploy_stage()
{
echo "In deploy stage"
}
return this

append array and ArrayList to ArrayList

Using Jenkins pipeline we have our own build script. Also all of our projects have a rakefile which is what we use to do a lot of the building steps. Our typical jenkins build executes 3 rake tasks but we do have some exceptions and that has to do when we have a angular website we try to build with it.
I've configured my pipeline like this:
buildGitProject {
repository='https://anonymous.visualstudio.com/Project/_git/my-csharp-project-with-angular'
branchName= 'master'
solutionName='MyCSharpSolution.sln'
emailTo='someone#aol.com'
preRakeCommands=['install_npm_dependencies', 'ng_build']
}
that relies on our build script which is this:
def call(body) {
def args= [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = args
body()
def agentName = "windows && ${args.branchName}"
def remoteConfig = org.pg.RemoteConfigFactory.create(args.repository);
pipeline {
agent none
options {
buildDiscarder(logRotator(numToKeepStr: org.pg.Settings.BUILDS_TO_KEEP))
skipStagesAfterUnstable()
timestamps()
}
stages {
stage("checkout") {
agent any
steps {
checkoutFromGit(remoteConfig, args.branchName)
}
}
stage('build') {
agent{node{ label agentName as String}}
steps {
buildSolution(args.solutionName, args.get('preRakeCommands', []), args.get('postRakeCommands', []))
}
}
stage('test') {
agent{node{ label agentName as String}}
steps {
testSolution(args.solutionName)
}
}
}
}
}
which fails in the build stage.
buildSolution.groovy
def call(String solutionName, ArrayList preRakeCommands, ArrayList postRakeCommands) {
unstash 'ws'
String[] rakeCommands = [
"build_solution[${solutionName}, Release, Any CPU]",
"copy_to_deployment_folder",
"execute_dev_dropkick"
]
String[] combinedRakeCommand = (preRakeCommands.plus(rakeCommands).plus(postRakeCommands)) as String[]
executeRake( combinedRakeCommand )
stash name: 'deployment', includes: 'deployment/**/*'
}
executeRake.groovy
def call(String... rakeTasks) {
def safeRakeTasks = rakeTasks.collect{ "\"$it\"" }.join(' ');
bat script: "rake ${safeRakeTasks}"
}
in the jenkins build log it says:
08:43:09 C:\jenkins_repos\Project\my-csharp-project-with-angular>rake "install_npm_dependencies" "ng_build" "[Ljava.lang.String;#11bd466"
I have no idea how or why it is using a string pointer because I thought that plus concated arrays and ArrayList... Plus it is in Jenkins so it is a pain to test.
List a = ['a1','a2','a3']
String [] s = ['s1','s2','s3']
List b = ['b1','b2','b3']
println a.plus(s as List).plus(b)
output:
[a1, a2, a3, s1, s2, s3, b1, b2, b3]
Another approach:
List a = ['a1','a2','a3']
String[] s = ['s1','s2','s3']
List b = ['b1','b2','b3']
println ([*a,*s,*b])
alternatively
println a + [*s] + b
which should perform better

Accessing Groovy class variables via Jenkins Declarative Pipeline

I am trying to create a pipeline for my project using Jenkins Declarative Pipleline. As a part of it i had to write some Groovy code in some file and i was successful in loading it. Below is the a code snippet from JenkinsFile
stage('Pre-Build'){
steps{
script{
pipeline = load 'dependencyChecker.groovy'
dependents = pipeline.gatherAllDependencies("${base_workspace}/${branch}/${project}/settings.gradle")
for (item in dependents){
sh "cp --parents ${item} ."
}
}
}
}
The following is the groovy file(dependencyChecker.groovy)
class Test1{
static def outStream;
static setOut(PrintStream out){
outStream = out;
out.println(outStream);
}
static List gatherAllDependencies(String settingsPath){
List projectDependencies = [];
computeAllDependencies(settingsPath,projectDependencies);
return projectDependencies;
}
static List computeAllDependencies(String settingsPath,List projectDependencies){
new File(settingsPath).splitEachLine('='){line ->
if(line[1]!=null){
outStream.println("Entire Project Name is ${line[1]}");
def name = line[1].split('\'');
if(name.length==3){
def dir = name[1];
Path path = Paths.get(settingsPath);
def dependency = path.getParent().resolve(dir).normalize();
outStream.println(dependency);
if(!projectDependencies.contains("${dependency}")){
projectDependencies.add("${dependency}")
if(new File("${dependency}/settings.gradle").exists()){
computeAllDependencies("${dependency}/settings.gradle",projectDependencies);
}
}
}
}
}
}
}
Test1.setOut(System.out);
return Test1;
Updated:
import java.nio.file.*
class Test1 implements Serializable{
def script;
Test1(def script){
this.script = script;
}
def gatherAllDependencies(String settingsPath){
List projectDependencies = [];
computeAllDependencies(settingsPath,projectDependencies);
return projectDependencies;
}
def computeAllDependencies(String settingsPath,List projectDependencies){
new File(settingsPath).splitEachLine('='){line ->
if(line[1]!=null){
this.script.echo("Entire Project Name is ${line[1]}");
def name = line[1].split('\'');
if(name.length==3){
def dir = name[1];
Path path = Paths.get(settingsPath);
def dependency = path.getParent().resolve(dir).normalize();
this.script.echo(dependency);
if(!projectDependencies.contains("${dependency}")){
projectDependencies.add("${dependency}")
if(new File("${dependency}/settings.gradle").exists()){
computeAllDependencies("${dependency}/settings.gradle",projectDependencies);
}
}
}
}
}
}
}
def a = new Test1(script:this);
return a;
dependents(in JenkinsFile) is always empty despite the same code working fine in Jenkins Script Console. I could not debug it as none of my println statements are logged anywhere
Can someone guide me as to where i am going wrong

Using FilePath to access workspace on slave in Jenkins pipeline

I need to check for the existence of a certain .exe file in my workspace as part of my pipeline build job. I tried to use the below Groovy script from my Jenkinsfile to do the same. But I think the File class by default tries to look for the workspace directory on jenkins master and fails.
#com.cloudbees.groovy.cps.NonCPS
def checkJacoco(isJacocoEnabled) {
new File(pwd()).eachFileRecurse(FILES) { it ->
if (it.name == 'jacoco.exec' || it.name == 'Jacoco.exec')
isJacocoEnabled = true
}
}
How to access the file system on slave using Groovy from inside the Jenkinsfile?
I also tried the below code. But I am getting No such property: build for class: groovy.lang.Binding error. I also tried to use the manager object instead. But get the same error.
#com.cloudbees.groovy.cps.NonCPS
def checkJacoco(isJacocoEnabled) {
channel = build.workspace.channel
rootDirRemote = new FilePath(channel, pwd())
println "rootDirRemote::$rootDirRemote"
rootDirRemote.eachFileRecurse(FILES) { it ->
if (it.name == 'jacoco.exec' || it.name == 'Jacoco.exec') {
println "Jacoco Exists:: ${it.path}"
isJacocoEnabled = true
}
}
Had the same problem, found this solution:
import hudson.FilePath;
import jenkins.model.Jenkins;
node("aSlave") {
writeFile file: 'a.txt', text: 'Hello World!';
listFiles(createFilePath(pwd()));
}
def createFilePath(path) {
if (env['NODE_NAME'] == null) {
error "envvar NODE_NAME is not set, probably not inside an node {} or running an older version of Jenkins!";
} else if (env['NODE_NAME'].equals("master")) {
return new FilePath(path);
} else {
return new FilePath(Jenkins.getInstance().getComputer(env['NODE_NAME']).getChannel(), path);
}
}
#NonCPS
def listFiles(rootPath) {
print "Files in ${rootPath}:";
for (subPath in rootPath.list()) {
echo " ${subPath.getName()}";
}
}
The important thing here is that createFilePath() ins't annotated with #NonCPS since it needs access to the env variable. Using #NonCPS removes access to the "Pipeline goodness", but on the other hand it doesn't require that all local variables are serializable.
You should then be able to do the search for the file inside the listFiles() method.

Resources