Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mount Jenkins workspace in docker container using Jenkins pipeline

Tags:

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.

like image 782
lvthillo Avatar asked Jan 12 '18 12:01

lvthillo


People also ask

Can we deploy Jenkins application on Docker?

Running Jenkins from a Docker image provides a convenient method for launching Jenkins in a self-contained and preconfigured environment. In this post you learned how to: Launch Jenkins in a Docker container. Install additional tools and plugins.


2 Answers

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.

like image 81
Ram Avatar answered Sep 17 '22 17:09

Ram


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'
        }
    }
like image 20
Yuriy Shpool Avatar answered Sep 16 '22 17:09

Yuriy Shpool