Search code examples
jenkinsjenkins-pipelinejenkins-declarative-pipeline

Variable expansion in Jenkins declarative pipeline on Windows


Consider the following pipeline:

pipeline {
/* continuous build pipeline for jenkins */
    agent any
    environment {
        /* initialize vairables for this job */
        path_msbuild = 'C:\\Program Files (x86)\\MSBuild\\14.0\\Bin\\msbuild.exe'
        path_workspace_root = 'C:\\Program Files (x86)\\Jenkins\\workspace\\MyApplication'
        path_solutionfile = '%path_workspace_root%\\MyApplication.sln' /* this variable doesn't expand */
        databasename = 'elements'
    }
    stages {
        stage ('solution') {
            steps {
                echo 'building solution'
                bat '"%path_msbuild%" "%path_solutionfile%" /p:Configuration=Release'
            }
        }
    }
}

This build job fails because the %path_workspace_root% does not expand and I get an error that the file I'm looking for cannot be found.

I have tried declaring the strings with double quotes:

pipeline {
/* continuous build pipeline for jenkins */
    agent any
    environment {
        /* initialize vairables for this job */
        path_msbuild = 'C:\\Program Files (x86)\\MSBuild\\14.0\\Bin\\msbuild.exe'
        path_workspace_root = 'C:\\Program Files (x86)\\Jenkins\\workspace\\MyApplication'
        path_solutionfile = "%path_workspace_root%\\MyApplication.sln" /* this variable still doesn't expand */
        databasename = 'elements'
    }
    stages {
        stage ('solution') {
            steps {
                echo 'building solution'
                bat '"%path_msbuild%" "%path_solutionfile%" /p:Configuration=Release'
            }
        }
    }
}

I have also tried using double quotes and delayed expansion syntax:

pipeline {
/* continuous build pipeline for jenkins */
    agent any
    environment {
        /* initialize vairables for this job */
        path_msbuild = 'C:\\Program Files (x86)\\MSBuild\\14.0\\Bin\\msbuild.exe'
        path_workspace_root = 'C:\\Program Files (x86)\\Jenkins\\workspace\\MyApplication'
        path_solutionfile = "!path_workspace_root!\\MyApplication.sln" /* this variable still doesn't expand */
        databasename = 'elements'
    }
    stages {
        stage ('solution') {
            steps {
                echo 'building solution'
                bat '"%path_msbuild%" "%path_solutionfile%" /p:Configuration=Release'
            }
        }
    }
}
  1. What is the correct syntax to get the %path_workspace_root% variable to expand correctly?
  2. Am I doing this "the hard way" (I'm new to Jenkins) and is there an easier way to accomplish what I'm doing? I imagine that as my pipeline gets larger I will have a number of these environment variables that need to be set.

Solution

  • Variable expansion using the %% syntax is ONLY for use in the BAT '' command. The standard jenkins syntax ${} is what I needed:

    pipeline {
    /* continuous build pipeline for jenkins */
        agent any
        environment {
            /* initialize vairables for this job */
            path_msbuild = 'C:\\Program Files (x86)\\MSBuild\\14.0\\Bin\\msbuild.exe'
            path_workspace_root = 'C:\\Program Files (x86)\\Jenkins\\workspace\\MyApplication'
            path_solutionfile = '${path_workspace_root}\\MyApplication.sln' /* this variable doesn't expand */
            databasename = 'elements'
        }
        stages {
            stage ('solution') {
                steps {
                    echo 'building solution'
                    bat '"%path_msbuild%" "%path_solutionfile%" /p:Configuration=Release'
                }
            }
        }
    }