35

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.

3 Answers 3

74

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 
Sign up to request clarification or add additional context in comments.

2 Comments

I just tried this in Jenkins and it worked wonderfully. Thanx!
Thanks! I don't know why your solution is marked with the green checkmark. This is the real solution.
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.

1 Comment

can become cumbersome when you need to use more than one environment variable in the script
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 :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.