Jenkins Powershell 写入控制台

Lob*_*nts 6 powershell jenkins jenkins-pipeline

我有一个调用 powershell 文件的 jenkins 工作。当我在自由式项目中使用它时,它会在控制台输出中显示 powershell 执行。将其切换到管道作业后,我不再看到输出。

目前我的管道如下所示:

 pipeline 
 {
    stages
    {
        stage ('Deploy To Dev')
         {
            steps
             {
                powershell '"%WORKSPACE%\\SpearsLoad\\Scripts\\CIDeployToDev.Ps1"'
             }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但我没有记录 powershell 步骤。

根据文档,我尝试将阶段更改为:

pipeline 
{
    stages
    {
        stage ('Deploy To Dev')
            {
            steps
            {
                node('Deploy the SSIS load')
                {
                    //Deploy the SSIS load
                    def msg = powershell(returnStdout: true, script: '"%WORKSPACE%\\SpearsLoad\\Scripts\\CIDeployToDev.Ps1"')
                    println msg
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但这给出了:

预期为第 123 行第 6 列的步骤。 def msg = powershell(returnStdout: true, script: '"%WORKSPACE%\SpearsLoad\Scripts\CIDeployToDev.Ps1"')

我觉得我错过了一些非常基本的东西。我究竟做错了什么 ?

bir*_*230 5

您需要将管道执行包装到script部分中,因为您尝试在声明性管道中使用脚本语法:

script {
    //Deploy the SSIS load
    def msg = powershell(returnStdout: true, script: '"%WORKSPACE%\\SpearsLoad\\Scripts\\CIDeployToDev.Ps1"')
    println msg
}
Run Code Online (Sandbox Code Playgroud)


Lob*_*nts 5

对于任何来这里寻找答案的人,我需要指出,代码实际上存在 3 个独立的问题:

  1. 根据接受的答案,缺少脚本部分。
  2. 由于未正确调用,powershell 并未实际运行
  3. 由于 %WORKSPACE% 变量中存在空格,powershell 未运行

最后,我选择了:

script {
    def msg = powershell(returnStdout: true, script: " & './SpearsLoad\\Scripts\\CIDeployToDev.Ps1'")
    println msg
}  
Run Code Online (Sandbox Code Playgroud)

这确实有效!