如何在Jenkinsfile中将Powershell变量传递给Groovy变量

时间:2019-06-06 13:12:09

标签: powershell jenkins jenkins-pipeline

我正在用Jenkinsfile编写管道。我有问题。如何通过powershell将powershell变量传递给groovy变量,或者如何在Jenkinsfile中的groovy中处理文件?

Cos(F) = (a1^2 - a2^2 - b1^2)/ (2*a2*b1) 
c^2 = a2^2 + b2^2 + (a1^2 - a2^2 + b1^2) * b2 / b1 

我的solutions.json文件:

     stage('GETTING SLN FILES') {
       steps {
           script {
               powershell """$dirs_with_sln = Get-ChildItem -Path . -Recurse *.sln | Select-Object -Property Directory -Unique
                             $slns = @()
                             foreach($dir in $dirs_with_sln) {
                               $dir = $dir.Directory
                               $FileExists = Test-Path -Path "$dir\\default.ps1"
                               if ($FileExists -eq $true) {
                                   $slns += $(Get-ChildItem -Path $dir -Filter *.sln).FullName
                               }
                             }
                          """
    }

1 个答案:

答案 0 :(得分:1)

这是给你的例子。

您的问题是out-file使用系统默认编码输出,而不是ASCII

当管道读取文本文件时,它无法将前1个或2个字节识别为ASCII字符。因此,报告错误。

因此,您可以简单地在ASCII中强制输出json文件或使用正确的编码读取json文件内容。

import groovy.json.JsonSlurper
pipeline {
  agent any
  stages {
    stage('1') {
      steps {
        script {
          powershell (script: "ls C:\\ | select Name, FullName | ConvertTo-Json | out-file C:\\Temp\\1.json -encoding ascii")
          def t = readFile("C:\\Temp\\1.json")
          def tt = (new JsonSlurper()).parseText(t)
          println tt
        }
      }
    }
  }
}
相关问题