Windows 上 Jenkins 声明管道中的变量扩展

Variable expansion in Jenkins declarative pipeline on Windows

考虑以下管道:

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'
            }
        }
    }
}

此构建作业失败,因为 %path_workspace_root% 没有展开,并且我收到一条错误消息,指出找不到我要查找的文件。

我试过用双引号声明字符串:

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'
            }
        }
    }
}

我也尝试过使用双引号和延迟扩展语法:

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. 正确扩展 %path_workspace_root% 变量的正确语法是什么?
  2. 我这样做了吗"the hard way"(我是 Jenkins 的新手),有没有更简单的方法来完成我正在做的事情?我想随着我的管道变大,我将有许多需要设置的环境变量。

使用 %% 语法的变量扩展仅用于 BAT '' 命令。标准的詹金斯语法 ${} 是我需要的:

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'
            }
        }
    }
}