airdata
airdata

Reputation: 641

Jenkins job separate workspace by build

We have a job which generate some html files in the workspace folder during the build. Our goal is to get those files after the build is completed and zip them.

The first step of the job is to clean workspace - to be sure that there is no files from previous builds.

Our problem apear when we start a build and someone start separate build - the workspace became wiped. The both of the builds are generating those html's and the content become mixed from the different builds.

If somebody having idea how can I separate every build to have their own workspace will be very glad to share it. I want this to be applied only for ONE job. Other jobs must stay with shared workspace.

Upvotes: 1

Views: 3274

Answers (3)

airdata
airdata

Reputation: 641

This is the solution that I've looked for:

pipeline {
    agent {
        node {
            label 'master'
            customWorkspace "${JENKINS_HOME}/workspace/${JOB_NAME}/${BUILD_NUMBER}"
        }
    }
}

In the end I use cleanup to remove generated folders for each build like this:

post {
    cleanup {
        deleteDir()
        dir("${workspace}@tmp") {
            deleteDir()
        }
        dir("${workspace}@script") {
            deleteDir()
        }
    }
}
Thanks you guys

Upvotes: 5

Doyniish
Doyniish

Reputation: 11

Under the job configuration, check 'Use custom workspace' and pass where you want this workspace to be made. I usually have the workspace directories made by passing in the $BUILD_NUMBER variable for the directory.

Use Custom Workspace

Upvotes: 0

EricD
EricD

Reputation: 316

If you have a pipeline job, you can just add a post action before termination of your job:

something like :

if (currentBuild.result == "SUCCESS") {
    sh '''
        tar czf myArchive.tgz *.html
        scp myArchive.tgz xxx@xxxx:
    '''
} else {  
    step ([$class: 'Mailer', recipients: '[email protected]'])
}
cleanWs cleanWhenFailure: false

I'll do some research if you really want to manipulate workspace. Maybe you can do something with redeclaring the path for the env.WORKSPACE variable but it doesn't seems great to me.

Upvotes: 1

Related Questions