Chetan D C
Chetan D C

Reputation: 23

How to test Jenkinsfile

I am trying to write the test cases to validate the Jenkinsfile, But the load script function not working expecting the extension to be provided and throwing ResourceException exception loadScript("Jenkinsfile")

Is their better way to test the Jenkinsfile

Upvotes: 2

Views: 8471

Answers (2)

SegFault
SegFault

Reputation: 2190

You can validate your Declarative Pipeline locally thanks to Jenkins built-in features.This can be done using a Jenkins CLI command or by making an HTTP POST request with appropriate parameters.

The command is the following:

curl -s -X POST -F "jenkinsfile=<YourJenkinsfile" \
https://user:[email protected]/pipeline-model-converter/validate

For a practical example follow this guide: https://pillsfromtheweb.blogspot.com/2020/10/validate-jenkinsfile.html

Upvotes: 1

FCh
FCh

Reputation: 677

The problem is that there are not enough tools for the development of pipelines. Pipelines is DSL and it imposes a restrictions.

There is an interesting approach to using flags. For example, test which defines outside pipeline(in job). If test=true, a pipeline change some "production" logic to "test" - select another agent, load artifacts into another repository, run another command and so on.

But recently appeared Pipeline Unit Testing Framework. It allows you to unit test Pipelines and Shared Libraries before running them in full. It provides a mock execution environment where real Pipeline steps are replaced with mock objects that you can use to check for expected behavior.

Useful links:

Upvotes: 3

Related Questions