Search code examples
dockerjenkinsjenkins-pipelinejenkins-declarative-pipeline

Jenkins + Docker Change workspace layout


I am trying to setup a yocto build job on a jenkins server. I am running into an issue where I need a specific folder layout where the repository contents are cloned to. The jenkins its self is running as a docker container with a connection to the "outside" docker socket to launch and start containers. Furthermore I need to cache the build directory (>20GB) in between runs, otherwise this will take ages in between runs.

I tried the following Jenkinsfile to see the layout jenkins uses:

pipeline {
    agent {
        docker {
            image 'XXXXXX/yocto:latest'
            args '-v $HOME/yocto:/home/yocto'
        }
    }
    stages {
        stage('Build') {
            steps {
                sh 'ls'
                sh 'echo "$(pwd)"'
            }
        }
    }
}

This yields me the following output:

+ls
<contents of the repository>
+pwd
/var/jenkins_home/workspace/XXXXX-yocto_master

How ever in order for my bootstrap script to work (which I can not change easily because loads of other people are relying on it) I would need the following folder layout:

/var/jenkins_home/workspace/XXXXXX-yocto_master
└── new_reposiotry_name
    └── <repository contents>

How can I instruct the Jenkins pipeline to not clone the contents of the repository into the workspace but rather put it into a folder? I know this can be done by moving files around but I would try to avoid it if there is some other way to do it.


Solution

  • Simples way probaly is to use the option checkoutToSubdirectory('myRepoDir')

    pipeline {
        options{
            checkoutToSubdirectory('myRepoDir')
        }
        agent {
            docker {
                image 'XXXXXX/yocto:latest'
                args '-v $HOME/yocto:/home/yocto'
            }
        }
        stages {
            stage('Build') {
                steps {
                    sh 'ls'
                    sh 'echo "$(pwd)"'
                }
            }
        }
    }
    
    
    

    Alternatively, you can have it as a pipeline step and use the skipDefaultCheckout() option to ignore default git checkout.

    pipeline {
        options{
            skipDefaultCheckout()
        }
        agent {
            docker {
                image 'XXXXXX/yocto:latest'
                args '-v $HOME/yocto:/home/yocto'
            }
        }
        stages {
            stage('Checkout SCM'){
                steps{
                     dir('myRepoDir'){
                         git branch: 'master', credentialsId: 'your-credential', url: 'your-repo'
                     }
                }
            }
            stage('Build') {
                steps {
                    sh 'ls'
                    sh 'echo "$(pwd)"'
                }
            }
        }
    }