简体   繁体   中英

How to pass powershell variable to groovy variable in Jenkinsfile

I am writing pipeline in Jenkinsfile. And I have the problem. How can I pass powershell variable to groovy variable, or how can I manipulate with files within groovy in Jenkinsfile as I do via powershell?

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

My solutions.json file :

[
    "D:\\ws\\workspace\\msbuild-test\\src\\DataProcessors\\AlertDelivery\\AlertDelivery.sln",
    "D:\\ws\\workspace\\msbuild-test\\src\\DataProcessors\\BusinessWireNewsProcessor\\BusinessWireNewsProcessor.sln",
    "D:\\ws\\workspace\\msbuild-test\\src\\DataProcessors\\ComponentMesosExecutor\\ComponentMesosExecutor.sln",
    "D:\\ws\\workspace\\msbuild-test\\src\\DataProcessors\\Crawling\\Core\\Agent\\CrawlingAgent.sln",
    "D:\\ws\\workspace\\msbuild-test\\src\\DataProcessors\\Crawling\\Crawlers\\Crawlers.Base\\_Crawlers.Base.sln",
    "D:\\ws\\workspace\\msbuild-test\\src\\DataProcessors\\Crawling\\Crawlers\\Crawlers.Custom\\_Crawlers.Custom.sln",
    "D:\\ws\\workspace\\msbuild-test\\src\\DataProcessors\\Crawling\\Crawlers\\Crawlers.Internal\\_Crawlers.Internal.sln",
    "D:\\ws\\workspace\\msbuild-test\\src\\DataProcessors\\Crawling\\Crawlers\\Crawlers.Reporting\\_Crawlers.Reporting.sln",
    "D:\\ws\\workspace\\msbuild-test\\src\\DataProcessors\\DocumentsMerge\\VADocumentsMerge\\VADocumentsMerge.sln",
    "D:\\ws\\workspace\\msbuild-test\\src\\DataProcessors\\Downloading\\DownloadProcessor.sln",
    "D:\\ws\\workspace\\msbuild-test\\src\\DataProcessors\\LogCollector\\LogCollector.sln",
    "D:\\ws\\workspace\\msbuild-test\\src\\DataProcessors\\MailProcessor\\MailProcessor.sln",
    "D:\\ws\\workspace\\msbuild-test\\src\\DataProcessors\\MailProcessor\\ProcessingAlgorithms\\CourthouseNewsAlgorithm\\InfoNgen.MailProcessor.CourthouseNewsAlgorithm.sln",
    "D:\\ws\\workspace\\msbuild-test\\src\\DataProcessors\\MailProcessor\\ProcessingAlgorithms\\DefaultProcessingAlgorithms\\InfoNgen.MailProcessor.DefaultProcessingAlgorithms.sln",
    "D:\\ws\\workspace\\msbuild-test\\src\\DataProcessors\\MailProcessor\\ProcessingAlgorithms\\HtmlNewsletterAlgorithm\\HtmlNewsletterAlgorithm.sln",
    "D:\\ws\\workspace\\msbuild-test\\src\\DataProcessors\\ProcessingUnit\\ProcessingUnit.sln",
    "D:\\ws\\workspace\\msbuild-test\\src\\DataProcessors\\SharePointDataLoader\\SharePointDataLoader.sln"
]


Here is an example for you.

Your problem is out-file output with system default encoding, which is not ASCII

When pipeline read the text file, it's not able to recognize the first 1 or 2 bytes as ASCII characters. So, report the error.

So you can simply force output the json file in ASCII or read the json file content use the proper encoding.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM