Skip to content

Instantly share code, notes, and snippets.

@SansGuidon
Last active April 10, 2023 10:57
Show Gist options
  • Select an option

  • Save SansGuidon/2958ba47630a176733e0136b42557284 to your computer and use it in GitHub Desktop.

Select an option

Save SansGuidon/2958ba47630a176733e0136b42557284 to your computer and use it in GitHub Desktop.

Revisions

  1. SansGuidon revised this gist Jun 24, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,8 @@

    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`
  2. SansGuidon revised this gist Jun 24, 2020. No changes.
  3. SansGuidon revised this gist Jun 24, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ This gist shows how you can validate Jenkinsfile within Vim :-) but if you don't
    `checkci`, `checkci Jenkinsfile.acceptance`, ...

    ## Vim Integration
    * Install https://github.com/dense-analysis/ale via the vim plugin manager of your choice, or manually...
    * Install https://github.com/dense-analysis/ale via the vim plugin manager of your choice, or manually... I suggest you start at https://vimawesome.com/plugin/ale ;)
    * Update your `~/.vimrc` with the proposed config
    * Create `~/.vim/ale_linters/Jenkinsfile/checkci.vim` with the proposed content

  4. SansGuidon revised this gist Jun 24, 2020. 1 changed file with 2 additions and 5 deletions.
    7 changes: 2 additions & 5 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -9,11 +9,8 @@ This gist shows how you can validate Jenkinsfile within Vim :-) but if you don't
    * `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:
    * 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
  5. SansGuidon revised this gist Jun 24, 2020. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,9 @@ This gist shows how you can validate Jenkinsfile within Vim :-) but if you don't
    ```
    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
    * Install https://github.com/dense-analysis/ale via the vim plugin manager of your choice, or manually...
    * Update your `~/.vimrc` with the proposed config
  6. SansGuidon revised this gist Jun 24, 2020. No changes.
  7. SansGuidon revised this gist Jun 24, 2020. 2 changed files with 4 additions and 2 deletions.
    2 changes: 2 additions & 0 deletions .vimrc
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,9 @@
    " ~/.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
    4 changes: 2 additions & 2 deletions .zshrc
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,6 @@
    #...
    # expose those variables here or anything else
    export JENKINS_USERNAME=""
    export JENKINS_URL=""
    export JENKINS_SECRET=""
    export JENKINS_URL="https://jenkins.mycompany.com"
    export JENKINS_SECRET="<API_TOKEN_HERE>"
    #...
  8. SansGuidon revised this gist Jun 24, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -22,4 +22,4 @@ ln -snf ~/.scripts/checkci.sh /usr/local/bin/checkci
    ![Demo (Vim)](https://i.imgur.com/COjoQZq.png)

    ## Demo (CLI)
    [!Demo (CLI)](https://i.imgur.com/oU4cY6T.png)
    ![Demo (CLI)](https://i.imgur.com/oU4cY6T.png)
  9. SansGuidon revised this gist Jun 24, 2020. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -18,5 +18,8 @@ ln -snf ~/.scripts/checkci.sh /usr/local/bin/checkci
    * Update your `~/.vimrc` with the proposed config
    * Create `~/.vim/ale_linters/Jenkinsfile/checkci.vim` with the proposed content

    ## Demo
    ![Demo](https://i.imgur.com/COjoQZq.png)
    ## Demo (Vim Integration)
    ![Demo (Vim)](https://i.imgur.com/COjoQZq.png)

    ## Demo (CLI)
    [!Demo (CLI)](https://i.imgur.com/oU4cY6T.png)
  10. SansGuidon revised this gist Jun 24, 2020. 1 changed file with 13 additions and 4 deletions.
    17 changes: 13 additions & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,22 @@
    * Install https://github.com/dense-analysis/ale via the vim plugin manager of your choice, or manually...
    * Update your `~/.vimrc` with the proposed config
    # 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 ;-)

    ## Validator script
    * Expose your jenkins credentials and instance URLs somehow (environment variables)
    * `JENKINS_URL`
    * `JENKINS_USERNAME`
    * `JENKINS_SECRET` (password, or user token...)
    * Create `~/.vim/ale_linters/Jenkinsfile/checkci.vim` with the proposed content
    * 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
    ```
    ```
    ## Vim Integration
    * Install https://github.com/dense-analysis/ale via the vim plugin manager of your choice, or manually...
    * Update your `~/.vimrc` with the proposed config
    * Create `~/.vim/ale_linters/Jenkinsfile/checkci.vim` with the proposed content

    ## Demo
    ![Demo](https://i.imgur.com/COjoQZq.png)
  11. SansGuidon created this gist Jun 24, 2020.
    15 changes: 15 additions & 0 deletions .vimrc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    " ~/.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'],
    \}
    8 changes: 8 additions & 0 deletions .zshrc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    #!/usr/bin/env bash
    # ~/.zshrc
    #...
    # expose those variables here or anything else
    export JENKINS_USERNAME=""
    export JENKINS_URL=""
    export JENKINS_SECRET=""
    #...
    13 changes: 13 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    * Install https://github.com/dense-analysis/ale via the vim plugin manager of your choice, or manually...
    * Update your `~/.vimrc` with the proposed config
    * Expose your jenkins credentials and instance URLs somehow (environment variables)
    * `JENKINS_URL`
    * `JENKINS_USERNAME`
    * `JENKINS_SECRET` (password, or user token...)
    * Create `~/.vim/ale_linters/Jenkinsfile/checkci.vim` with the proposed content
    * 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
    ```
    14 changes: 14 additions & 0 deletions checkci.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    #!/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
    28 changes: 28 additions & 0 deletions checkci.vim
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    " ~/.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