create custom workspace for each execution of a job

6,995 views
Skip to first unread message

niristotle okram

unread,
Feb 1, 2016, 3:23:10 PM2/1/16
to Jenkins Users
i am trying to create unique workspace for my workflow/pipeline. The workspace will contain certain files that i don't want to mess up when the job runs concurrently. my workflow looks something like 

node("master") {
stage name: 'sync', concurrency: 3
ws('/opt/mount1/jenkins/jobs/GoogleFlow/workspace/(${env.BUILD_NUMBER})') {
//code block
}

stage name: 'build_and_test', concurrency: 1
 ws('/opt/mount1/jenkins/jobs/GoogleFlow/workspace/(${env.BUILD_NUMBER})') {
  //code block
}
stage name: 'test', concurrency: 3
 ws('/opt/mount1/jenkins/jobs/GoogleFlow/workspace/(${env.BUILD_NUMBER})') {
  //code block
}
}

I am expecting jenkins to create workspaces like inside the main/default workspace (/opt/mount1/jenkins/jobs/GoogleFlow/workspace) as 
/opt/mount1/jenkins/jobs/GoogleFlow/workspace/1
/opt/mount1/jenkins/jobs/GoogleFlow/workspace/2
/opt/mount1/jenkins/jobs/GoogleFlow/workspace/3


how do i make the env variable ${env.BUILD_NUMBER} get the value while creating the workspace?

niristotle okram

unread,
Feb 1, 2016, 3:38:07 PM2/1/16
to Jenkins Users
Okay - i figured out the point of error. i should be using the " instead of ' . So it should be 

ws("/opt/mount1/jenkins/jobs/GoogleFlow/workspace/${env.BUILD_NUMBER}")

Ryan Campbell

unread,
Feb 1, 2016, 3:48:42 PM2/1/16
to Jenkins Users

Workspaces should already be unique to a given concurrent build of Jenkins. You shouldn't have to do anything special to enable this.


--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/47c32643-6963-47d2-adf0-c288fe7bda35%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

niristotle okram

unread,
Feb 1, 2016, 4:01:16 PM2/1/16
to jenkins...@googlegroups.com
Yes, i started by thinking that the workspace will be auto unique. i noticed that when i trigger mulitple build for a job (trying to simulate the concurrency), it sometime creates workspaces like 

/opt/mount1/jenkins/jobs/GoogleFlow/workspace
/opt/mount1/jenkins/jobs/GoogleFlow/workspace@2
/opt/mount1/jenkins/jobs/GoogleFlow/workspace@3
then, the next comes back to 
/opt/mount1/jenkins/jobs/GoogleFlow/workspace


i am not sure why its coming back to  this workspace "/opt/mount1/jenkins/jobs/GoogleFlow/workspace" instead of "/opt/mount1/jenkins/jobs/GoogleFlow/workspace@4". 


My guess is that the first job that was using the /opt/mount1/jenkins/jobs/GoogleFlow/workspace have moved to the next stage and the workspace have been allocated to the 4th job in the series. 

This creates a mess to my job1's file in the same workspace as the job4. The job4 starts rewriting on them. I have set my workspace to be like so now, so that each instance of the job uses a specific workspace corresponding to the BUILD_NUMBER. (Ignore the messy stupid code for the stages) 




node("master") {
ws("/opt/mount1/jenkins/jobs/GoogleFlow/workspace/${env.BUILD_NUMBER}") {
stage name: 'sync', concurrency: 1
  echo "before sync"
  sh '''touch buildFile
  echo "This is from ${BUILD_NUMBER}" >> buildFile
  cat buildFile'''
  sh "sleep 5"
  echo "after sync"
  sh "date"


stage name: 'build_and_test', concurrency: 1
  stage name: 'build'
  echo "before build"
  sh "date"
  sh '''sleep 10
  cat buildFile'''
  echo "build 1/3"
  sh "sleep 5"
  echo "build 2/3"
  sh '''sleep 5
  cat buildFile'''
  echo "build 3/3"
  sh "date"

stage name: 'test', concurrency: 1
  echo "before test"
  sh "date"
  sh '''sleep 10
  cat buildFile'''
  sh "date"
}
}


I have to decide if i want to delete this workspace after each instance or leave them for debugging. If left, then find a way to purge them. Am putting myself in a cobweb now?? :)






--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/i-8gTNug3ck/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CANHK%3DJ2SFE5TGQ_2r3oXd8mis7%2Bq__JvetXW45--A6dd%2BsbpDA%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.



--
Regards
nirish okram

Ryan Campbell

unread,
Feb 1, 2016, 4:13:45 PM2/1/16
to jenkins...@googlegroups.com
Standard recommendation is to write your job so that it can correctly clean up the workspace before building, for instance with a git reset or similar. Git scm step has flags for this.

If you want to analyze the workspace upon failure, perhaps use a try/catch block and put an archive step in the catch.

niristotle okram

unread,
Feb 1, 2016, 4:25:40 PM2/1/16
to jenkins...@googlegroups.com
ahh - yes the try/catch is an option that i missed. Now for the delete, i am referring to the various workspace folder that each run of the job/workflow is going to create with my current script. My current workflow script is creating directories with the name as the BUILD_NUMBER as unique workspace for each run. So, i will end up with lots of directories under "/opt/mount1/jenkins/jobs/GoogleFlow/workspace"

like

/opt/mount1/jenkins/jobs/GoogleFlow/workspace/1
/opt/mount1/jenkins/jobs/GoogleFlow/workspace/2
/opt/mount1/jenkins/jobs/GoogleFlow/workspace/3
/opt/mount1/jenkins/jobs/GoogleFlow/workspace/4
.
.
.
.
.
.
.

is there any DSL command to delete these workspaces, once the job succeeds? 

ws("/opt/mount1/jenkins/jobs/GoogleFlow/workspace/${env.BUILD_NUMBER}")


i see an option "deleteDir()", can i use something like this (at the end of my workflow)?

deleteDir("/opt/mount1/jenkins/jobs/GoogleFlow/workspace/${env.BUILD_NUMBER}")


For more options, visit https://groups.google.com/d/optout.



--
Regards
nirish okram

Craig Rodrigues

unread,
Feb 1, 2016, 7:06:10 PM2/1/16
to Jenkins Users
Reply all
Reply to author
Forward
0 new messages