Zack Williams | e64341b | 2018-04-10 22:14:35 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
Zack Williams | 32293ee | 2018-05-18 09:15:13 -0700 | [diff] [blame] | 3 | # Copyright 2017-present Open Networking Foundation |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Zack Williams | e64341b | 2018-04-10 22:14:35 -0700 | [diff] [blame] | 17 | # jflint.sh - lint for Jenkins declarative pipeline jobs |
| 18 | # |
| 19 | # curl commands from: https://jenkins.io/doc/book/pipeline/development/#linter |
| 20 | |
| 21 | set -e -u -o pipefail |
| 22 | |
Zack Williams | f4c9c40 | 2018-06-27 14:58:43 -0700 | [diff] [blame] | 23 | JENKINS_URL=https://jenkins.opencord.org/ |
Zack Williams | e64341b | 2018-04-10 22:14:35 -0700 | [diff] [blame] | 24 | JF_LIST=() |
| 25 | |
Zack Williams | bd4241f | 2019-02-13 10:36:08 -0700 | [diff] [blame] | 26 | JF_FAIL=0 |
| 27 | |
Zack Williams | e64341b | 2018-04-10 22:14:35 -0700 | [diff] [blame] | 28 | # if no args, and there's a Jenkinsfile in cwd, check it |
| 29 | if [ ! -n "$1" ] && [ -f "Jenkinsfile" ] ; then |
| 30 | JF_LIST+=("Jenkinsfile") |
| 31 | else |
| 32 | # iterate over all args, check if they exist, then add to list of jenkinsfiles to check |
| 33 | for arg in "$@"; do |
| 34 | if [ -f "$arg" ]; then |
| 35 | JF_LIST+=($arg) |
| 36 | else |
| 37 | echo "File does not exist: ${arg}" |
| 38 | exit 1; |
| 39 | fi |
| 40 | done |
| 41 | fi |
| 42 | |
| 43 | # 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] | 44 | 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] | 45 | |
| 46 | for target in "${JF_LIST[@]-}"; do |
Zack Williams | bd4241f | 2019-02-13 10:36:08 -0700 | [diff] [blame] | 47 | echo "Checking: '${target}'" |
| 48 | CURL_OUT=$(curl -s -H "${JENKINS_CRUMB}" -F "jenkinsfile=<${target}" $JENKINS_URL/pipeline-model-converter/validate) |
| 49 | |
| 50 | # Jenkins doesn't set a HTTP failure code when validation fails, so check output |
| 51 | if [[ $CURL_OUT =~ Jenkinsfile\ successfully\ validated ]] |
| 52 | then |
| 53 | echo "Validated successfully: '${target}'" |
| 54 | else |
| 55 | echo "Failed to validate: '${target}' - errors:" |
| 56 | echo "$CURL_OUT" |
| 57 | JF_FAIL=1 |
| 58 | fi |
| 59 | |
Zack Williams | e64341b | 2018-04-10 22:14:35 -0700 | [diff] [blame] | 60 | done |
| 61 | |
Zack Williams | bd4241f | 2019-02-13 10:36:08 -0700 | [diff] [blame] | 62 | exit $JF_FAIL |