red888
red888

Reputation: 31520

jenkins docker agent and workspaces

Using multibranch pipelines and running a docker agent in one of my stages I found the workspace inside the container is not being updated:

stage('run inside container') {
     agent {
         docker {
             image "my/image"}}
    steps{
       sh 'ls -l'
       ...

So I found jenkins is running this docker command:

docker run -t -d -u 1000:1000 -u root -w /home/jenkins/workspace/myworkspace@2

its mounting the workspace but with a "2". This is not the main workspace and it looks like its not being updated. How can I have it use my main workspace?

Upvotes: 5

Views: 6037

Answers (3)

alpha89
alpha89

Reputation: 316

Jenkins create new workspace for user with myworkspace@2 or @3 if more than one build is triggered parallelly. This is to make sure that two jobs should not overwrite with each other.

If your requirement is to run only one job at a time you can enable "Do not allow concurrent builds" as shown in image below for you job.

enter image description here

Upvotes: 0

SantaXL
SantaXL

Reputation: 654

Use reuseNode true in docker agent options.

stage('run inside container') {
     agent {
         docker {
             image "my/image"
             reuseNode true
         }
    }
    steps{
       sh 'ls -l'
       ...

See Workspace synchronization in the docs.

Upvotes: 3

Oweis Al-Agtash
Oweis Al-Agtash

Reputation: 188

If your goal is to run a stage inside another container, then instead of using agents, simply do this:

stage("NPM Install") {
            steps {
                script {
                    // this will  generate node_modules folder and map it 
                    // back to this workspace.
                    docker.image("node:12.14.1-alpine").inside {

                        sh "npm install"
                    }
                }
            }
        }

This way Jenkins will bind mount the current pipeline workspace with the container.

Upvotes: 3

Related Questions