Jenkinsfile Parameters Issue : Issue while running with multiple parameters to bat maven - java

I have an issue while running my job through the Jenkins file, whatever parameters I gave its updated in the place where I called it, but when it runs with bat mvn command it tool goal as that of the parameter itself. Suppose I Specified the parameter name is QA from my Jenkins build with parameter and run it ..it complete all the steps except the last one where i have build, bat mvn, it tried to run taking goal and lifecycle as QA instead of clean test-compile.
I have attached the Jenkinfile herewith.
pipeline {
agent { label 'TESTING' }
tools {
maven 'maven'
}
parameters{
choice(name: 'ENVIRONMENT' , choices: ['QA' , 'UAT' , 'PREPROD'] , description: '')
choice(name: 'SUITE' , choices: ['Regression' , 'Smoke'] , description: '')
}
stages {
stage('Clone') {
steps {
git branch: 'master' , credentialsId: 'xyzabc' , url: "https://git.xyz.com/project/automation.git"
}
}
stage('Git Checkout') {
steps {
git(branch: 'development',
credentialsId: 'xyzabc',
url: "https://git.xyz.com/project/automation.git")
}
}
stage('Build') {
steps {
echo "parameter name from jenkinsfile Environment is ${params.ENVIRONMENT}"
echo "parameter name from jenkinsfile SUITE is ${params.SUITE}"
bat "mvn clean test-compile exec:java -Dexec.mainClass='com.test.mainClass' -Dexec.args='${params.ENVIRONMENT} ${params.SUITE}' -Dexec.classpathScope=test"
}
}
}
}
ERROR MESSAGE IN JENKINS RUN,

Related

Deployment pipeline with Jenkins

I have been trying to do cicd with jenkin pipeline.
I have pull project from git, create jar and run the jar file
Jar file starts the server
and after the server starts the next stage doesn't gets executed
next step is to run karate test cases.
Got stuck here.
This is my pipeline script::
node {
def mavenHome = tool name: "Maven", type: "maven"
stage('git clone') {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'b6423164-b18d-43e0-a48f-edd10f90396d', url: 'https://github.com/prajdshakya93/Employee-Record-Managment-System.git']]])
}
stage('build artifact with dependencies'){
bat "${mavenHome}/bin/mvn clean compile assembly:single"
bat("xcopy config.properties C:\\ProgramData\\Jenkins\\.jenkins\\workspace\\ermsPipelinePratice\\target")
}
stage('run jar file'){
dir("target") {
bat "java -jar erms-1.0-SNAPSHOT-jar-with-dependencies.jar"
}
}
stage('run karate'){
bat "${mavenHome}/bin/mvn test -Dtest=KarateTests"
}
}

Jenkins Pipeline: Build docker within docker container

I'm trying to do the following
Checkout the code
Do some prechecks using some other docker images (don't want to install these on Jenkins node)
Build jar using docker image maven:3.6-jdk-8
Then run Dockerfile to build app image
Push the image to repository
Now, I don't want to install anything apart from Docker on Jenkins node. I want to run the full pipeline in Docker container to achieve this. What I'm struggling is how to build the 4th step from within the container.
I wrote the Jenkinsfile as below
pipeline {
agent none
stages {
stage('Maven build') {
agent {
docker {
image 'maven:3.6-jdk-8'
args '-u root:root'
}
}
steps {
checkout(
[
$class: 'GitSCM',
branches: [
[name: '*/master']
],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [
[
credentialsId: '<cred-id>',
url: '<github-url>']
]
])
sh '''
set -eux pipefail
mvn -e clean install
'''
}
}
stage('Build docker image') {
// Which docker image to use?
}
}
}
But I'm not sure how to build a docker image within container. The search didn't help that much. I tried using the Jenkins node for the docker image building but it seems I cannot mix and match. I totally understand this is quite an open question but I think it would be helpful to know the straightforward answer(s).
Have a look at this Jenkins Plugin: https://docs.cloudbees.com/docs/admin-resources/latest/plugins/docker-workflow

Coveralls badge is displaying as 'unknown'

I have developed a springboot application with Gradle. I have integrated Jenkins and now I'm trying to integrate code coverage into my project. I was using JaCoCo coveralls with Jenkins CI for the purpose.
Gradle plugin
plugins {
id 'com.github.kt3k.coveralls' version '2.10.1'
}
jacocoTestReport {
reports {
xml.enabled = true
html.enabled = true
}
getClassDirectories().setFrom(classDirectories.files.collect {
fileTree(dir: it,
exclude: [
'**/BmsService*',
'**/BmsClientWrapper*'])
})
}
And my Jenkins job invokes the following JaCoCo test command.
sh "gradle clean test jacocoTestReport coveralls"
My Jenkins build gets successful and the data is being sent to https://coveralls.io successfully.
My question is: if I do a build with feature branch (except master), in coveralls.io the badge was not displayed properly and even it doesn't show the 'committed by' as well. I'm using GitHub by the way.
Any help would be grateful!
Following are the pipeline jobs I have:
stage('Git checkout') {
deleteDir()
checkout(scm)
}
stage('Build Source code') {
container('gradle-docker') {
sh 'gradle -version'
sh 'gradle clean'
sh "gradle build"
}
}
stage('Code Coverage') {
if('master'.equals(env.BRANCH_NAME)) {
def secrets = [
[$class: 'VaultSecret', path: 'secret/build/credentials', secretValues: [
[$class: 'VaultSecretValue', envVar: 'COVERALLS_REPO_TOKEN', vaultKey: 'coveralls_repo_token']
]]
]
wrap([$class: 'VaultBuildWrapper', vaultSecrets: secrets]) {
container('gradle-docker') {
sh "gradle clean test jacocoTestReport coveralls"
}
}
} else {
println 'Code Coverage is only for the master branch, Skipping for ' + env.BRANCH_NAME
}
}
Based on coveralls documentation you need to configure CI_BRANCH environment details.
And even it's mentioned in coveralls gradle plugin.
You need to extract this information using git commands and populate the required variables.

Jenkins pipeline DSL Maven Error due to #tmp dir

I just created a Jenkins Pipeline DSL job where I cloned a Java code from SCM and tried to run mvn clean. But the pipeline continuously throwing an error saying:
mvn clean install -Dmaven.test.skip=true -Dfindbugs.skip=true
/var/lib/jenkins/workspace/<project>#tmp/durable-77d8d13c/script.sh: 2:
/var/lib/jenkins/workspace/<project>#tmp/durable-77d8d13c/script.sh: mvn: not found
Seems like it tries to find pom.xml inside the <project>#tmp directory which is empty. Actual code is cloned successfully inside the <project> directory. Below is my Jenkinsfile:
node {
stage ("Clean Workspace") {
echo "${WORKSPACE}"
cleanWs()
}
stage ("Get Code") {
git branch: "${params.branch}", url: 'git#bitbucket.org:xx/xxxxxxx.git'
}
stage ("mvn clean") {
sh "mvn clean install -Dmaven.test.skip=true -Dfindbugs.skip=true"
}
}
I also tried with ${WORKSPACE} env variable but still does not work.
The issue was resolved, I modified the Pipeline DSL jobs by including Maven tool step as:
stage ("mvn clean") {
withEnv( ["PATH+MAVEN=${tool name: 'mvn', type: 'maven'}/bin"] ) {
sh "mvn clean install -Dmaven.test.skip=true -Dfindbugs.skip=true"
}
}
Instead of doing like that you can directly add mvn to path itself. for that, open your ~/.bash_profile and add these lines.
MAVEN_HOME="<path to maven folder>"
export MAVEN_HOME
PATH=$PATH:$MAVEN_HOME/bin
Don't forget to do source ~/.bash_profile to get the changes. This will provide mvn to path so that you can access mvn directly.
So if you do that your code can be now like...
stage ("mvn clean") {
sh "mvn clean install -Dmaven.test.skip=true -Dfindbugs.skip=true"
}
The error message mvn: not found means that mvn command could not be found by Jenkins pipeline.
You should use a withMaven pipeline step to provide a valid maven environnement.
For example :
node {
stage ("Clean Workspace") {
echo "${WORKSPACE}"
cleanWs()
}
stage ("Get Code") {
git branch: "${params.branch}", url: 'git#bitbucket.org:xx/xxxxxxx.git'
}
stage ("mvn clean") {
withMaven {
sh "mvn clean install -Dmaven.test.skip=true -Dfindbugs.skip=true"
}
}
}
Check that Jenkins is properly configured to use maven (Under 'Manage Jenkins > Configure System') and the 'Pipeline Maven Plugin' is installed (Under 'Manage Jenkins > Manage Plugins').
More information here : https://wiki.jenkins.io/display/JENKINS/Pipeline+Maven+Plugin

Jenkinsfile maven plugin inside a docker container

I'm using Jenkins with a Jenkinsfile that runs my builds inside a docker container. I have a simple Java application that I'd like to build and deploy to artifactory using the Jenkins Artifactory plugin.
My Jenkinsfile is below -
node {
def server = Artifactory.server "my-artifactory"
def rtMaven = Artifactory.newMavenBuild()
stage("Prepare environment"){
docker.image('driv/docker-maven-java-oracle').inside {
checkout scm
stage("Artifactory configuration") {
rtMaven.deployer releaseRepo:'libs-release-local', snapshotRepo:'libs-snapshot-local', server: server
rtMaven.resolver releaseRepo:'libs-release', snapshotRepo:'libs-snapshot', server: server
}
stage("Maven build") {
def buildInfo = rtMaven.run pom: 'pom.xml', goals: 'clean install'
}
}
}
}
However, when I run builds with the above Jenkinsfile I get the error -
java.lang.RuntimeException: Couldn't find maven installation
I know that I can add a line to my Jenkinsfile like this -
rtMaven.tool = MAVEN_TOOL
...that I can use to specify a pre-configured Jenkins Tool to point the Artifactory plugin at Maven. However, it seems to me that such a pre-configured tool would have to be on the Jenkins machine, or a build node, and not inside my docker container.
So, is it possible to point the Artifactory plugin at a maven installation inside my docker container?
Thanks.
This should probably be fixed in their jenkins plugin itself but as "Christopher Orr" suggested mapping environment vars at the pipeline stage level to the same ones set in the container work
For example
stage('mystage') {
environment {
// If your using the official maven image, these are probably where it puts it
MAVEN_HOME = '/usr/share/maven'
JAVA_HOME= '/usr/local/openjdk-8'
}
steps {
sh """
# confirm location of MAVEN_HOME and JAVA_HOME in container and set in environment directive
printenv
"""
...
// call rtMavenRun()

Categories

Resources