blob: cef7b910a8cca6efa20371058130ccce73ea3d16 [file] [log] [blame]
Zack Williamse64341b2018-04-10 22:14:35 -07001#!/usr/bin/env bash
2
Zack Williamsbe542312022-06-23 21:51:32 -07003# SPDX-FileCopyrightText: 2017-present Open Networking Foundation
4# SPDX-License-Identifier: Apache-2.0
Zack Williams32293ee2018-05-18 09:15:13 -07005
Zack Williamse64341b2018-04-10 22:14:35 -07006# jflint.sh - lint for Jenkins declarative pipeline jobs
7#
8# curl commands from: https://jenkins.io/doc/book/pipeline/development/#linter
9
10set -e -u -o pipefail
11
Zack Williamsf4c9c402018-06-27 14:58:43 -070012JENKINS_URL=https://jenkins.opencord.org/
Zack Williamse64341b2018-04-10 22:14:35 -070013JF_LIST=()
14
Zack Williamsbd4241f2019-02-13 10:36:08 -070015JF_FAIL=0
16
Zack Williamse64341b2018-04-10 22:14:35 -070017# if no args, and there's a Jenkinsfile in cwd, check it
18if [ ! -n "$1" ] && [ -f "Jenkinsfile" ] ; then
19 JF_LIST+=("Jenkinsfile")
20else
21# iterate over all args, check if they exist, then add to list of jenkinsfiles to check
22 for arg in "$@"; do
23 if [ -f "$arg" ]; then
24 JF_LIST+=($arg)
25 else
26 echo "File does not exist: ${arg}"
27 exit 1;
28 fi
29 done
30fi
31
32# JENKINS_CRUMB is needed if your Jenkins master has CRSF protection enabled as it should
Zack Williamsbd4241f2019-02-13 10:36:08 -070033JENKINS_CRUMB=$(curl -s "$JENKINS_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)")
Zack Williamse64341b2018-04-10 22:14:35 -070034
35for target in "${JF_LIST[@]-}"; do
Zack Williamsbd4241f2019-02-13 10:36:08 -070036 echo "Checking: '${target}'"
37 CURL_OUT=$(curl -s -H "${JENKINS_CRUMB}" -F "jenkinsfile=<${target}" $JENKINS_URL/pipeline-model-converter/validate)
38
39 # Jenkins doesn't set a HTTP failure code when validation fails, so check output
40 if [[ $CURL_OUT =~ Jenkinsfile\ successfully\ validated ]]
41 then
42 echo "Validated successfully: '${target}'"
43 else
44 echo "Failed to validate: '${target}' - errors:"
45 echo "$CURL_OUT"
46 JF_FAIL=1
47 fi
48
Zack Williamse64341b2018-04-10 22:14:35 -070049done
50
Zack Williamsbd4241f2019-02-13 10:36:08 -070051exit $JF_FAIL