Recently I have tasked myself to work on how we organize our Jenkins jobs code. We are using Jenkins declarative pipeline (officially that is "simplified and opinionated syntax on top of the Pipeline
sub-systems", but basically it is "nicely structured, but they forbid you to use mostly anything fancy in you Groovy code").
In our case we have lots (and it is slowly growing) of jobs which are running tests from different directories in a same way, but with different parameters. Also we have another set of jobs that are checking something and in some case they trigger the test jobs. This all loudly calls for sharing the code, so I wanted to take a look at how to do it.
Here is list of tabs I'm closing now once I'm done :)
- Shared libraries - Create another git repo with vars/ directory and share the code there
- Defining Declarative Pipelines - You can define function that implements whole declarative pipeline (you can also define custom step, but you can not define shared stage)
- Groovy Maps - If you want to pass lots of parameters to the function, passing a Map is very handy and easy to read - in Python equivalent would be passing a dict I think
- Cannot define variable in pipeline stage - In declarative pipeline you can not define variable directly, use this workaround
- sh: Shell Script - Jenkins help for sh step
- Catching both output and error code - spoiler: try { ... } catch ( ... ) { ... } is also forbidden in declarative pipeline, use script { ... } again
- when - this is way how to skip a stage in declarative pipeline based on something
- Writing file in groovy - This would probably work, but Jenkins was giving me lots of security related warnings
- Library variable in jenkins shared library - how to get variables from vars/someVariables.groovy in vars/someFunction.groovy
No comments:
Post a Comment