I got this code from my Jenkins groovy script:
./gradlew -PgroupParam='123' -PversionParam=${params.versionParam} clean build --info
I want to know how I we use these parameters (groupParam, versionParam) in my gradle.build file?
What is the best practice for it ?
In my gradle.build I tried to get them like this and it returned null:
def groupParam = System.getProperty("groupParam")
def versionParam = System.getProperty("versionParam")
And also this approach returned the same:
def groupParam = System.getenv("groupParam")
def versionParam = System.getenv("versionParam")
The parameters passed can be accessed using method project.getProperty(String). In your case you can use project.getProperty('groupParam') and project.getProperty('versionParam').
getProperty(String) will return MissingPropertyException incase the property does not exist so it is better to use menthod hasProperty(String) before you use getProperty().
if(project.hasProperty('groupParam')) {
// do this
}
Related
I have a step in a pipeline that pulls objects from the context and uses them. However, I need to access those objects outside of the steps to feed into different steps, and the second step doesn't expose it.
stage() {
steps {
script {
def status = waitForQualityGate()
// Use the taskId
}
}
}
}
The waitForQualityGate() call only returns a boolean, so I can't access it there.
I could instead manually initialize the step, like so:
script {
def qualityGate = new WaitForQualityGateStep()
def taskId = qualityGate.getTaskId()
}
but the taskId is null. If I try to run the start methods manually on the step:
script {
def qualityGate = new WaitForQualityGateStep()
qualityGate.start().start()
def taskId = qualityGate.getTaskId()
}
It fails with the message:
java.lang.IllegalStateException: you must either pass in a StepContext to the StepExecution constructor, or have the StepExecution be created automatically
The WaitForQualityGateStep has the info I need, but I can't initialize it without having a StepContext (which is an Abstract class). How can I get one from the pipeline?
You can define the variable before the pipeline and in the step just set its value. This way the variable is visible across the pipeline.
I still have no idea how to manually get a step context to manually execute a step, but in case anyone else finds this by trying to get information out of the Sonar plugin, this is how I got the task ID that I needed.
def output = sh(script: "mvn sonar:sonar", returnStdout: true)
echo output // The capture prevents printing to console
def taskUri = output.find(~'/api/ce/task\\?id=[\\w-]*')
I have a jenkins build that needs to get the filenames for all files checked in within a changeset.
I have installed groovy on the slave computer and configured Jenkins to use it. I am running the below script that should return the names (or so I assume as this may be wrong as well) and print to the console screen however I am getting this error:
groovy.lang.MissingPropertyException: No such property: paths for class: hudson.plugins.tfs.model.ChangeSet
Here is the Groovy System Script:
import hudson.plugins.tfs.model.ChangeSet
// work with current build
def build = Thread.currentThread()?.executable
// get ChangesSets with all changed items
def changeSet= build.getChangeSet()
def items = changeSet.getItems()
def affectedFiles = items.collect { it.paths }
// get file names
def fileNames = affectedFiles.flatten().findResults
fileNames.each {
println "Item: $it" // `it` is an implicit parameter corresponding to the current element
}
I am very new to Groovy and Jenkins so if its syntax issue or if I'm missing a step please let me know.
I don't know the version of jenkins you are using but according to the sourcecode of ChangeSet that you can find here I suggest you to replace line 9 with:
def affectedFiles = items.collect { it.getAffectedPaths() }
// or with the equivalent more groovy-idiomatic version
def affectedFiles = items.collect { it.affectedPaths }
Feel free to comment the answer if there will be more issues.
I use parameterized builds in a lot of places. I'd like a pipeline's input to be a build number of a different job. I haven't found a clean way to query Jenkins from within groovy to get the other job's build parameters. Does anybody have a snippet to share?
This works for me in script console
import hudson.model.ParametersAction
def getParams(String project, String buildNumber){
def params=[]
Jenkins.instance.getItem(project).getBuild(buildNumber).getActions(ParametersAction)
.each { action ->
action.getParameters().each {
params << it
}
}
return params
}
I have a parameterized build like below:
then i've create a groovy script to create a variable URL_TOMCAT where its value depends on the TARGET_TOMCAT parameter:
Even after this update i got the same error
import hudson.model.*
def target = build.buildVariableResolver.resolve("TARGET_TOMCAT")
def URL_TOMCAT = ""
switch(target ) {
case "tomcat1": URL_TOMCAT= "http://localhost:8080/manager/text"
break
case "tomcat2": URL_TOMCAT = "http://localhost:8089/manager/text"
break
}
Then i want to get The URL_TOMCAT value and adjust the maven build step as shown:
But i got this error :
Has any on an idea how to fix this error?
In your groovy script you need to make an API call to get the value of the parameter from Jenkins into your workspace.
Import hudson.model
def targetTomcat = build.buildVariableResolver.resolve("TARGET_TOMCAT")
def URL_TOMCAT = ""
switch(targetTomcat) {
case "tomcat1": URL_TOMCAT = "http://localhost:8080/manager/text"
break
case "tomcat2": URL_TOMCAT = "http://localhost:8089/manager/text"
break
}
I want to point out that the URL_TOMCAT variable won't be available to any other buildsteps, it's scoped to just the groovy build step. If you want to expose your URL_TOMCAT variable to the rest of the build you'll need to expose it to the build environment somehow. I normally do this by writing the value to a file as a key value pair and using the EnvInject Plugin
You can write it to a file in groovy like so:
def workspace = build.buildVariableResolver.resolve("WORKSPACE")
new File("${workspace}\\Environment.Variables").write("URL_TOMCAT=${URL_TOMCAT}")
If you don't want to write it to the jobs workspace you can skip grabbing that value and just code a specific path.
After your groovy build step add an Envinject build step and enter the path to the file containing the key value pair in the Properties File Path field. You should be able to reference URL_TOMCAT like any other environment variable in the rest of the build. Continuing with the about path I would use ${WORKSPACE}\Environment.Variables as the path.
I'm using Extensible choice to define two parameters: NEXT_CHANGE and NEXT_RELEASE as follow:
but when i tried to use NEXT_CHANGE variable in NEXT_RELEASE groovy script it doesn't work. i tried both these methods with no success:
def hardcoded_param = "NEXT_CHANGE"
def resolver = build.buildVariableResolver
def hardcoded_param_value = resolver.resolve(hardcoded_param)
return [hardcoded_param_value]
and
def thr = Thread.currentThread()
def build = thr?.executable
def envVarsMap = build.parent.builds[0].properties.get("envVars")
eturn [envVarsMap.get('NEXT_CHANGE')]
any thought?
You are trying to use this plugin in wrong way. Look at this question and its answer