我尝试从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变量的示例,反之亦然:(
我做错了什么?有可能实现这一目标吗?
谢谢!
答案 0 :(得分:1)
您可以改用Jenkins Pipeline Utility。
def getPackageVersion() {
def package = readJSON file: '${env.WORKSPACE}/app/package.json'
echo package.version
return package.version
}
那么您应该可以像这样访问它。
def pcgVersion = getPackageVersion()