Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create resusable jenkins pipeline script

I am considering to use Jenkins pipeline script recently, one question is that I don't figure out a smart to way to create internal reusable utils code, imagine, I have a common function helloworld which will be used by lots of pipeline jobs, so I hope to create a utils.jar can injected it into the job classpath.

I notice Jenkins have a similar concept with the global library, but my concern regarding this plugin:

Since it is a plugin, so we need to install/upgrade it through jenkins plugin manager, then it may require reboot to apply the change, this is not what I want to see since utils may change, add always, we hope it could be available immediately.

Secondly, it is official jenkins shared lib, I dont want to (Or they will not apply us) put private code into jenkins repo.

Any good idea?

like image 939
Tim Avatar asked Aug 01 '16 09:08

Tim


People also ask

How do you make a pipeline Jenkins script?

To create a simple pipeline from the Jenkins interface, perform the following steps: 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 create a Jenkins Groovy script?

Usage. To create Groovy-based project, add new free-style project and select "Execute Groovy script" in the Build section, select previously configured Groovy installation and then type your command, or specify your script file name. In the second case path taken is relatively from the project workspace directory.

Can I mix declarative and scripted pipeline?

Yes you can only if you want to have external function inside step block.


1 Answers

The Shared Libraries (docs) allows you to make your code accessible to all your pipeline scripts. You don't have to build a plugin for that and you don't have to restart Jenkins.

E.g. this is my library and this a Jenkinsfile that calls this common function.


EDIT (Feb 2017): The library can be accessed through Jenkins' internal Git server, or deployed through other means (e.g. via Chef) to the workflow-lib/ directory within the jenkins user's home directory. (still possible, but very unhandy).

The global library can be configured through the following means:

  • an @Library('github.com/...') annotation in the Jenkinsfile pointing to the URL of the shared library repo.
  • configured on the folder level of Jenkins jobs.
  • configured in Jenkins configuration as global library, with the advantage that the code is trusted, i.e., not subject to script security.

A mix of the first and last method would be a not explicitly loaded shared library that is then requested only using its name in the Jenkinsfile: @Library('mysharedlib').

like image 154
StephenKing Avatar answered Nov 07 '22 03:11

StephenKing