Zack Williams | 7468c36 | 2018-04-06 09:52:30 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 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 | 32293ee | 2018-05-18 09:15:13 -0700 | [diff] [blame] | 17 | # ansiblelint.sh - check all yaml files that they pass the ansible-lint tool |
| 18 | |
Zack Williams | 823d63c | 2020-03-18 08:52:34 -0700 | [diff] [blame] | 19 | set +e -o pipefail |
hwchiu | f4a77ce | 2019-09-06 17:15:21 -0700 | [diff] [blame] | 20 | |
Zack Williams | 7468c36 | 2018-04-06 09:52:30 -0700 | [diff] [blame] | 21 | fail_ansible=0 |
| 22 | |
| 23 | # verify that we have ansible-lint installed |
| 24 | command -v ansible-lint >/dev/null 2>&1 || { echo "ansible-lint not found, please install it" >&2; exit 1; } |
| 25 | |
| 26 | # when not running under Jenkins, use current dir as workspace |
| 27 | WORKSPACE=${WORKSPACE:-.} |
| 28 | |
| 29 | echo "=> Linting Ansible Code with $(ansible-lint --version)" |
Zack Williams | 32293ee | 2018-05-18 09:15:13 -0700 | [diff] [blame] | 30 | |
hwchiu | f4a77ce | 2019-09-06 17:15:21 -0700 | [diff] [blame] | 31 | # allow directories to be skipped |
| 32 | # space separated directory list expected in SKIP_DIRS |
hwchiu | f4a77ce | 2019-09-06 17:15:21 -0700 | [diff] [blame] | 33 | SKIP_REGEX="" |
| 34 | |
Zack Williams | 823d63c | 2020-03-18 08:52:34 -0700 | [diff] [blame] | 35 | if [ -n "$SKIP_DIRS" ]; then |
hwchiu | f4a77ce | 2019-09-06 17:15:21 -0700 | [diff] [blame] | 36 | echo "=> Skipping files matching these directories: $SKIP_DIRS" |
| 37 | # prefix with ./ as find generates, swap spaces for pipes |
Zack Williams | 823d63c | 2020-03-18 08:52:34 -0700 | [diff] [blame] | 38 | SKIP_REGEX=$(echo "$SKIP_DIRS" | sed 's/[^ ]*/.\/&\//g' | sed 's/ /|/g') |
hwchiu | f4a77ce | 2019-09-06 17:15:21 -0700 | [diff] [blame] | 39 | fi |
| 40 | |
Zack Williams | 823d63c | 2020-03-18 08:52:34 -0700 | [diff] [blame] | 41 | set -u |
| 42 | |
Zack Williams | 32293ee | 2018-05-18 09:15:13 -0700 | [diff] [blame] | 43 | while IFS= read -r -d '' yf |
| 44 | do |
hwchiu | f4a77ce | 2019-09-06 17:15:21 -0700 | [diff] [blame] | 45 | if [[ $yf =~ $SKIP_REGEX ]]; then |
| 46 | echo "==> SKIPPING: ${yf}" |
| 47 | else |
| 48 | echo "==> CHECKING: ${yf}" |
| 49 | ansible-lint -p "${yf}" |
| 50 | rc=$? |
| 51 | if [[ $rc != 0 ]]; then |
| 52 | echo "==> LINTING FAIL: ${yf}" |
| 53 | fail_ansible=1 |
| 54 | fi |
Zack Williams | 32293ee | 2018-05-18 09:15:13 -0700 | [diff] [blame] | 55 | fi |
| 56 | done < <(find "${WORKSPACE}" \( -name "*.yml" -o -name "*.yaml" \) -print0) |
Zack Williams | 7468c36 | 2018-04-06 09:52:30 -0700 | [diff] [blame] | 57 | |
| 58 | exit ${fail_ansible} |