blob: ea5a82a6fbb8059d4194c2b67f33b3f61850584e [file] [log] [blame]
Zack Williams7468c362018-04-06 09:52:30 -07001#!/usr/bin/env bash
2
Joey Armstrongbeef4cd2023-01-18 09:59:58 -05003# Copyright 2017-2023 Open Networking Foundation (ONF) and the ONF Contributors
Zack Williams7468c362018-04-06 09:52:30 -07004#
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 Williams32293ee2018-05-18 09:15:13 -070017# ansiblelint.sh - check all yaml files that they pass the ansible-lint tool
18
Zack Williams823d63c2020-03-18 08:52:34 -070019set +e -o pipefail
hwchiuf4a77ce2019-09-06 17:15:21 -070020
Zack Williams7468c362018-04-06 09:52:30 -070021fail_ansible=0
22
23# verify that we have ansible-lint installed
24command -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
27WORKSPACE=${WORKSPACE:-.}
28
29echo "=> Linting Ansible Code with $(ansible-lint --version)"
Zack Williams32293ee2018-05-18 09:15:13 -070030
hwchiuf4a77ce2019-09-06 17:15:21 -070031# allow directories to be skipped
32# space separated directory list expected in SKIP_DIRS
hwchiuf4a77ce2019-09-06 17:15:21 -070033SKIP_REGEX=""
34
Zack Williams823d63c2020-03-18 08:52:34 -070035if [ -n "$SKIP_DIRS" ]; then
hwchiuf4a77ce2019-09-06 17:15:21 -070036 echo "=> Skipping files matching these directories: $SKIP_DIRS"
37 # prefix with ./ as find generates, swap spaces for pipes
Zack Williams823d63c2020-03-18 08:52:34 -070038 SKIP_REGEX=$(echo "$SKIP_DIRS" | sed 's/[^ ]*/.\/&\//g' | sed 's/ /|/g')
hwchiuf4a77ce2019-09-06 17:15:21 -070039fi
40
Zack Williams823d63c2020-03-18 08:52:34 -070041set -u
42
Zack Williams32293ee2018-05-18 09:15:13 -070043while IFS= read -r -d '' yf
44do
hwchiuf4a77ce2019-09-06 17:15:21 -070045 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 Williams32293ee2018-05-18 09:15:13 -070055 fi
56done < <(find "${WORKSPACE}" \( -name "*.yml" -o -name "*.yaml" \) -print0)
Zack Williams7468c362018-04-06 09:52:30 -070057
58exit ${fail_ansible}