Search code examples
jenkinsjenkins-pipelinecicd

Jenkins pipeline stages workspace changing


I have a jenkins pipeline setup where I'm basically trying to do this:

Stage 1: Checkout code on Windows
Stage 2: Build Java on Windows
Stage 3: Build C++ on Windows
Stage 4: Checkout code on Linux
Stage 5: Build C++ on Linux
Stage 6: Upload builds

Here's the skeleton of my jenkins pipeline:

pipeline {
    
    agent none
    stages {
        stage('Windows Checkout') {
            agent { 
                label {
                    label 'windows' 
                }
            }
            
            steps {
                script {
                    echo 'Pull the code'
                }
            }  // steps
        }
        stage('Windows Java Build') {
            agent{ label 'windows'}
            steps {
                script {
                    echo "In Windows Java Build"
                }
            }  // steps
        }
        stage('Windows C++') {
            agent { 
                label {
                    label 'windows' 
                }
            }
            steps {
                echo "In Windows CPP Build"
            }  // steps
        }  // stage
        stage ('Linux Checkout') {
            agent{
                node{
                    label 'RHEL_7'
                }
            }
            steps {
                echo "Linux checkout code"
            }
        }   // stage

        stage ('Linux Build') {
            agent{
                node {
                    label 'RHEL_7'
                }
            }
            steps {
                script {
                    echo "Building linux..."
                    echo "stash linuxZip"
                }
            }
        }
        stage ('Publish Artifacts') {
            agent {
                label {
                    label 'windows'
                }
            }
            steps {
                echo "Starting the Publish"
                script {
                    echo "unstash linuxZip"
                    echo "remote copy linux and windows zips"
                }
            }
        }        
    }
}

The stages are serial and depend on work done in the previous stages (code is only pulled down once for Windows, then subsequent stages process it). This works without issue as long as I run 1 job at a time.

My problem: if I run multiple jobs, the workspaces between stages within each job change sometimes. For example, if I start job1, it starts using workspace WS. Then if I start job2 in parallel, it starts using workspace WS@2 (this part is good). Then if I abort job1, stage 2 of job2 reverts to using workspace WS instead of WS@2 (this part I need to fix), which causes it to fail, or pass but using the wrong code branch (silently doing the wrong thing).

My questions: Is there something I'm missing in the pipeline where I can tell jenkins to choose a workspace at the beginning, reserve it for the entire job, and reuse it throughout stages? Note that none of these are parallel (for now), so if a job is out working on the linux node (and the windows node is free), I don't want a second job to come in and start messing up the windows workspace that the first job already built but has not yet uploaded. Also note that docker is not in play here, so the reuseNode parameter is not available.


Solution

  • Each time agent declaration is used, a new workspace is created. Sometimes it coincides with a previously used location, but this should not be relied upon.

    Reuse agent declaration to prevent agent and workspace switching.

    To transfer files between agents and workspaces, use stash and unstash steps.