How to install Java 11 through Jenkins Scripted pipeline? - jenkins

I want to install Java 11 using scripted pipeline of Jenkins, and I have tried the below ways
node('MySlaves'){
stage('TestStage'){
println "ALm Exporter Started"
tool name: '11.0.12', type: 'jdk'
}
}
node('MySlaves'){
environment {
JAVA_HOME = tool name: '11.0.12', type: 'jdk'
}
tools {
jdk '11.0.12'
}
All the above 3 methods didn't help and if I use println "JAVA_HOME set : "+env.JAVA_HOME then I am getting the value of JDK1.8. Can someone let me know how can I install Java 11 (It is present in Custom tool) in Scripted pipeline ?

Related

'.' is not recognized as an internal or external command for Gradle command in Jenkins when gradlew script is executed

I try to run SonarQube analysis for a Gradle project in a Jenkins Pipeline using the following code:
stage('SonarQube') {
withGradle {
withSonarQubeEnv('SonarQube Env') {
bat "./gradlew sonarqube"
}
}
}
The Gradle plugin is installed in Jenkins but I am getting the following error:
05:15:05 D:\*\*\*\*\*\*>./gradlew sonarqube
05:15:05 '.' is not recognized as an internal or external command,
Two things are incorrect in your code. On Windows machines you have to:
use backslashes instead of slashes in paths (./command → .\command)
execute script written for Windows (gradlew is a Unix script, gradlew.bat is a Windows script)
This code should work:
stage('SonarQube') {
withGradle {
withSonarQubeEnv('SonarQube Env') {
bat '.\\gradlew.bat sonarqube'
}
}
}
Gradle Wtapper by default is provided with two script gardlew and gradlew.bat. If your project doesn't have the gradlew.bat file, execute on your Unix machine ./gradlew wrapper. The missing file will be generated.
Btw. You don't need the Jenkins Gradle plugin, when you use Gradlew Wrapper. The plugin is required when you want to provide Gradle installations for jobs, example:
stage('SonarQube') {
withGradle {
withSonarQubeEnv('SonarQube Env') {
bat "${tool(name: 'toolId', type: 'gradle')}\\bin\\gradle.bat sonarqube"
}
}
}
toolId must much the identifiers used in the Jenkins Global Tool Configuration, examples: gradle-6.X, gradle-6.8.3 etc.

Jenkins - Is it possible to use two different versions of terraform in a jenkins file

Currently we are using terraform 11 but we would like to start moving to 12. The idea is to move module by module, which means some modules will be using terraform version 11 and those that can run on 12 will be using version 12.
My question now is that in our Jenkins file, we have a stage which downloads terraform 11 and then different stages then to run terraform, is it possible to download terraform 12 as well and have some stages then use 11 and and others use 12?
stage('Download Terraform') {
steps{
sh "wget path/terraform/0.11.8/terraform-0.11.8.zip"
sh "unzip -o terraform-0.11.8.zip"
sh "rm terraform-0.11.8.zip"
}
}
stage('Create .terraformrc') {
steps {
sh "echo ~"
writeFile file: "/home/user/.terraformrc", text: """
credentials "" {
token = ""
}
"""
}
}
stage('Enable CloudTrail') {
steps {
{code}
}
}
stage('Create Automation Lambdas') {
steps {
{code}
}
}
In the above example, i would like the "Enable Cloudtrail" stage to run terraform 12 and the "Create Automation Lambdas" stage to run with terraform 11.....
This is how I solved this (sorry if this is not exactly what you needed):
Install terraform plugin
In jenkins UI > Global Tools Configuration add multiple terraform installations and name them in a consistent, predictable way:
In you pipeline you can now 'pick' the version to use:
pipeline {
agent { label terraformdevagent }
environment {
TF_HOME = tool('terraform-0.14.4')
PATH = "$TF_HOME:$PATH"
}}
You can make an optional parameter and a function that returns the parameter if not null and a hardcoded value if the parameter is null.
This way you can have hundreds of jobs under one TF version and still test in isolation the new version.
It can also let you handle exceptions and not block your rollouts because of a handful of projects that need to be refactored for the new version.
TF_HOME = tool( "${params.newTerraformVersion}?:'terraform-0.14.4'")

Compile groovy project and run JUnit tests via Jenkins

I googled for ages now and I give up, the buzz word Groovy + Jenkins is bringing up so many false flags...
I have a Groovy project I developed in IntelliJ, it contains also a JUnit.groovy with unit tests. Now this is a script for SoapUI, no need for Maven, Ant nor Grails, but I would like to be able to compile those files on Jenkins and run the unit tests after. Is it possible to build and test those files on Jenkins? So far all solutions seem to be me manually running groovyc (commited with my repository) and then running JUnit on the JUnit.class.
So before I start to dig deeper and write a Maven, Grails or Ant file, is there another way that does not involve me pushing the GroovySDK on my git? Or is there may be a simple build script, not involving 20 libraries and steps that would build the groovy sources and run the JUnit tests :) ?
I'm new to Jenkins obviously ;), thanks for your input.
Update:
So for all as newbie as me, what was required? First I changed my local source code to a gradle project (remember to activate AutoImport in IntelliJ) and also activate the creation of the JUnit xml and since I do not use Maven and the system is "offline" we have the libs in git anyway so my build.gradle is:
version '2.5-SNAPSHOT'
apply plugin: 'groovy'
dependencies {
compile fileTree(dir: '../Library', include: ['*.jar'])
}
test {
reports {
junitXml.enabled = true
html.enabled = true
}
}
sourceCompatibility = 1.8
set up gradle wrapper for the project via gradle wrapper for the gradlew.bat
then I added a post-commit in my git-/.hooks/ so my Jenkins is triggered upon commit via curl http://jenkins:8080/git/notifyCommit?url=https://git.git&branches=dev
finally set up a pipeline on jenkins:
#!groovy
node {
stage('Checkout') {
git branch: 'dev', credentialsId: 'youwish', url: 'https://git.git'
}
stage('Build') {
dir('./Modules') {
gradle('clean')
gradle('compileTestGroovy')
}
}
stage('UnitTest') {
dir('./Modules') {
gradle('test')
junit '/build/test-results/**/TEST-*.xml'
}
}
stage('IntegrationTest') {
stage('CodeTableDownload') {
dir('./SoapUi') {
bat 'AutoRun.bat'
junit '/results/**/*-JUNIT.xml'
}
}
}
}
def gradle(command) {
bat "./gradlew.bat $command"
}
There's a Groovy plugin for Jenkins that will let you execute Groovy scripts on Jenkins.
But, why not let something like Gradle do the build and run the test for you? A minimal Gradle build file for Groovy that will do both is:
apply plugin: 'groovy'
repositories {
jcenter()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.12'
testCompile 'junit:junit:4.12'
}
You don't have to commit the GDK, just declare a dependency.

How To Deployit configuration in jenkins pipeline as code in jenkins

i found one link in google
https://docs.xebialabs.com/xl-deploy/concept/jenkins-xl-deploy-plugin.html
here the following steps are present but it is throwing
No dsl method xldCreatePackage
node {
stage('Checkout') {
git url: '<git_project_url>'
}
stage('Package') {
xldCreatePackage artifactsPath: 'build/libs', manifestPath: 'deployit-manifest.xml', darPath: '$JOB_NAME-$BUILD_NUMBER.0.dar'
}
stage('Publish') {
xldPublishPackage serverCredentials: '<user_name>', darPath: '$JOB_NAME-$BUILD_NUMBER.0.dar'
}
stage('Deploy') {
xldDeploy serverCredentials: '<user_name>', environmentId: 'Environments/Dev', packageId: 'Applications/<project_name>/$BUILD_NUMBER.0'
}
}
You need to install the XL Deploy plugin into your Jenkins installation.
Right now you have the Pipeline plugin installed which gives you the Jenkinsfile & pipeline capability only. The XebiaLabs Jenkins plugin will take advantage of that but you need to plugin to give you the functionality you want.

Execute SonarQube Scanner within Jenkins 2 Pipeline

I want to execute a "SonarQube Scanner" Step within my Jenkins 2.x Pipeline.
When I try to create a sample groovy within the pipeline-syntax I only get a groovy script of the following format:
step <object of type hudson.plugins.sonar.SonarRunnerBuilder>
Does anyone know what is the correct Step Syntax? E.g. Publish JUnit Report looks like
step([$class: 'JUnitResultArchiver', testResults: ''])
I use the following Versions:
Jenkins 2.11
SonarQube Scanner 2.6.1
SonarQube Plugin 2.4.1
I think I got it.
First you have to retrieve your SonarQube Scanner Tool
def sonarqubeScannerHome = tool name: 'SonarQubeScanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation'
Then you can call sonar-scanner via Shell:
sh "${sonarqubeScannerHome}/bin/sonar-scanner -e -Dsonar.host.url=..."
env.sonarHome= tool name: 'scanner-2.4', type: 'hudson.plugins.sonar.SonarRunnerInstallation'
withSonarQubeEnv('sonar.installation') { // from SonarQube servers > name
sh "${sonarHome}/bin/sonar-runner -Dsonar.host.url=${SONAR_HOST_URL} -Dsonar.login=${SONAR_AUTH_TOKEN} -Dsonar.projectName=xxx -Dsonar.projectVersion=xxx -Dsonar.projectKey=xxx -Dsonar.sources=."
}
You can, instead, just provide the full path to ur sonar-runner. As shown in the below snippet.
def runSonarScan(sonar_url){
withEnv(['SONAR_HOST=' + sonar_url]) {
sh '''
$/opt/sonarqube/sonar-runner-2.4/bin/sonar-runner -e -Dsonar.host.url=${SONAR_HOST}
'''
}
}
If you have specific sonar properties add them as a sonar-project.properties file as shown here Sonar Project Properties

Resources