Skip to content

Instantly share code, notes, and snippets.

@SansGuidon
Last active April 10, 2023 10:57
Show Gist options
  • Save SansGuidon/2958ba47630a176733e0136b42557284 to your computer and use it in GitHub Desktop.
Save SansGuidon/2958ba47630a176733e0136b42557284 to your computer and use it in GitHub Desktop.
Validating Jenkinsfile in Vim and/or using CLI / terminal
" ~/.vimrc
" ...
" Check syntax in Vim asynchronously and fix files, with Language Server Protocol (LSP) support
Plugin 'dense-analysis/ale'
" ...
" dense-analysis/ale options
let g:ale_history_log_output = 1
let g:ale_use_global_executables = 1
let g:ale_fix_on_save = 1
let g:ale_completion_enabled = 1
let g:ale_open_list = 1
let g:ale_fixers = {
\ '*': ['remove_trailing_lines', 'trim_whitespace'],
\ 'Jenkinsfile': ['checkci'],
\}
#!/usr/bin/env bash
# ~/.zshrc
#...
# expose those variables here or anything else
export JENKINS_USERNAME=""
export JENKINS_URL="https://jenkins.mycompany.com"
export JENKINS_SECRET="<API_TOKEN_HERE>"
#...

Introduction

This gist shows how you can validate Jenkinsfile within Vim :-) but if you don't use Vim, you should be able to use it in a terminal or adapt it to your needs anyway, you can even convert it to a bash function ;-)

It's inspired by the official documentation https://www.jenkins.io/doc/book/pipeline/development/#linter

Validator script

  • Expose your jenkins credentials and instance URLs somehow (environment variables)
    • JENKINS_URL
    • JENKINS_USERNAME
    • JENKINS_SECRET (password, or user token...)
  • Create somewhere (~/scripts/checkci.sh ?) a script with the proposed content in this gist.
    • make it executable chmod +x ~/.scripts/checkci.sh
    • create a symbolic link to it. example : ln -snf ~/.scripts/checkci.sh /usr/local/bin/checkci
  • It should be enough to allow you to validate Jenkinsfile via the terminal. example: checkci, checkci Jenkinsfile.acceptance, ...

Vim Integration

Demo (Vim Integration)

Demo (Vim)

Demo (CLI)

Demo (CLI)

#!/usr/bin/env bash
# ~/.scripts/checkci.sh
#...
# expose those variables (in your ~/.zshrc, your (bash) profile, or anywhere you prefer...
#export JENKINS_USERNAME=""
#export JENKINS_URL=""
#export JENKINS_SECRET=""
#...
function _checkci() {
local jenkinsfile="${1:-Jenkinsfile}"
curl --user "$JENKINS_USERNAME:$JENKINS_SECRET" -X POST -F "jenkinsfile=<$jenkinsfile" "$JENKINS_URL/pipeline-model-converter/validate"
}
_checkci "$*"
exit 0
" ~/.vim/ale_linters/Jenkinsfile/checkci.vim
call ale#linter#Define('Jenkinsfile', {
\ 'name': 'checkci',
\ 'executable': 'checkci',
\ 'command': 'checkci',
\ 'callback': 'ale_linters#Jenkinsfile#checkci#HandleJenkinsValidator',
\})
function! ale_linters#Jenkinsfile#checkci#HandleJenkinsValidator(buffer, lines) abort
" Regular expression to match messages:
" They look like:
" WorkflowScript: 7: Expected a step @ line 7, column 17.
let l:pattern = '\v^WorkflowScript: (\d+): (.*) \@ line (\d+), column (\d+)\.(\_.*)$'
" For each match, update the l:output list:
let l:output = []
for l:match in ale#util#GetMatches(a:lines, l:pattern)
let l:code = l:match[5]
call add(l:output, {
\ 'lnum': l:match[3] + 0,
\ 'col': l:match[4] + 0,
\ 'text': l:code . ': ' . l:match[2],
\})
endfor
return l:output
endfunction
@burnettk
Copy link

hot! i'm curious if this vim plugin works for your use case: https://github.com/burnettk/vim-jenkins. it looks like your vim integration goes a bit deeper by highlighting the lines of code that failed validation. feel free to pull request or submit an issue or point me to an alternate repo if you want to collaborate on this!

@rshutt-va
Copy link

So, when I do this - vim is always complaining that:

There is no fixer named `checkci`.  Check :ALEFixSuggest

Which, of course, seems to be true since this example adds a linter and not a fixer? But if I remove checkci from the list of fixers for Jenkinsfile, of course the linter does not run at all?

boggle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment