Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I share scripts across Jenkins jobs?

Tags:

jenkins

I'm setting up Jenkins to do several CI jobs. Setting up the second job, I find myself copying the contents of 'Execute shell' build steps from the first job, verbatim to the second job (because I've already used parameters wherever I can, so it gets ${project} ${version} from source control and project and version are set in the jobs environment using the Environment Injector Plugin).

I don't like this copying (I've already had to go back and copy over fixes).

How can I share these scripts (or even better: parts of scripts) within Jenkins?

like image 414
Legolas Avatar asked May 20 '14 15:05

Legolas


3 Answers

I found a way inside Jenkins after all: the Template Project Plugin which does what I want, although in a slightly strange way (create a disabled project per build step you want to share, then in each project you want to use it, use the new 'Use all builders from other project' builder.

For anyone looking for more options, there's a template plugin that looks much better in Cloudbees commercial offering (but no price indication anywhere), a Managed Script Plugin that looks like it was what I wanted and an ezTemplates if you care to build it yourself. I haven't tried either one.

like image 126
Legolas Avatar answered Oct 21 '22 04:10

Legolas


You can put the contents of 'Execute' shell on script and place the script on a common location which is accessible by the slaves. in 'Execute Shell', you just need to call this script with appropriate arguments.

like image 33
GeeDee Avatar answered Oct 21 '22 04:10

GeeDee


You would need to use scripts to share your scripts (or parts of scripts) with other jenkins jobs. A better idea would be to not type the content of the shell script inside the build job and just use source control. You can call the script with each project from source control or use the copy artifacts plugin, and in the execute shell build step call the script.

If you can't do that then you would have to write a script that looks for the newly generated shell script in your TEMP directory, and copy it to the workspace of your next job. That just sounds more of a waste of time than copying and pasting though.

With source control, if you make one change to the script, it is changed everywhere.
Practice safe scripting!

like image 44
Cole9350 Avatar answered Oct 21 '22 04:10

Cole9350