从jenkins文件中的powershell脚本检索值

问题描述 投票:0回答:1

我尝试从powershell脚本中检索一个字符串,并在Jenkins文件中使用它,以便更改SonarQube中显示的版本。

我开始实现从package.json获取项目版本的功能。基本上,我将Workspace目录作为参数,如果他在package.json本身或子文件夹中找到了Workspace,则询问完整路径。如果找到该文件,则对其进行解析并返回版本:

updateDisplayedVersionInSonar.ps1

param (
  [Parameter(Mandatory = $true)]
  [string]$Workspace
)
try{

    $packageFullPath = ""
    $pcgVersion = ""

    Get-ChildItem -Path ${Workspace} -Filter package.json
     -Recurse -ErrorAction SilentlyContinue -Force | % {$packageFullPath = $_.FullName}
    try { 

      Test-Path $packageFullPath -PathType leaf

      $json = Get-Content $packageFullPath | Out-String | ConvertFrom-Json

      if($json.PSobject.Properties.Name -contains "version"){       
        $pcgVersion =  $json.version
      }
      else{
        $pcgVersion = "unknown"
      }

      Write-Output $pcgVersion          
  }
  catch {
    Write-Output "There is no package.json file!"
  }
}
catch{
  $ErrorMessage = $_.Exception.Message
  write-host "An error has occured: ${ErrorMessage}"
  exit 1
}

现在,我想使用Jenkins文件中ps脚本返回的版本:

stage('SonarQube Frontend') {
environment {
    sonarqubeScannerHome = tool name: 'SonarQube Scanner', type: hudson.plugins.sonar.SonarRunnerInstallation'
    sonarQubeId = 'SonarQubeServer'
    sonarProjectName = "\"SPACE ${REPOSITORY_NAME}\""
    sonarProjectKey = "${REPOSITORY_NAME}"
    testsPaths = 'app/myProject-ui/webapp/TEST/unit/utils'
    testExecutionReportPaths = 'app/myProject-ui/reports/sonar/TESTS-qunit.xml'
    javascriptLcovReportPaths = 'app/myProject-ui/reports/coverage/lcov.info'
}
steps {

    withSonarQubeEnv(env.sonarQubeId) {     
        withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: sonarQubeId, usernameVariable: 'SONAR_USER', passwordVariable: 'SONAR_PASSWORD']]) {   
            script{
                sonarProperties =  " -Dsonar.projectName=${env.sonarProjectName}" +
                    " -Dsonar.projectKey=${env.sonarProjectKey}" +   
                    " -Dsonar.login=${SONAR_USER}" +
                    " -Dsonar.password=${SONAR_PASSWORD}" +
                    " -Dsonar.sources=./" +  
                    " -Dsonar.exclusions=**/*.java"
                //some other conditions

                //this line will be executed and i will see in Jenkins Console output the version found in package.json
                powershell "powershell -File C:/Automation/updateDisplayedVersionInSonar.ps1 -Workspace '${env.WORKSPACE}/app'"

                //I try to make it as a variable, but it will print "echo version here - null -" :(
                pcgVersion = powershell "powershell -File C:/Automation/updateDisplayedVersionInSonar.ps1 -Workspace '${env.WORKSPACE}/app'" 
                echo "echo version here - ${pcgVersion} -"

                //I want to use it here in order to be displayed the correct version of the app in Sonar
                sonarProperties = sonarProperties + " -Dsonar.projectVersion= ${pcgVersion}" 
            }

        bat "${env.sonarqubeScannerHome}/bin/sonar-scanner" + " -Dsonar.host.url=${SONAR_HOST_URL}" + sonarProperties
        }   

    } 
}
} //end of SonarQube Frontend stage

我尝试了How to execute powershell script from jenkins by passing parameters的解决方案,但没有任何结果。

我也尝试过这种方式:

version = powershell(returnStdout: true, script:'powershell -File C:/SCP/Automation/updateDisplayedVersionInSonar.ps1 -Workspace "${env.WORKSPACE}"')


version = powershell(returnStdout: true, script:'C:/SCP/Automation/updateDisplayedVersionInSonar.ps1 -Workspace "${env.WORKSPACE}"')

我发现了很多有关如何在Powershell中使用Jenkins变量的示例,反之亦然:(]

我做错了什么?有可能实现这一目标吗?

谢谢!

我尝试从Powershell脚本中检索字符串,并在Jenkins文件中使用它,以便更改SonarQube中显示的版本。我开始实现用于项目的功能...

powershell jenkins jenkins-groovy
1个回答
0
投票

您可以改用Jenkins Pipeline Utility

© www.soinside.com 2019 - 2024. All rights reserved.