Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kuper-adrian/jenkins-shared-library-example
Example for a Jenkins shared library with unit tests
https://github.com/kuper-adrian/jenkins-shared-library-example
example-project jenkins jenkins-pipeline unit-test
Last synced: 4 months ago
JSON representation
Example for a Jenkins shared library with unit tests
- Host: GitHub
- URL: https://github.com/kuper-adrian/jenkins-shared-library-example
- Owner: kuper-adrian
- License: wtfpl
- Created: 2019-01-03T15:17:05.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-03-13T19:51:28.000Z (almost 6 years ago)
- Last Synced: 2024-09-30T18:05:51.502Z (4 months ago)
- Topics: example-project, jenkins, jenkins-pipeline, unit-test
- Language: Groovy
- Homepage:
- Size: 9.77 KB
- Stars: 51
- Watchers: 3
- Forks: 23
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Jenkins Shared Pipeline Example with Unit Tests
This repository contains a example shared library for Jenkins that can be unit tested with JUnit and Mockito. The general idea is:
1. Keep the custom steps inside `vars` as small as possible and without any logic. Instead do everything inside classes (inside `src`).
2. Create an interface `IStepExecutor` which declares methods for all required Jenkins steps (sh, bat, error, etc.). The classes call steps only through this interface.
3. Write unit tests for your classes like you normally would. Use your favorite mocking and dependency framework to mock IStepExecutor.This way you should be able to:
* Compile and execute your library/unit tests without Jenkins
* Test that your classes work as intended
* Test that Jenkins steps are called with the right parameters
* Test the behaviour of your code when a Jenkins step fails
* Build, test, run metrics and deploy your Jenkins Pipeline Library through Jenkins itselfI suggest the IntelliJ IDEA for the least painful Jenkins Shared Library development experience ;)
Head over to my [blog post](https://dev.to/kuperadrian/how-to-setup-a-unit-testable-jenkins-shared-pipeline-library-2e62) on dev.to if you want a tutorial/more detailed explanation.
## Example Jenkinsfile
```
// add the following line and replace necessary values if you are not loading the library implicitly
// @Library('my-library@master') _pipeline {
agent any
stages {
stage('build') {
steps {
ex_msbuild 'test'
}
}
}
}
```## License
Good ol' WTFPL