Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
- id: jenkinslint
name: Lint Jenkinsfile
name: Lint Jenkinsfiles
description: Validates Jenkinsfiles using a Jenkins server
entry: jenkinslint
language: script
files: ^Jenkinsfile
types: [file, jenkins]
11 changes: 5 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ Not all parameters need to be set. Example:
Standalone
----------

Validate files using ``jenkinslint`` followed by the name of the Jenkinsfile,
Validate files using ``jenkinslint`` followed by a list of Jenkinsfiles,
e.g.

::

jenkinslint Jenkinsfile
jenkinslint Jenkinsfile a.jenkins b.jenkinsfile

Using the pre-commit framework
------------------------------
Expand All @@ -55,10 +55,9 @@ Install the pre-commit framework, and add the following to the
::

- repo: https://github.com/PeterMosmans/jenkinslint
rev: master
rev: v1.0.0
hooks:
- id: jenkinslint

This will automatically validate files named ``Jenkinsfile`` during the pre
commit phase.

This will automatically validate files named ``Jenkinsfile, *.jenkins or
*.jenkinsfile`` during the pre commit phase.
43 changes: 28 additions & 15 deletions jenkinslint
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,11 @@ fi
# shellcheck disable=SC1090
[[ -f "${INIFILE}" ]] && source "${INIFILE}"

JENKINS_FILE=${1:-Jenkinsfile}
if [ ! -f "${JENKINS_FILE}" ]; then
echo "Could not find file ${JENKINS_FILE}"
exit 1
else
echo "Validating ${JENKINS_FILE}"
fi

# Disable certificate validation when JENKINS_UNSAFE_SSL parameter is defined
[[ -n "${JENKINS_UNSAFE_SSL}" ]] && JENKINS_UNSAFE_SSL="-k"

ssh_validation(){
JENKINS_FILE=$1
result=$(ssh "${JENKINS_SERVER}" -p "${JENKINS_SSH_PORT}" declarative-linter < "${JENKINS_FILE}")
return=$?
if [[ $return -ge 2 ]]; then
Expand All @@ -72,6 +65,7 @@ show_settings(){
}

web_validation(){
JENKINS_FILE=$1
JENKINS_CRUMB=$(curl ${JENKINS_UNSAFE_SSL} -s -m 5 "$JENKINS_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)")
return=$?
if [ $return -ne 0 ]; then
Expand All @@ -92,16 +86,35 @@ web_validation(){
validate(){
if [[ $result =~ Errors ]]; then
echo "$result"
exit 1
ERROR=true
else
echo "$result"
exit 0
fi
}

if [[ "$JENKINS_SSH_PORT" ]]; then
ssh_validation
else
web_validation
ERROR=false

JENKINS_FILE_LIST=("${@}")
if [[ $# == 0 ]]; then
# Default to file named Jenkinsfile if no arguments are supplied
JENKINS_FILE_LIST+=("Jenkinsfile")
fi

for jenkinsfile in "${JENKINS_FILE_LIST[@]}"; do
if [ ! -f "${jenkinsfile}" ]; then
echo "Could not find file ${jenkinsfile}"
else
echo "Linting ${jenkinsfile} ..."
if [[ "$JENKINS_SSH_PORT" ]]; then
ssh_validation "$jenkinsfile"
else
web_validation "$jenkinsfile"
fi
validate
fi
done

if [[ $ERROR == "true" ]]; then
exit 1
fi
validate
exit 0