jenkins 2.7 pipeline build in docker container - jenkins

I am trying to build my project with Jenkins and pipeline plugin in docker container. My Jenkinsfile looks like this:
node('docker') {
docker.image('build-node:1').inside {
stage 'scm checkout'
checkout scm
stage 'maven build'
sh "mvn -B clean > mvn.log"
}
}
In Jenkins log:
...
Entering stage maven build
Proceeding
[Pipeline] sh
[versioning] Running shell script
+ mvn -B clean
[Pipeline] }
$ docker stop ***
$ docker rm -f ***
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code -1
Finished: FAILURE
In mvn.log I see all is ok:
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: X.XXX s
[INFO] Finished at: 2016-XX-XXTXX:XX:XX+XX:XX
[INFO] Final Memory: XXM/XXXM
[INFO] ------------------------------------------------------------------------
Where is an error or how determine it?
My Jenkins server is docker container form docker hub, which using docker server as a node.
New check:
try {
sh 'mvn clean | tee mvn.log'
} catch (e) {
println "Maven failed : ${e}"
}
Output:
[versioning] Running shell script
+ mvn clean
+ tee mvn.log
[Pipeline] echo
Maven failed : hudson.AbortException: script returned exit code -1
mvn.log:
[INFO] BUILD SUCCESS

Perhaps try
sh 'mvn -B clean | tee mvn.log'
so that you can see the output from Maven in the build log. Or simply
sh 'mvn -B clean'
if you were not planning on using mvn.log for anything anyway.

I configure pipeline on 1651.3 LTS version of Jenkins and all work perfect. I think problem was in combination of plugins or unstable core version of Jenkins.

Related

Jenkins - groovy script returned exit code 126

I got a very simple Groovy script in Jenkins running in pipeline
For this code:
sh 'chmod +x gradlew'
sh './gradlew build --info'
I'm getting this error:
[Pipeline] sh
+ ./gradlew build --info
/var/jenkins_home/workspace/Pipeline with Gradle#tmp/durable-646d54ad/script.sh: line 1: ./gradlew: Permission denied
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 126
Finished: FAILURE
What am I missing here?
Looks like it does not have permission to run gradlew from your error message.
line 1: ./gradlew: Permission denied
You can do the following:
SSH into the jenkins machine
Switch to jenkins user
sudo su jenkins -
Check the permissions on the file gradlew using the command
ls -l /var/lib/jenkins/workspace/Pipeline with Gradle
Run the connand ./gradlew build --info from terminal and make sure it works before you run the job again
I use declarative pipelines using groovy and it works fine. I have grails installed on the machine and jenkins user can run the command. I use:
sh './gradlew dependencies && ./gradlew assemble && find ./ -name "*.war"'
Please add a comment if you have any more questions...

Jenkins inside docker how to configure path for hp fortify sourceanalyzer

I am running my jenkins instance inside docker.
I am trying to do fortify scan as a post-build step.
I have
HPE Security Fortify Jenkins Plugin
installed.
Now when I try to do something like
def call(String maven_version) {
withMaven(maven: maven_version) {
script {
sh "sourceanalyzer -b %JOB_NAME% -jdk 1.7 -extdirs %WORKSPACE%/target/deps/libs/ %WORKSPACE%/target/deps/src/**/* -source target/%JOB_NAME%.fpr"
}
}
}
But I get
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Fortify Analysis)
[Pipeline] withMaven
[withMaven] Options: []
[withMaven] Available options:
[withMaven] using JDK installation provided by the build agent
[withMaven] using Maven installation 'Maven 3.3.9'
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] sh
[Running shell script
+ sourceanalyzer -b %JOB_NAME% -jdk 1.7 -extdirs %WORKSPACE%/target/deps/libs/ %WORKSPACE%/target/deps/src/**/* -source target/%JOB_NAME%.fpr
script.sh: sourceanalyzer: not found"
I think all I need to do is create an environment variable for sourceanalyzer, but how do I see where that plugin-is, since this is a docker container and not really an operating system running. Thats where the source of my confusion is.
It is not looking for environment variable.
sourceanalyzer is a executable. and it's not available in the PATH.
Additionally : you can consider docker container as an Operating system (aggregated of multiple things and layers togather before starting.)
If you want to get into RUNNING instance of your JENKIN image then launch following command. (Ensure your container is running).
#>docker exec -it <container-id> sh
Container id is available when you launch
#>docker ps

How to mount Jenkins workspace in docker container using Jenkins pipeline

I'm using Jenkins in docker. The /var/jenkins_home is mounted on /var/jenkins-data on my host. My Jenkins can execute docker commands (mount of sockets) and I've installed the git plugin and pipeline plugin.
Now I have a pipeline job named test and the following pipeline:
pipeline {
agent any
stages {
stage('Clone') {
steps {
git branch: 'master', url: 'https://github.com/lvthillo/maven-hello-world.git'
}
}
stage('Build in Docker') {
agent {
docker {
image 'maven:3.5.2'
args '-v /var/jenkins_home/workspace/test:/opt/maven -w /opt/maven'
}
}
steps {
sh 'pwd'
sh 'mvn -v'
sh 'mvn clean install'
}
}
}
}
What I want to achieve is cloning my public repo from github. This works. In the next step I want to start a docker container (maven) and print the current directory, the maven version and perform a clean install.
The output of the 3 commands is:
[test#2] Running shell script
+ pwd
/var/jenkins_home/workspace/test#2
[Pipeline] sh
[test#2] Running shell script
+ mvn -v
Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T07:58:13Z)
Maven home: /usr/share/maven
Java version: 1.8.0_151, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "4.4.86-boot2docker", arch: "amd64", family: "unix"
[Pipeline] sh
[test#2] Running shell script
+ mvn clean install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.179 s
[INFO] Finished at: 2018-01-12T12:12:00Z
[INFO] Final Memory: 5M/31M
[INFO] ------------------------------------------------------------------------
[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (/var/jenkins_home/workspace/test#2). Please verify you invoked Maven from the correct directory. -> [Help 1]
It seems to work because maven is not installed on my host, so it's executed from inside the container, but it's suddenly creating a new workspace (#2) instead of using the existing one from where I cloned the repo. I don't want to clone the repo in my container immediately because I want multiple stages, all with different containers, but all executed on my git repo in my workspace.
What am I doing wrong or how can I fix this?
I was thinking it was maybe because of the agent step. my first step can run on any agent (any slave), the docker step will run in the docker container, but must of course run on that same slave als where the git clone was executed.
pipeline {
agent any
stages {
stage('Clone') {
steps {
git branch: 'master', url: 'https://github.com/lvthillo/maven-hello-world.git'
stash name:'scm', includes:'*'
}
}
stage('Build in Docker') {
steps {
unstash 'scm'
script{
docker.image('maven:3.5.2').inside{
sh 'pwd'
sh 'mvn -v'
sh 'mvn clean install'
}
}
}
}
}
}
You can use this pipeline even with a multi-node setup. Docker plugin mounts your workspace as a docker workspace too.Hence, it is not necessary to mount any volume unless they are outside the workspace.
Thanks, the Previous solution works for me. My version for node container and ${PWD} as param
stage('Build Solution') {
agent {
docker {
image 'node:6-alpine'
args '-v ${PWD}:/usr/src/app -w /usr/src/app'
reuseNode true
}
}
steps {
sh 'npm install'
}
}
My last explanation was helping myself to solve the problem:
This text helped me to solve it. I had to ensure that all the steps on my pipeline were using the same agent as the initial one where I performed my git clone:
Addit reuseNode true solved it:
stage('Build in Docker') {
agent {
docker {
image 'maven:3.5.2'
args '-v /var/jenkins_home/workspace/test:/opt/maven -w /opt/maven'
reuseNode true
}
}
The error message returned by maven indicates that was not able to find the pom.xml file in the directory where the execution was launched.
I had the same issue, and solved by cd to the directory containing my project pom.xml.

Jenkins pipeline marked as failed but all steps succeded

I set up pipeline to build my wildfly-swarm based microservice build docker image and push it to docker repository. So I set up pipeline script and executed build and from logs (attached bellow) we see that build was successful but I get failed status in stage view with error: "Script returned exit code 1".
And the script that failed was:
docker build -f Dockerfile -t swarm-microservice .
It does not matter if I change script execution from:
sh '''docker build -f Dockerfile -t swarm-microservice .'''
to:
script {
docker.build("swarm-microservice", '.')
}
I have also tried to change the output of the script result by changing script to:
sh '''docker build -f Dockerfile -t swarm-microservice . || true '''
But its not helping. What am I missing?
Started by user test
[Pipeline] node
Running on node-lin02 in /build/workspace/XYZ/some-docker-image
[Pipeline] copyArtifacts
Copied 2 artifacts from "XYZ » swarm-microservice" build number 49
Copied 0 artifacts from "XYZ » swarm-microservice » XYZ-swarm-microservice" build number 49
[some-docker-image] Running shell script
[some-docker-image] Running shell script
+ docker build -f Dockerfile -t swarm-microservice .
Sending build context to Docker daemon 380.9MB
Step 1/4 : FROM openjdk:8u151-jdk-slim
---> 22f79f57057d
Step 2/4 : ADD swarm-microservice-swarm.jar /opt/swarm-microservice-swarm.jar
---> Using cache
---> 16f16f07a4da
Step 3/4 : EXPOSE 8281
---> Using cache
---> 26820815d1d1
Step 4/4 : ENTRYPOINT java -jar -Djava.net.preferIPv4Stack=true -XX:MaxRAM=512m /opt/swarm-microservice-swarm.jar -S src
---> Using cache
---> 41c896987ba6
Successfully built 41c896987ba6
Successfully tagged swarm-microservice:latest
[Pipeline] sh
[some-docker-image] Running shell script
+ set -e
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (tag docker image)
[Pipeline] sh
[some-docker-image] Running shell script
+ docker tag swarm-microservice my.awesome.demo.so.test.repo.com:5000/XYZ/swarm-microservice
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (push docker image to repository)
[Pipeline] sh
[some-docker-image] Running shell script
+ docker push my.awesome.demo.so.test.repo.com:5000/XYZ/swarm-microservice
The push refers to a repository [my.awesome.demo.so.test.repo.com:5000/XYZ/swarm-microservice]
2e35fec0db40: Preparing
c09cee929b6f: Preparing
5f09fc66f922: Waiting
cec7521cdf36: Waiting
2e35fec0db40: Layer already exists
063e7100cc44: Layer already exists
latest: digest: sha256:xxx size: 1788
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

How to configure Jenkins pipeline so that if there are multiple shell scripts and if one fails the jenkins jobs still runs instead of exiting

I want to configure a Jenkins pipeline job so that it should be able to run multiple shell script jobs. Even if one shell script fails the job should run the other two before failing the job.
You need to tweak your shell script, not Jenkins pipeline to achieve what you want!
Try this in your shell script
shell script command > /dev/null 2>&1 || true
so fail/pass it will execute and go to next shell script
You can always try catch the potentially failing sh execution
node {
sh "echo test"
try {
sh "/dev/null 2>&1"
} catch (error) {
echo "$error"
}
sh "echo test1"
}
Above runs successfully and produces
Started by user Blazej Checinski
[Pipeline] node
Running on agent2 in /home/build/workspace/test
[Pipeline] {
[Pipeline] sh
[test] Running shell script
+ echo test
test
[Pipeline] sh
[test] Running shell script
+ /dev/null
/home/build/workspace/test#tmp/durable-b4fc2854/script.sh: line 2: /dev/null: Permission denied
[Pipeline] echo
hudson.AbortException: script returned exit code 1
[Pipeline] sh
[test] Running shell script
+ echo test1
test1
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Resources