https://github.com/alexanderbazhenoff/jenkins-shared-library
Jenkins shared library.
https://github.com/alexanderbazhenoff/jenkins-shared-library
jenkins library pipeline
Last synced: 6 months ago
JSON representation
Jenkins shared library.
- Host: GitHub
- URL: https://github.com/alexanderbazhenoff/jenkins-shared-library
- Owner: alexanderbazhenoff
- License: apache-2.0
- Created: 2023-03-02T20:22:57.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-22T15:38:35.000Z (over 1 year ago)
- Last Synced: 2025-01-30T20:54:21.933Z (8 months ago)
- Topics: jenkins, library, pipeline
- Language: Groovy
- Homepage:
- Size: 78.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jenkins shared library
Frequently used code patterns that makes writing Jenkins pipelines easier.


[](https://makeapullrequest.com)## Usage
1. Connect this repository to your Jenkins shared library as described in
[`official documentation`](https://www.jenkins.io/doc/book/pipeline/shared-libraries/#global-shared-libraries).
2. Write your Jenkins pipeline like:**To use library from** [`src/org`](src/org/alx):
```groovy
#!/usr/bin/env groovy
@Library('jenkins-shared-library')
node('master') {
CommonFunctions = new org.alx.commonFunctions() as Object
// your pipeline code and call this object by 'CommonFunctions.', e.g:
CommonFunctions.cleanSshHostsFingerprints(env.IP_LIST.tokenize(' '))
// where IP_LIST is a space separated string IP list which is defined by pipeline
// parameter IP_LIST
}
```To use GitLab related functions (e.g. runAnsible) you should set GitCredentialsID variable, which can be defined in
`OrgAlxGlobals()` class:```groovy
@Library('jenkins-shared-library')
node('master') {
// CommonFunctions assignment and cast should be placed before GlobalConstants
CommonFunctions = new org.alx.commonFunctions() as Object
GlobalConstants = new org.alx.OrgAlxGlobals() as Object
// Then use constants from OrgAlxGlobals
}
```**To use library from** [`vars`](vars):
```groovy
@Library('jenkins-shared-library')
// or @Library(['jenkins-shared-library', 'another-library']) _
// if you need to use several libraries.
node('master') {
// your pipeline code then example usage of unstashParameter:
String fileInWorkspace = unstashParameter "JENKINS_PIPELINE_FILE_PARAMETER_NAME"
// Output file content
sh String.format('cat %s', fileInWorkspace)
}
```Please also keep in mind a differences between `vars` and `src` folder organizations
(read [**this article**](http://tdongsi.github.io/blog/2017/12/26/class-in-jenkins-shared-library/) for details).3. Read `Groovydoc` in the comments of file(s) (e.g:
[`src/org/alx/commonFunctions.groovy`](src/org/alx/commonFunctions.groovy)) for details.
4. (Optional) To use Git-related functions (e.g. `cloneGitToFolder()`, `installAnsibleGalaxyCollections()` or
`runAnsible()`) to clone from ssh URL you should set up **GitCredentialsID** variable in
[`commonFunctions.groovy`](src/org/alx/commonFunctions.groovy).