How can I share scripts through Jenkins work?

I am creating Jenkins to complete several CI assignments. When I create the second task, I find that I am copying the contents of the “Executing the shell” execution steps from the first task, verbatim to the second task (because I already used the parameters wherever I can, so it gets $ {project} $ {version} from the control source , and the project and version are installed in the task environment using the injector environment plug-in).

I do not like this copying (I already had to go back and copy corrections).

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

+7
jenkins
source share
3 answers

I found a way inside Jenkins: Template Project Plugin that does what I want, albeit in a slightly strange way (create a disconnected project for every build step you want to split, then in every project you want to use, use the new constructor "Use all builders from another project."

For those who are looking for more options, there is a template plugin that looks much better in the Cloudbees commercial offer (but without specifying a price anywhere), a Managed Script plugin that looks the way I wanted, and ezTemplates if you want it yourself create. I have not tried any.

+4
source share

You can put the contents of the Run shell on the script and put the script in a shared folder accessible to slaves. in the "Execute Shell" you just need to call this script with the appropriate arguments.

+2
source share

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

If you cannot do this, you will have to write a script that looks for the newly created script shell in your TEMP directory and copy it into the workspace of your next job. It sounds more like a waste of time than copying and pasting.

With source control, if you make one change to the script, it will be changed everywhere.
Practice a safe script!

+1
source share