john2x / jenkinsfile-mode

Major mode for editing Jenkins declarative pipeline syntax
GNU General Public License v3.0
16 stars 10 forks source link

Validation of Jenkinsfile via API #1

Open OverkillGuy opened 4 years ago

OverkillGuy commented 4 years ago

Believe it or not, Jenkins has an API to validate jenkinsfiles: See an interesting article about it.

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

Imagine running this (or equivalent in elisp) and reporting the result as a flymake/flycheck plugin, hovering over problems with info like:

Unknown stage section "git". Starting with version 0.5, steps in a stage must be in a ‘steps’ block. @ line 10, column 9.
           stage('Checkout Code') {
           ^

WorkflowScript: 10: Expected one of "steps", "stages", or "parallel" for stage "Checkout Code" @ line 10, column 9.
           stage('Checkout Code') {
           ^

I'd like that feature to be in emacs (towards feature-parity with VSCode, yey), and your mode sounds relevant for that, hence my ticket. I'd love to do it myself, but I will likely be distracted (always am) so I guess I am posting this here as FYI, to remind myself, and to share the joy. Feel free to close this as wontfix.

hydev commented 4 years ago

FYI, writing a basic flycheck checker is quite simple (see). In my setup I use the Jenkins declarative linter (aka Jenkinsfile validator) via SSH, therefore I have the following in my init.el:

(flycheck-define-checker jenkinsfile
  "A Jenkins declarative pipeline syntax checker using the Jenkins declarative linter.
See URL `https://www.jenkins.io/doc/book/pipeline/development/#linter'."
  :command ("ssh" "-p" "16022" "user@localhost" "declarative-linter")
  :standard-input t
  :error-patterns
  ((error line-start "WorkflowScript: " line ": " (message) " @ line " line ", column " column "." line-end))
  :modes (jenkinsfile-mode groovy-mode)))

Just set the SSH port according to your setup in Jenkins, add your SSH public key and set user@localhost to your username and Jenkins URL. The error patterns may not be complete, but this should get you started.

CsBigDataHub commented 4 years ago

+1

I wanted to create a feature request for this but some else beat me to it. Thanks.

@hydev can I use curl in place of ssh?

CsBigDataHub commented 2 years ago

FYI, writing a basic flycheck checker is quite simple (see). In my setup I use the Jenkins declarative linter (aka Jenkinsfile validator) via SSH, therefore I have the following in my init.el:

(flycheck-define-checker jenkinsfile
  "A Jenkins declarative pipeline syntax checker using the Jenkins declarative linter.
See URL `https://www.jenkins.io/doc/book/pipeline/development/#linter'."
  :command ("ssh" "-p" "16022" "user@localhost" "declarative-linter")
  :standard-input t
  :error-patterns
  ((error line-start "WorkflowScript: " line ": " (message) " @ line " line ", column " column "." line-end))
  :modes (jenkinsfile-mode groovy-mode)))

Just set the SSH port according to your setup in Jenkins, add your SSH public key and set user@localhost to your username and Jenkins URL. The error patterns may not be complete, but this should get you started.

If Someone wants curl alternative for flycheck use this - https://github.com/CsBigDataHub/flycheck-jenkinsfile/blob/master/flycheck-jenkinsfile.el