34

I have written a simple script via PowerShell to gather some files and zip them into one folder, lets call it Script.ps1. I want to make the script run every time Jenkins does a new build, however I also would like the name of the zip file to be the BUILD_NUMBER.

How can I create a variable in PowerShell that is Jenkins's current build number? As of the moment I am calling the Script.ps1 in the execute shell section of configuration.

Gorgsenegger
  • 7,356
  • 4
  • 51
  • 89
Foster
  • 746
  • 2
  • 8
  • 14

3 Answers3

72

I'm not familiar with Jenkins, but I believe BUILD_NUMBER is an environment variable.

To access it in PowerShell, use $env:BUILD_NUMBER

E.g. If using 7-zip

7z.exe a "$env:BUILD_NUMBER.zip" C:\Build\Path 
Rynant
  • 23,153
  • 5
  • 57
  • 71
6

You can add arguments to your Script.ps1. Just use Param at the top of the script:

Param( $BuildNumber ) #must be first statement in script
# your current script goes here

then you can call the script passing BUILD_NUMBER as argument from Jenkins. Refer to this question for calling Powershell script with argument.

Community
  • 1
  • 1
tkokasih
  • 1,117
  • 9
  • 19
2

You could also use the Powershell Plugin, see here.

It allows you to enter PowerShell commands directly in a text box in Jenkins.

Then you can use the available environment variables (see docu of the plugin). The usage is a little cryptic:

(Get-Content ./Sources/SlmMachineControl/GUI/Project1.rc).replace("`"FileVersion`", `"1.0.0.0`"" ,"`"FileVersion`" `"2.3.$($env:BUILD_NUMBER).0`"") | Set-Content ./Sources/SlmMachineControl/GUI/Project1.rc

Also note the escaping of the double-quotes. Took me quite a while :)

anhoppe
  • 4,287
  • 3
  • 46
  • 58