So I have a file called "text1.txt" that has only one line -
version.build=846
I want to do something like this-
def svntag = ccsmp_v_ {version.build}
println $svntag
ccsmp_v_846
Is there any way to do this ?
You can read a file from the workspace using the methods from pipeline utility steps plugin.
For your case it would be best to use readProperties method, for example:
def properties = readProperties file: '<your properties file in the workspace>'
Then you can access the value with:
def svntag = "ccsmp_v_${properties['version.build']}"
Related
I have a properties file where certain string shoudl be replaced with Jenkins parameter. I ahve tried using the variable directly in the Properties file which did not work.
properties file
DOCKER_TAG_SUFFIX=-REPLACE_RELEASE_VERSION
PROPERTY_FILE_PATH=someproperty
Jenkinsfile snippet
def jboss_parameters = readProperties file: jboss_propfile
jboss_parameters .replaceAll("RELEASE_VERSION",params.RELEASE_VERSION) # try1
jboss_parameters = readFile(jboss_propfile).replaceAll("REPLACE_RELEASE_VERSION",params.RELEASE_VERSION) # try2
# try 3
jboss_parameters.each{k,v ->
if (v == "REPLACE_RELEASE_VERSION" )
jboss_parameters.setProperty($k,params.RELEASE_VERSION)
}
# try 4
def jboss_source_file = new File(jboss_propfile)
def jboss_parameters = jboss_source_file.text.replace("REPLACE_RELEASE_VERSION",params.RELEASE_VERSION)
I am not able to find another way that works for me.
println jboss_parameters output
{DOCKER_TAG_SUFFIX=-REPLACE_RELEASE_VERSION, PROPERTY_FILE_PATH=someproperty}
The readProperties step returns a dictionary (map), not a string, that is crated from the properties file.
Your first attempt (# try1) fails because maps in groovy do not have a replaceAll function like strings have and therefore you will get an error.
Your third attempt (# try3) is failing because you are comparing the map values to REPLACE_RELEASE_VERSION without the - character and therefore the comparison always fails and no values are changed.
I tested the second attempt (# try 2) and it seems to be working, so i am not sure what is your issue, but it is easier to handle properties as a map instead of a string that is retuned from the readFile method.
So if you have only specific properties that need to be updated you can update them directly:
def jboss_parameters = readProperties file: jboss_propfile
jboss_parameters.DOCKER_TAG_SUFFIX = params.RELEASE_VERSION // update relevant property
Or if you have multiple properties that should be modified you can iterate and update each value using the collectEntries method. Something like:
def jboss_parameters = readProperties file: jboss_propfile
updated_parameters = jboss_parameters.collectEntries { key, value ->
[key, value.replaceAll("REPLACE_RELEASE_VERSION",params.RELEASE_VERSION)]
}
i read a prop file from local like:
def props = readProperties file: 'dir/my.properties'
and in jenkins file, i want access the key/value from this file,so i do:
def myCustomKey = "test_"+ENV
when i try to get value from props by my custom key, i don't know how to get it, i have try below:
echo "props: ${props[$myCustomKey]}"
echo "props: ${props."$myCustomKey"}"
echo "props: ${props.myCustomKey}"
it is not working at all.anyone know how to get the value of my props with the key is a variable?
def myCustomKey = "test_"+ENV
echo """props: ${props."$myCustomKey"}"""
I can run this inside of a job dsl project:
def pluginsListFile = new File("${plugins}/plugins.txt")
def pluginsList = jenkins.model.Jenkins.instance.getPluginManager().getPlugins()
pluginsList.each {
pluginsListFile.append "${it.getShortName()}: ${it.getVersion()}\n"
}
But I want the job dsl script to create a job that runs this groovy code (on a schedule). It looked like systemGroovyCommand is what I would use be I dont understand- looks like you have to use a .groovy file for systemGroovyCommand which i would like to avoid.
Yes, it's systemGroovyCommand. You don't have to store this script in separate file, but it's a best practice.
systemGroovyCommand accepts string as parameter, so you can pass your code this way, but remember to escape special characters.
Example usage:
def script = '''
def pluginsListFile = new File("${plugins}/plugins.txt")
def pluginsList = jenkins.model.Jenkins.instance.getPluginManager().getPlugins()
pluginsList.each {
pluginsListFile.append "${it.getShortName()}: ${it.getVersion()}\\n"
}
'''
job('TEST_JOB_SCRIPT') {
steps {
systemGroovyCommand(script)
}
}
I have a parametrized build and I'd like to populate parameter values based on contents of files/directories on the local slave and/or on a remote box accessible via ssh.
It's not a problem to access local and remote files during build stages, but I need to make it work in an Active Choice Plugin (or something similar).
Apparently, sh function doesn't work, but some Java-like Groovy API is still available (as described here: https://wiki.jenkins.io/display/JENKINS/Active+Choices+Plugin)
jenkinsURL=jenkins.model.Jenkins.instance.getRootUrl()
def propFile=vPropFile //name of properties file
def propKey=vPropKey // name of properties key
def relPropFileUrl=vRelPropFileUrl // userContent/properties/
def propAddress="${jenkinsURL}${relPropFileUrl}$propFile"
def props= new Properties()
props.load(new URL(propAddress).openStream())
def choices=[]
props.get(propKey.toString()).split(",").each{
choices.add(it)
}
return choices
I wonder if it's possible to access managed files the same way or better yet to access something remotely using SSH.
Is there an API for that?
I couldn't find a solution that would allow to SSH during during Active Choices parameter script execution.
However, I was able to use configuration file(s) managed by Jenkins. Here's the code that can be run from the Active Choices parameter script:
def gcf = org.jenkinsci.plugins.configfiles.GlobalConfigFiles.get()
// Read different file based on referencedParameter ENVIRONMENT
def deploymentFileName = 'deployment.' + ENVIRONMENT + '.properties'
def deploymentFile = gcf.getById(deploymentFileName)
def deploymentProperties = new Properties();
deploymentProperties.load(new java.io.StringReader(deploymentFile.content))
def choices = []
// Make use of Properties object here to return list of choices
return choices
Later in the main Groovy Script of the pipeline it's possible to update file the same way, but the file has to be read/loaded again as the script context is different:
def gcf = org.jenkinsci.plugins.configfiles.GlobalConfigFiles.get()
def deploymentFile = gcf.getById(deploymentFileName)
def deploymentProperties.load(new java.io.StringReader(deploymentFile.content))
// Update deploymentProperties as necessary here.
def stringWriter = new java.io.StringWriter()
deploymentProperties.store(stringWriter, "comments")
// Content of the deploymentFile object is immutable.
// So need to create new instance and reuse the same file id to overwrite the file.
def newDeploymentFile = deploymentFile.getDescriptor().newConfig(
deploymentFile.id, deploymentFile.name, deploymentFile.comment, stringWriter.toString())
gcf.save(newDeploymentFile)
Of course, all necessary API permissions have to be granted in Jenkins.
In my jenkins pipeline I am working with properties stored in file.
I can read properties from file and add new items to the map using this code, but I do not understand how to persist my changes.
node('hozuki-best-girl') {
def propertiesPath = "${env.hozuki_properties}"
def props = readProperties file: propertiesPath
props['versionCode'] = 100500
}
What should I do in order to persist my changes? There is no writeProperties method here https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#code-readproperties-code-read-properties-from-files-in-the-workspace-or-text
you can use yaml format instead of properties.
it also simple and human readable and in jenkins-pipeline there are read and write operations for yaml
or you can use this kind of code:
#NonCPS
def propsToString(Map map){
return new StringWriter().with{w-> (map as Properties).store(w, null); w; }.toString()
}
writeFile file: propertiesPath, text: propsToString(props)
The Phoenix AutoTest Plugin has a writeProperties step.