Get gradle variables in jenkins pipeline script
Solution 1
I think you actually have two different approaches to tackle this problem :
1. Get version/group from sh script
Find a way to get Gradle version from gradle build tool (e.g. gradle getVersion()
, but I'm not familiar with Gradle) and then use shell script to get this version. If Gradle command to get the version is gradle getVersion()
, you would do in your pipeline :
def projectVersion = sh script: "gradle getVersion()", returnStdout: true
def projectGroup= sh script: "gradle getGroup()", returnStdout: true
and then just inject your $projectVersion
and $projectGroup
variables in your current pipeline.
2. Configure your Gradle build script to publish to Artifactory
This is the reverse approach, which I personnaly prefer : instead of giving Artifactory all your Gradle project information, juste give Gradle your Artifactory settings and use Gradle goal to easily publish to Artifactory.
JFrog has a good documentation for this solution in their Working with Gradle section. Basically, you will follow the following steps :
- Generate a compliant Gradle build script from Artifactory using
Gradle Build Script Generator
and include it to your project build script - Use Gradle goal
gradle artifactoryPublish
to simply publish your current artifact to Artifactory
Solution 2
For future reference here an example with the more modern declarative pipeline:
pipeline {
agent any
stages {
stage('somestage') {
steps {
script {
def version = sh (
script: "./gradlew properties -q | grep \"version:\" | awk '{print \$2}'",
returnStdout: true
).trim()
sh "echo Building project in version: $version"
}
}
}
}
}
see also:
- Gradle plugin project version number
- How to do I get the output of a shell command executed using into a variable from Jenkinsfile (groovy)?
p.streef
Updated on June 14, 2022Comments
-
p.streef almost 2 years
I'm trying to migrate my build pipelines to the "Pipeline plugin" using the groovy build scripts.
My pipelines are usually:
- Test (gradle)
- IntegrationTest (gradle)
- Build (gradle)
- Publish (artifactory)
I would like to use the gradle variables like version/group etc. in my jenkins build script to publish to the correct folders in artifactory. Something the artifactory plugin would take care of for me in the past. How can this be achieved?
For a single gradle project I use something like this:
node('master') { def version = 1.0 def gitUrl = 'some.git' def projectRoot = "" def group = "dashboard/frontend/" def artifactName = "dashboard_ui" def artifactRepo = "ext-release-local" stage "git" git branch: 'develop', poll: true, url: "${gitUrl}" dir(projectRoot) { sh 'chmod +x gradlew' stage "test" sh './gradlew clean test' stage "build" sh './gradlew build createPom' stage "artifact" def server = Artifactory.server('artifactory_dev01') def uploadSpec = """{ "files": [ { "pattern": "build/**.jar", "target": "${artifactRepo}/$group/${artifactName}/${version}/${artifactName}-${version}.jar" }, { "pattern": "pom.xml", "target": "${artifactRepo}/$group/${artifactName}/${version}/${artifactName}.pom" } ] }""" def buildInfo1 = server.upload spec: uploadSpec server.publishBuildInfo buildInfo1 } }
-
mturra about 6 yearsThe 1st approche doesn't work if you don't write a task getVersion and getGroup. More the gradle output is difficult to parse. The 2nd approche is good but I don't want to store artifact repository credential in gradle files. I store credential/token/certificate in CI (jenkins credential) server and I need a method to pass it to the publish task.
-
chinabuffet about 5 yearsWas the call to
trim()
required, or just a best practice/recommendation? -
Yavuz Tas over 2 yearsExcellent answer! To make it perfect, I propose using
\"^version:\"
instead of\"version:\"
. Sometimes it can interfere if you have another Gradle property likesome_dubious_library_version: 1.2.3
as I observe.