Powershell step in pipeline not executing

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Powershell step in pipeline not executing

Flavio Campana
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: "[hidden email]", 
           to: "[hidden email]", 
           subject: "Error in ${env.JOB_NAME}", 
           body: "See at ${env.BUILD_URL}")
        }

    unstable {   
        mail(from: "[hidden email]", 
           to: "[hidden email]", 
           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

--
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 [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: Powershell step in pipeline not executing

slide
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.

On Thu, Jan 18, 2018 at 9:14 AM Flavio Campana <[hidden email]> wrote:
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: "[hidden email]", 
           to: "[hidden email]", 
           subject: "Error in ${env.JOB_NAME}", 
           body: "See at ${env.BUILD_URL}")
        }

    unstable {   
        mail(from: "[hidden email]", 
           to: "[hidden email]", 
           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

--
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 [hidden email].
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.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAPiUgVe5bs0rBb-x%2Bt0nEpjngdU6nWN-XKT4m3Q6M91qtLbTUA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Powershell step in pipeline not executing

Flavio Campana
I tried right now but nothing changed

Il giorno giovedì 18 gennaio 2018 17:33:11 UTC+1, slide ha scritto:
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/46735d94-2e06-40f7-8f9b-8f67d16e4263%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Powershell step in pipeline not executing

Björn Pedersen
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

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/adef1318-236c-4dd5-972b-dba1e0726a93%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Powershell step in pipeline not executing

Flavio Campana
I got i straight from the pipeline syntax page (and some online example) and i dont get syntax errors

Il giorno giovedì 18 gennaio 2018 17:53:41 UTC+1, Björn Pedersen ha scritto:
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

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/e16abc89-8577-41be-8a52-2f9149563694%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.