Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins: How do I lint Jenkins pipelines from the command line?

I would like to be able to perform linting on Jenkins pipelines and it seems that Groovy linting is not enough.

How can I do this?

like image 317
sorin Avatar asked Jun 22 '17 14:06

sorin


People also ask

What is Linting in Jenkins?

Command-line Pipeline Linter Jenkins can validate, or "lint", a Declarative Pipeline from the command line before actually running it. This can be done using a Jenkins CLI command or by making an HTTP POST request with appropriate parameters. We recommended using the SSH interface to run the linter.

How can I see pipeline in Jenkins?

Step 4) Go to your Jenkins dashboard and create a view by clicking on the “+” button. Select the Build Pipeline View option and click OK. Step 5) Under Pipeline view configuration, locate Pipeline Flow. Under Pipeline flow, select the initial job to run.


2 Answers

HTTP without crumb.

If you want to use HTTP and don't want to use CRUMB. just add your username and password using the '-u' parameter. Replace <username> and <password> with the username and password of your user. Also Check that the url of the jenkins server is correct.

 curl --user <username>:<password> -X POST -F "jenkinsfile=<Jenkinsfile" http://localhost:8080/pipeline-model-converter/validate

src

like image 121
Kent Kostelac Avatar answered Oct 16 '22 19:10

Kent Kostelac


Looks like there are two options for linting pipeline scripts, one via the cli on the leader or an http POST call:

Linting via the CLI with SSH

# ssh (Jenkins CLI)
# JENKINS_SSHD_PORT=[sshd port on master]
# JENKINS_HOSTNAME=[Jenkins master hostname]
ssh -p $JENKINS_SSHD_PORT $JENKINS_HOSTNAME declarative-linter < Jenkinsfile

Linting via HTTP POST using curl

# curl (REST API)
# Assuming "anonymous read access" has been enabled on your Jenkins instance.
# JENKINS_URL=[root URL of Jenkins master]
# JENKINS_CRUMB is needed if your Jenkins master has CRSF protection enabled as it should
JENKINS_CRUMB=`curl "$JENKINS_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)"`
curl -X POST -H $JENKINS_CRUMB -F "jenkinsfile=<Jenkinsfile" $JENKINS_URL/pipeline-model-converter/validate

https://jenkins.io/doc/book/pipeline/development/#linter

like image 29
kongkoro Avatar answered Oct 16 '22 20:10

kongkoro