How to mount Jenkins workspace in docker container using Jenkins pipeline

前端 未结 4 1071
攒了一身酷
攒了一身酷 2020-12-13 20:34

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 so

相关标签:
4条回答
  • 2020-12-13 21:01
    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.

    0 讨论(0)
  • 2020-12-13 21:06

    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
                    }
                }
    
    0 讨论(0)
  • 2020-12-13 21:15

    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.

    0 讨论(0)
  • 2020-12-13 21:20

    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'
            }
        }
    
    0 讨论(0)
提交回复
热议问题