How I pass params from Jenkins to Gradle

Hello,
I need to pass a params from Jenkins to Gradle (build with params). I have a String and I need to use this String params to make my release ?

If you use the Gradle Jenkins plugin, Jenkins build parameter are passed to Gradle as System properties.

(This can be seen in the build log : your build parameter is passed to gradle thanks to -D… on the command line launching gradle)

Therefore, if you have a Jenkins Build parameter FOO, you can access it in your Gradle builds with
System.getProperty(‘FOO’)

Beware that if your Build parameter is not a string but a boolean value, you will retrieve it as a string
System.getProperty(‘BOOLEAN_PARAM’) returns ‘true’ or 'false’
You can then convert it to a boolean (if needed) with the toBoolean() method.

Small tip:
Declare aGradle property for the Systen properties map if you need to access to several properties.

build.gradle:
ext.systemProperties= System.properties
println “System property FOO is ${systemProperties.FOO}”

1 Like

Thanks you… working…
Now I need to know how I replace a config files in zip files.
Example :
By default, I have a config.xml file in my project at folder config/config.xml then , form Jenkins I will upload a zip file configuration.zip that contains config.xml … So from gradle I need to unzip configuration.zip and replace the config.xml from unzip folder to the default config.xml …

Any help ?

Here is a topic explaining how to unzip with Gradle

and after you can perform a copy to replace your xml file

You also have the Gradle Zip task to rebuild your zip if needed
https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Zip.html

To spice it up nicely

project.copy {
  from zipTree('/path/to/zip') {
    exclude 'path/to/config.xml'
  }
  from '/folder/your/default/condig/is' {
    include 'config.xml'
  }
  into '/path/to/dest/folder'  
}

I used project.copy, but dependeing on your situation a Copy task must be better. Syntax should be the same.

Hi ,
Thank you for your reply,
I have try with this code

but it is not working when I excute my task from ANdroid Studio

If you keep the include portion, only the matching elements will be copied. That’s probably not what you want. You have to remove it or adapt it for your needs. I only wrote an example.

Thank you …

it is working :
task unzip(type: Copy) {
from zipTree(‘C:/Users/dell/Desktop/Config/configuration_tabs.zip’)

into 'src/main/res/xml/'

println 'Unzip configuration files...'
}

Now, I need to upload this file from Jenkins as file params then unzip it into the folder 'src/main/res/xml’
how I can upload a file ? Any help ?

well its not obvious, but you can just add parameter behind your task. buildController -Dmode=saas. works for me, but I didnt tested -P yet.