forked from elijb/jb-jenkins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-groovy-shared-library.txt
51 lines (35 loc) · 1.72 KB
/
05-groovy-shared-library.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Documantation: https://jenkins.io/doc/book/pipeline/shared-libraries/
Example: https://github.com/monodot/pipeline-library-demo
1. create a directory called vars under specific GIT repo.
This will hold your custom steps.
Each of them will be a different .groovy file underneath your vars directory, e.g.:
vars/
deployApplication.groovy
parseFile.groovy
sayHello.groovy
readSystemCredentials.groovy
doCodeReview.groovy
Each of your custom steps is a different .groovy file inside your vars/ directory.
In Jenkins terminology, these are called Global Variables, which is why they are located inside vars/
2. Add a custom step
Create a file for your custom step, and fill in the code. For example, a simple greeting function would look like this:
#!/usr/bin/env groovy
def call(String name = 'human') {
echo "Hello, ${name}."
}
3. Set up the library in Jenkins
In Jenkins, go to Manage Jenkins → Configure System. Under Global Pipeline Libraries
add a library with the following settings:
Name: pipeline-library-demo
Default version: Specify a Git reference (branch or commit SHA), e.g. master
Retrieval method: Modern SCM
Select the Git type
Project repository: https://github.com/monodot/pipeline-library-demo.git
4. Use the library in a pipeline
To use the shared library in a pipeline, you just add @Library('your-library-name') to the top of your pipeline definition, or Jenkinsfile. Then call your step by name, e.g. sayHello
@Library('pipeline-library-demo')_
stage('Demo') {
echo 'Hello world'
sayHello 'Dave'
}
NOTE: The underscore (_) is not a typo! You need this underscore if the line immediately after the @Library annotation is not an import statement.