5

I've been trying to use Command-line Pipeline Linter.

Can i use this declarative-linter locally on the server?

I tried run:

declarative-linter < Jenkinsfile

And got:

-bash: declarative-linter: command not found

Do i need to install this command or it should be installed automatically after the Pipeline plugin installation?

Please advise.

Omri
  • 1,436
  • 7
  • 31
  • 61

1 Answers1

2

There are three high-level methods of interaction with the declarative-linter. The specific set you have requested is the CLI, with the specific subset of on the server itself. The documentation you referenced then references the CLI documentation to read how to form commands with the Jenkins CLI.

In general, the command on the server looks like:

java -jar jenkins-cli.jar [-s JENKINS_URL] [global options...] command [command options...] [arguments...]

When running on the server, you can use localhost for the URL. The full path to the jenkins-cli is also helpful to be safe (example shown below is for the RedHat family). Note you should use https below instead if you are using a cert for the server.

java -jar /var/cache/jenkins/war/WEB-INF/jenkins-cli.jar -s http://localhost:8080/ [global options...] command [command options...] [arguments...]

Now we substitute in the command and arguments specific to the declarative-linter. Note we are using the stdin syntax specific to Linux.

java -jar /var/cache/jenkins/war/WEB-INF/jenkins-cli.jar -s http://localhost:8080/ declarative-linter < /path/to/Jenkinsfile

That will allow local linting of your Jenkinsfile via the jenkins-cli on the Jenkins server. Note that if you need to authenticate, you need to substitute a -auth username:password or -i /path/to/ssh_key in the [global options...] part of the command before declarative-linter and after the argument for the Jenkins URL.

Matthew Schuchard
  • 25,172
  • 3
  • 47
  • 67
  • Thanks for your answer. When i run this command i get: `ERROR: anonymous is missing the Overall/Read permission`. I haven't managed to solve it yet. – Omri Feb 19 '19 at 10:07
  • @Omri You need to run it with elevated permissions too i.e. `sudo`. – Matthew Schuchard Feb 19 '19 at 11:40
  • 1
    @Omri That typically happens when you are not running on the Jenkins server. If you are sure you are running on the Jenkins server, you can still solve it either way by injecting a `-auth username:password` or `-i /path/to/ssh_key` right before the `declarative-linter` in the command. – Matthew Schuchard Feb 19 '19 at 12:01
  • Thanks. Now it works great! I already tried to use the `auth` parameter yesterday, But it didn't work since i used `who-am-i` as described here: https://www.jeffgeerling.com/blog/2018/fixing-jenkins-cli-error-anonymous-missing-overallread-permission. – Omri Feb 19 '19 at 12:15