Powershell step in pipeline not executing

44 views
Skip to first unread message

Flavio Campana

unread,
Jan 18, 2018, 11:13:48 AM1/18/18
to Jenkins Users
I've got a strange problem with a declarative pipeline and powershell, it seems the script is never executed.
The script is in a managed file.
The pipeline:

pipeline {
    agent any
    triggers { 
        pollSCM('H * * * *') 
    }
    tools {
        msbuild "DefaultMSBuild"
    }
    environment {
        solution = 'Project.sln'
        project = 'Project\\Project.csproj'
        projectDir = 'Project\\Project'
        key = credentials('proget-testauto-apikey')
    }
    stages {
        stage('Build and test') {
            steps {
                echo "Getting version for ${env.projectDir}"
                configFileProvider([configFile(fileId: '52c08128-fa6f-4c79-8a43-ff4f6dd13dbd', variable: 'script')]) {
                    powershell script: "${script} -projecDir ${env.projectDir}"
                   script { res = powershell script: "${script} -projecDir ${env.projectDir}" 
                    echo "results: ${res}"
                    }
                }
                echo 'Restore nugets..'
                bat "%NUGET% restore Project.sln"
                echo "Building solution : Project.sln"
                script {
                    scanner = tool name: 'NewSonarScanner', type: 'hudson.plugins.sonar.MsBuildSQRunnerInstallation'
                    props = readProperties text: '${res}'
                    withSonarQubeEnv('DefaultSonart') {
                        bat "${scanner}\\SonarQube.Scanner.MSBuild.exe begin /k:Project /n:Project /v:${props.PackageVersion} /d:sonar.host.url=%SONAR_HOST_URL% /d:sonar.login=%SONAR_AUTH_TOKEN% /d:sonar.cs.nunit.reportsPaths=\"NTestResult.xml\" /d:sonar.cs.opencover.reportsPaths=\"opencover.xml\" /d:sonar.verbose=true"
                        bat "\"${tool name: 'DefaultMSBuild', type: 'msbuild'}\"\\msbuild.exe Project.sln /m /target:clean,build /p:Configuration=Release" //;VisualStudioVersion=12.0
                        nunitReturn = bat returnStatus: true, script: "%OPENCOVER% -output:\"%WORKSPACE%\\opencover.xml\" -returntargetcode -register:user -target:\"%NUNIT%\" -targetargs:\"%WORKSPACE%\\Project.Tests\\bin\\Release\\Project.Tests.dll -v --result=NTestResult.xml;format=nunit3 --work=%WORKSPACE%\" "
                        bat "${scanner}\\SonarQube.Scanner.MSBuild.exe end"
                    }
                }
            }
        }
        stage('Positive result') {
            when {
              expression { nunitReturn == 0 }
            }
            steps {
                echo "Archiving Project"
                archiveArtifacts artifacts: '**/bin/Release/**', onlyIfSuccessful: true
                bat "%NUGET% pack Project\\Project.csproj -Prop Configuration=Release"
                bat "%NUGET% push Project\\Project.${props.PackageVersion}.nupkg -s %REPOTESTAUTO% ${key}"
                
            }
        }
        stage('Negative result'){
            when {
              expression { nunitReturn > 0 }
            }
            steps {
                script { currentBuild.result = "UNSTABLE" }
            }
        }
    }
    post {

    failure {   
        mail(from: "au...@test.com", 
           to: "d...@test.com", 
           subject: "Error in ${env.JOB_NAME}", 
           body: "See at ${env.BUILD_URL}")
        }

    unstable {   
        mail(from: "au...@test.com", 
           to: "d...@test.com", 
           subject: "Test failed in ${env.JOB_NAME}", 
           body: "See at ${env.BUILD_URL}")
        }
    }
    options {
        // For example, we'd like to make sure we only keep 10 builds at a time, so
        // we don't fill up our storage!
        buildDiscarder(logRotator(numToKeepStr:'20'))
        timeout(time: 60, unit: 'MINUTES')
    }
}

the script:
param([Parameter(Mandatory=$true)][string]$projectDir)
$packageVersion = ''
Write-Output "Executed=True"
Write-Output "PropFile=:$env:WORKSPACE\\$projectDir\\Properties\\AssemblyInfo.cs'"
$content = Get-Content ($env:WORKSPACE + '\\'  + $projectDir + '\\Properties\\AssemblyInfo.cs');
foreach ($line in $content)
{
if ($line -match '\[assembly: AssemblyVersion\("((\d+)\.?(\d*)\.?(\d*)?[\.-]?([\d\*]*)?([a-zA-Z]*)?(\d*?))"\)\]')
{
Write-Output "AssemblyFullVersion=$($Matches[1])"
Write-Output "AssemblyVersion=$($Matches[2]).$($Matches[3]).$($Matches[4])"
Write-Output "AssemblyMajor=$($Matches[2])"
Write-Output "AssemblyMinor=$($Matches[3])"
Write-Output "AssemblyRevision=$($Matches[4])"
Write-Output "AssemblyBuild=$($Matches[5])"
Write-Output "AssemblySuffix=$($Matches[6])"
Write-Output "AssemblyNumber=$($Matches[7])"

if ($packageVersion -eq ''){
$packageVersion = $Matches[1];
}
}
elseif ($line -match '\[assembly: AssemblyInformationalVersion\("((\d+)\.?(\d*)\.?(\d*)?[\.-]?([\d\*]*)?([a-zA-Z]*)?(\d*?))"\)\]')
{
Write-Output "AssemblyInfoFullVersion=$($Matches[1])"
Write-Output "AssemblyInfoVersion=$($Matches[2]).$($Matches[3]).$($Matches[4])"
Write-Output "AssemblyInfoMajor=$($Matches[2])"
Write-Output "AssemblyInfoMinor=$($Matches[3])"
Write-Output "AssemblyInfoRevision=$($Matches[4])"
Write-Output "AssemblyInfoBuild=$($Matches[5])"
Write-Output "AssemblyInfoSuffix=$($Matches[6])"
Write-Output "AssemblyInfoNumber=$($Matches[7])"
$packageVersion = $Matches[1]
}
}

Write-Output "PackageVersion=' + $packageVersion"

This is wha i get in the logs
Pipeline] configFileProvider
provisioning config files...
copy managed file [PSReadVersionNew] to file:/R:/jenkins/workspace/Libraries/Project@2@tmp/config8563286677318369656tmp
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] powershell
[SigningLibrary@2] Running PowerShell script
[Pipeline] echo
results: 
[Pipeline] }
[Pipeline] // script
[Pipeline] }
Deleting 1 temporary files
[Pipeline] // configFileProvider
[Pipeline] echo
Restore nugets..
[Pipeline] bat
[SigningLibrary@2] Running batch script

Slide

unread,
Jan 18, 2018, 11:33:11 AM1/18/18
to jenkins...@googlegroups.com
You may want to try naming the variable something else besides "script" which is a pipeline variable (you use it right below). I might recommand PSSCRIPT, or something similar.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/e693d2e9-759f-4a48-9a7b-0db5d341e65e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Flavio Campana

unread,
Jan 18, 2018, 11:41:46 AM1/18/18
to Jenkins Users
I tried right now but nothing changed

Björn Pedersen

unread,
Jan 18, 2018, 11:53:41 AM1/18/18
to Jenkins Users
Hi,

I have no experience with declarative pipelines,
but  configFileProvider is a standalone step , not taking a body like you use it (it's not a withConfigFile...)

try:

               echo "Getting version for ${env.projectDir}"
               configFileProvider([configFile(fileId: '52c08128-fa6f-4c79-8a43-ff4f6dd13dbd', variable: 'MYSCRIPT')])
               powershell script: "${MYSCRIPT} -projecDir ${env.projectDir}"
               script { res = powershell script: "${MYSCRIPT} -projecDir ${env.projectDir}"
                    echo "results: ${res}"
               }




Björn

Flavio Campana

unread,
Jan 18, 2018, 12:00:59 PM1/18/18
to Jenkins Users
I got i straight from the pipeline syntax page (and some online example) and i dont get syntax errors
Reply all
Reply to author
Forward
0 new messages