问题
We have a situation where JDK1.7 is used to compile a project and JDK1.8 is used in sonar pipeline job on the aforementioned compiled code.
Is it possible to use 2 JDKs in a single scripted job. If sonar stage is reached, then JDK 1.8 must be used.
Currently this is our pipeline.
node('jenkins_uat_sdg_1g'){
env.JAVA_HOME="${tool 'JDK1.7_110'}"
env.PATH="${env.JAVA_HOME}/bin:${env.PATH}"
deleteDir()
try{
try{
stage('SCM Code Checkout'){
echo "Checking out source code from SVN..."
echo "successfully checked out from SVN"
}
} catch(error){
println("Unable to checkout...there were some errors!")
currentBuild.result = "FAILURE"
error()
}
try{
stage('Compile & Package Generation'){
echo "Begining to compile the code"
bat label: 'build-maven', script: 'mvn -f pom.xml clean compile install'
echo "Successfully compiled"
}
}catch(error){
println("Unable to compile...there were some errors!")
currentBuild.result = "FAILURE"
error()
}
}
Different pipeline script is used for sonar analysis.
node('jenkins_uat_sdg_1g'){
env.JAVA_HOME="${tool 'JDK1.8'}"
env.PATH="${env.JAVA_HOME}/bin:${env.PATH}"
stage('SCM Code Checkout'){
echo "Checking out source code from SVN..."
echo "successfully checked out from SVN"
}
stage('sonarqube analysis'){
withSonarQubeEnv('SonarServer'){
bat label: 'sonar-analyis', script: '"D:/Apache Build Tools/apache-maven-3.6.1-bin/apache-maven-3.6.1/bin/mvn" org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar -f parent/pom.xml -Dsonar.host.url=http://10.xx.xx.xx:9500 -Dsonar.login=xxxxxxxxxxxxxxxxxxxxxa4b4cf180c6'
}
}
}
stage('Quality Gate'){
timeout(time: 5, unit: 'MINUTES'){
def qg = waitForQualityGate()
if(qg.status != 'OK'){
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
Is it possible to have a single script which utilizes two JDKs in a single job.
Regards
回答1:
You can make use of withEnv step to set one or more environment variables within a block. These are available to any external processes spawned within that scope
The following code works for me where i am accessing different Java versions in different stages:
node('slave1') {
deleteDir()
try {
try {
stage('During Build') {
withEnv(['JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin']) {
sh '${JAVA_HOME}/java -version'
println("or whatever command you want to run in this block...")
}
}
} catch(error) {
println("Unable to find Java 7!")
currentBuild.result = "FAILURE"
error()
}
try {
stage('During Sonar Analysis') {
withEnv(['JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk.x86_64/bin']) {
sh '${JAVA_HOME}/java -version'
println("or whatever command you want to run in this block...")
}
}
} catch(error) {
println("Unable to find Java 8!")
currentBuild.result = "FAILURE"
error()
}
} catch(error) {
println("Last catch block!")
error()
}
}
Note: As you would have noticed above, we are using single quotes in Groovy, so the variable expansion is being done by the Bourne shell, not Jenkins
Output:
回答2:
I found maven pipeline plugin (withMaven) and it resolved the issue. Now I am able to select jdk 1.7 for compilation and jdk 1.8 for sonarqube analysis.
Regards
Kris
来源:https://stackoverflow.com/questions/57692448/using-multiple-jdks-in-single-scripted-pipeline