Tip of the day: validating a Jenkinsfile from the CLI with HTTPie

Submitted by Frederic Marand on

The Jenkins butler with a keyboard, variations by fgm@osinetfr and Dall-E 2An easily missed feature in Jenkins is the bundled linter for Jenkinsfiles.

While there are many examples about how to use it with cURL, I could not find any using HTTPie, which I find much more convenient in general than cURL. So, after some fumbling due to the little documented expectations of the validator, here is the solution showing how to validate a Jenkinsfile from the CLI:

http --form                                         \
     -a $USER:$PASS                                 \
     -pb                                            \
     POST                                           \
     $JENKINS_URL/pipeline-model-converter/validate \
     jenkinsfile=@$JENKINSFILE
Let's see what these arguments mean:
  • --form specifies use of the application/x-www-form-urlencoded MIME type, as expected by the validator
  • -a $USER:$PASS provides basic authentication credentials.
    • If you do not want to include the password, just use $USER: and HTTPie will request the password on the CLI.
    • This assumes these two variables contain your Jenkins username and password
  • -pb asks HTTPie to only print the response body
  • $JENKINS_URL is the absolute URL to the Jenkins root
  • $JENKINSFILE is the path to the Jenkinsfile to validate. May be relative or absolute
That's it. Note that, as usual with HTTPie, the ordering is strict:
  1. http the command itself
  2. The options, like -a and --form
  3. The method, in this case POST. HTTPie uses GET by default
  4. The target URL
  5. The so-called "fields", which translate to the request body. In this case, the jenkinsfile=@$JENKINSFILE requests HTTPie to load the contents of the file designated by $JENKINSFILE and load that content as the value of the jenkinsfile HTML form field

Hopefully, you will receive a result looking like this:

Jenkinsfile successfully validated.

More details in the HTTPie forms documentation.