Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Powershell script from file in Jenkins pipeline

I am trying to run a Powershell script from a file in a Jenkins pipeline.

I have read this which shows how to run powershell scripts that are entered into the pipeline, but I can't get a script to run from a file.

I have tried:

powershell returnStatus: true, script: '-File build.ps1'

and various combinations but can't figure out how to do this.

like image 598
bic Avatar asked Oct 13 '17 09:10

bic


People also ask

How do I run a script in Jenkins pipeline?

Click New Item on your Jenkins home page, enter a name for your (pipeline) job, select Pipeline, and click OK. In the Script text area of the configuration screen, enter your pipeline syntax.

How do I run PowerShell as admin in Jenkins?

msc and right click on Jenkins. Then click Properties , go to Logon tab, check mark This account and enter username and password which has admin privileges. Stop and Start Jenkins service. From now on when you will run powershell commands in Jenkins, they will be run as administrator.


2 Answers

Figured it out soon after, the line should have read

powershell returnStatus: true, script: '.\\build.ps1'
like image 97
bic Avatar answered Oct 07 '22 18:10

bic


@bic's answer works great, and it helped me a lot. If you need to pass variables in and you don't want to use environment vars inside the script, the best way I've found is a two-liner (declarative syntax):

writeFile file: 'tempFile.ps1', text: "${libraryResource 'psScript1.ps1'}"
powershell './tempFile.ps1 -someArg $env:someArg'

So you're writing a temp file with the content of the script to the local dir. Then you're calling the script directly with powershell, and injecting the vars in the call.

I agree it's more convenient to use environment vars in your scripts. There are a few patterns you have to keep in mind:

  1. Env vars set in the top-level environment block can't be changed during the run. (At least that is my experience.)
  2. Env vars can only be Strings. There are tricks you can play to get maps to work, but they're tricks.
  3. If you want to set an env var that's changeable throughout the pipeline's scope, you have to do it in a script block in a step. I usually set them all up in my ('init') stage. You should then be able to use and modify these vars anywhere in the pipe - and any sub-scripts that are spawned off of it. If you have a lot of env vars, i'd recommend shunting them off to an external groovy library.
    pipeline {
       ...
       stage('init') {
         steps {
           script {
             env.myVariable = 'some value'
           }
         }
       }
      ...
    
  4. You can also set env vars scoped only to that stage. In my experience, these are not evaluated before entering the actual steps block - ie, they will not be set during when condition evaluations. Again, just my experience, i might be wrong.
    stage('A') {
      environment {
        stageEnvVar = 'another value'
      }
      steps {
        ...
      }
    }
    

This is the best reference I've found on Jenkins env vars: https://e.printstacktrace.blog/jenkins-pipeline-environment-variables-the-definitive-guide/

like image 3
Max Cascone Avatar answered Oct 07 '22 19:10

Max Cascone