在Jenkins管道中的Windows Powershell,无法获取Get-Content -wait的输出

时间:2018-01-29 02:51:18

标签: windows powershell jenkins

我有一个运行Windows的Jenkins节点,我想" tail" jenkins输出的文件,我似乎无法让它工作。

所以,在管道中,我并行运行两个分支,一个是做一些ping并写入一个文件,另一个应该读出同一个文件并写入输出内容,但我看不到任何内容记录器线程。

以下是管道脚本:

pipeline {
  agent none
  stages {
    stage('Stage A') {
      agent {
        label "winvm01"
      }
      steps {
            script {
                stage('Stage 1') {        
                 parallel (
                     "worker_thread" : { 
                         bat "ping google.com /n 15 > C:\\Jenkins\\testout.txt" 
                         echo "worker ended"
                     },

                     "logger_thread" : {
                         echo "will call powershell"
                         powershell script: '''
                            Write-Output "starting get content"
                            Get-Content "C:\\Jenkins\\testout.txt" -tail 1 -wait | % { Write-Output "## $(Get-Date -Format "hh:mm:ss") $_" }
                            exit 0
                         '''                         

                         echo "logger ended"
                     }
                   )
                }   
            }
      }
    }
  }
}

这将在Jenkins中产生以下输出:

Running in Durability level: MAX_SURVIVABILITY
[Pipeline] stage
[Pipeline] { (Stage A)
[Pipeline] node
Running on WinVM01 in C:\Jenkins\workspace\StackoverflowExample
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Stage 1)
[Pipeline] parallel
[Pipeline] [worker_thread] { (Branch: worker_thread)
[Pipeline] [logger_thread] { (Branch: logger_thread)
[Pipeline] [worker_thread] bat
[worker_thread] [StackoverflowExample] Running batch script
[Pipeline] [logger_thread] echo
[logger_thread] will call powershell
[Pipeline] [logger_thread] powershell
[worker_thread] 
[worker_thread] C:\Jenkins\workspace\StackoverflowExample>ping google.com /n 15  1>C:\Jenkins\testout.txt 
[logger_thread] [StackoverflowExample] Running PowerShell script
[Pipeline] [worker_thread] echo
[worker_thread] worker ended
[Pipeline] [worker_thread] }

然后继续运行直到我中止工作。

当我直接从节点上的powershell控制台中的记录器线程运行该行,然后启动构建时,我可以看到ping命令的结果行,所以命令本身应该没问题。如果我在Get-Content调用中省略了-wait,则文件的当前内容会找到Jenkins的方式并退出该线程。我似乎无法获得Get-Content的输出 - 等待Jenkins。

有人请指出我做错了什么!

0 个答案:

没有答案