Zack Williams | e64341b | 2018-04-10 22:14:35 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
Zack Williams | be54231 | 2022-06-23 21:51:32 -0700 | [diff] [blame] | 3 | # SPDX-FileCopyrightText: 2017-present Open Networking Foundation |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
Zack Williams | 32293ee | 2018-05-18 09:15:13 -0700 | [diff] [blame] | 5 | |
Zack Williams | e64341b | 2018-04-10 22:14:35 -0700 | [diff] [blame] | 6 | # jflint.sh - lint for Jenkins declarative pipeline jobs |
| 7 | # |
| 8 | # curl commands from: https://jenkins.io/doc/book/pipeline/development/#linter |
| 9 | |
| 10 | set -e -u -o pipefail |
| 11 | |
Zack Williams | f4c9c40 | 2018-06-27 14:58:43 -0700 | [diff] [blame] | 12 | JENKINS_URL=https://jenkins.opencord.org/ |
Zack Williams | e64341b | 2018-04-10 22:14:35 -0700 | [diff] [blame] | 13 | JF_LIST=() |
| 14 | |
Zack Williams | bd4241f | 2019-02-13 10:36:08 -0700 | [diff] [blame] | 15 | JF_FAIL=0 |
| 16 | |
Zack Williams | e64341b | 2018-04-10 22:14:35 -0700 | [diff] [blame] | 17 | # if no args, and there's a Jenkinsfile in cwd, check it |
| 18 | if [ ! -n "$1" ] && [ -f "Jenkinsfile" ] ; then |
| 19 | JF_LIST+=("Jenkinsfile") |
| 20 | else |
| 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 |
| 30 | fi |
| 31 | |
| 32 | # JENKINS_CRUMB is needed if your Jenkins master has CRSF protection enabled as it should |
Zack Williams | bd4241f | 2019-02-13 10:36:08 -0700 | [diff] [blame] | 33 | JENKINS_CRUMB=$(curl -s "$JENKINS_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)") |
Zack Williams | e64341b | 2018-04-10 22:14:35 -0700 | [diff] [blame] | 34 | |
| 35 | for target in "${JF_LIST[@]-}"; do |
Zack Williams | bd4241f | 2019-02-13 10:36:08 -0700 | [diff] [blame] | 36 | 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 Williams | e64341b | 2018-04-10 22:14:35 -0700 | [diff] [blame] | 49 | done |
| 50 | |
Zack Williams | bd4241f | 2019-02-13 10:36:08 -0700 | [diff] [blame] | 51 | exit $JF_FAIL |