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

时间:2019-05-03 16:12:53

标签: jenkins jenkins-pipeline jenkins-declarative-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'
            }
        }
    }
}

此构建作业失败,因为%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. 我是不是在做这种“艰难的方式”(对于Jenkins来说是新来的),有没有更简单的方法来完成我的工作?我想随着管道的变大,我将需要设置许多这些环境变量。

1 个答案:

答案 0 :(得分:0)

使用%%语法的变量扩展仅可用于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'
            }
        }
    }
}