How to mount Jenkins workspace in docker container using Jenkins pipeline - docker

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.

Related

Jenkins build failed: gradlew: not found

I want to configure Jenkins 2.375.2 to build gradle project. But when I configure the pipe using Blue Ocean plugin and I run the pipeline I get error:
+ ./gradlew build
/var/lib/jenkins/workspace/jenkins_master#tmp/durable-dcccf1cd/script.sh: 1: ./gradlew: not found
Jenkins file:
pipeline {
agent any
stages {
stage('Build Image') {
steps {
sh "echo 'building..'"
// configure credentials under http://192.168.1.28:8080/user/test/credentials/ and put credentials ID
git credentialsId: '8f6bc3ab-9ef5-4d89-8e14-4972d63325c5 ', url: 'http://192.168.1.30:7990/scm/jen/spring-boot-microservice.git', branch: 'master'
// execute Java -jar ... and build docker image
sh './gradlew build'
sh 'docker build -t springio/gs-spring-boot-docker .'
}
}
}
I tried to add Gradle config
But still I get the same error. Do you know how I can fix this issue?

ssh-agent not working on jenkins pipeline

I am newbie and trying to implement CI/CD for my hello world reactive spring project. After releasing the image to docker repo, the next step is to connect to aws ec2 and run the created image. I have already installed ssh agen plugin and tested positive in the ssh connection configured in Mangejenkins->configuration system->ssh client.
Also My system env variabes has path=C:\Windows\System32\OpenSSH\ssh-agent.exe
In the last step I am getting :
Could not find ssh-agent: IOException: Cannot run program "ssh-agent": CreateProcess error=2, The system cannot find the file specified
Check if ssh-agent is installed and in PATH
[ssh-agent] FATAL: Could not find a suitable ssh-agent provider
My Pipelien code:
pipeline {
agent any
tools {
maven 'maven'
jdk 'jdk1.8'
}
environment {
registry ="my-registry"
registryCredential=credentials('docker-credentials')
}
stages {
stage('SCM') {
steps {
git branch: 'master',
credentialsId: 'JenkinsGitlab',
url:'https://www.gitlab.com/my-repo/panda-app'
}
}
stage('Build') {
steps {
bat 'mvn clean package spring-boot:repackage'
}
}
stage('Dockerize') {
steps {
bat "docker build -t ${registry}:${BUILD_NUMBER} ."
}
}
stage('Docker Login') {
steps{
bat "docker login -u ${registryCredential_USR} -p ${registryCredential_PSW}"
}
}
stage('Release to Docker hub') {
steps{
bat "docker push ${registry}:${BUILD_NUMBER}"
}
}
stage('Deploy to AWS') {
steps {
sshagent(['panda-ec2']) {
bat "ssh -o StrictHostKeyChecking=no ubuntu#my-aws-host sudo docker run -p 8080:8080 ${registry}:${BUILD_NUMBER}"
}
}
}
}}
The build-in SSH-agent of Windows is incompatible with Jenkins SSH-Agent plugin.
I'm using the SSH-agent from the Git installation. Make sure to insert the directory(!) path of Git ssh-agent.exe before any other path, to prevent the use of Windows SSH-agent.
With a default Git for Windows installation, you can set the PATH environment variable like this:
path=c:\Program Files\Git\usr\bin;%path%
For me it didn't work to set the env var from within Jenkins UI. I added it through the settings app. When doing so, make sure to insert it before "%SystemRoot%\system32\OpenSSH".

Getting error Jenkin pipeline docker: command not found

Dockerfile:
pipeline {
agent any
stages {
stage ('Compile') {
steps {
withMaven(maven: 'maven_3_6_3') {
sh 'mvn clean compile'
}
}
}
stage ('unit test and Package') {
steps {
withMaven(maven: 'maven_3_6_3') {
sh 'mvn package'
}
}
}
stage ('Docker build') {
steps {
sh 'docker build -t dockerId/cakemanager .'
}
}
}
}
docker build -t dockerId/cakemanager .
/Users/Shared/Jenkins/Home/workspace/CDCI-Cake-Manager_master#tmp/durable-e630df16/script.sh:
line 1: docker: command not found
First install docker plugin from Manage Jenkins >> Manage Plugins >> Click on available and search for Docker and install it.
and then configure it on Manage Jenkins >> Global tool configuration.
You need to manually install docker on your Jenkins master or on agents if you're running builds on them.
Here's the doc to install docker on OS X https://docs.docker.com/docker-for-mac/install/

mvn command not found in jenkins scripted pipeline

I am new to jenkins. I am getting the below error when i am trying to trigger scripted pipeline through jenkins.
/Users/Shared/Jenkins/Home/workspace/pipelinedemo#tmp/durable-a4f2db2a/script.sh:
line 1: mvn: command not found
Below is the code snippet .
pipeline {
agent any
stages {
stage('clone repo and clean it ') {
steps {
sh "rm -rf my-app"
sh "git clone https://github.com/Testing/my-app"
sh "mvn clean -f my-app"
}
}
stage('Test') {
steps {
sh "mvn test -f my-app"
}
}
stage('Deploy') {
steps {
sh "mvn package -f my-app"
}
}
}
}
Please note that i am able to run mvn command through freestyle project. i am getting this error from scripted line. Please answer this. thanks in advance. Geeth
ssh to your linux server, then
1. Install maven on your linux machine.
2. Add it to your PATH variable. (you should able to run mvn -version command there)
3. Restart your jenkins service.
Then, you should be able to use it in your pipeline script.

jenkins 2.7 pipeline build in docker container

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.

Resources