Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Powershell write to console

I have a jenkins job that calls a powershell file. When I use it from a freestyle project it shows the powershell execution in the console output. Having switched it to a pipeline job I no longer see the output.

Currently my pipeline looks like this:

 pipeline 
 {
    stages
    {
        stage ('Deploy To Dev')
         {
            steps
             {
                powershell '"%WORKSPACE%\\SpearsLoad\\Scripts\\CIDeployToDev.Ps1"'
             }
        }
    }
}

but I get no logging of the powershell steps.

Following the documentation I tried changing the stage to:

pipeline 
{
    stages
    {
        stage ('Deploy To Dev')
            {
            steps
            {
                node('Deploy the SSIS load')
                {
                    //Deploy the SSIS load
                    def msg = powershell(returnStdout: true, script: '"%WORKSPACE%\\SpearsLoad\\Scripts\\CIDeployToDev.Ps1"')
                    println msg
                }
            }
        }
    }
}

but that gives:

Expected a step @ line 123, column 6. def msg = powershell(returnStdout: true, script: '"%WORKSPACE%\SpearsLoad\Scripts\CIDeployToDev.Ps1"')

I feel like I am missing something quite fundamental. What am I doing wrong ?

like image 673
Lobsterpants Avatar asked Feb 01 '19 09:02

Lobsterpants


2 Answers

You need to wrap your pipeline execution into script section, because you're trying to use scripted syntax in declarative pipeline:

script {
    //Deploy the SSIS load
    def msg = powershell(returnStdout: true, script: '"%WORKSPACE%\\SpearsLoad\\Scripts\\CIDeployToDev.Ps1"')
    println msg
}
like image 67
biruk1230 Avatar answered Sep 17 '22 13:09

biruk1230


For anyone who comes here looking for answers, I need to point out that there were actually 3 separate problems with the code:

  1. The lack of a script section as per the accepted answer.
  2. The powershell was not actually running due to not calling it correctly
  3. The powershell was not running due to spaces in the %WORKSPACE% variable

in the end, I went with:

script {
    def msg = powershell(returnStdout: true, script: " & './SpearsLoad\\Scripts\\CIDeployToDev.Ps1'")
    println msg
}  

which actually works!

like image 40
Lobsterpants Avatar answered Sep 19 '22 13:09

Lobsterpants