Skip to content

Commit

Permalink
Converted shell check workflow to zuul
Browse files Browse the repository at this point in the history
Signed-off-by: Gondermann <gondermann@b1-systems.de>
  • Loading branch information
gndrmnn committed Sep 25, 2023
1 parent e2f04a9 commit 4eb6b8c
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 6 deletions.
19 changes: 13 additions & 6 deletions .zuul.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@
vars:
tox_envlist: gilt

- job:
name: check-shell-syntax
run: playbooks/check-shell-syntax/run.yaml

- project:
merge-mode: squash-merge
default-branch: main
check:
jobs:
- cfg-generics-tox
- flake8
- yamllint
#- cfg-generics-tox
- check-shell-syntax
#- flake8
#- yamllint
gate:
jobs:
- cfg-generics-tox
- flake8
- yamllint
#- cfg-generics-tox
- check-shell-syntax
#- flake8
#- yamllint
periodic-daily:
jobs:
- cfg-generics-tox
- check-shell-syntax
- flake8
- yamllint
5 changes: 5 additions & 0 deletions playbooks/check-shell-syntax/run.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- name: Check-shell-syntax
hosts: all
roles:
- check_shell_syntax
9 changes: 9 additions & 0 deletions roles/check_shell_syntax/defaults/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
# defaults file for check_shell_syntax
zuul_work_dir: "{{ zuul.project.src_dir }}"
shell_check_install_path: "$HOME/shellcheck"
shell_check_version: "stable"
shell_check_scan_dir: "."
shell_check_format: "gcc"
shell_check_ignore_paths: ""
shell_check_ignore_names: ""
114 changes: 114 additions & 0 deletions roles/check_shell_syntax/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
---
# tasks file for check-shell-syntax
- name: Download shellcheck
ansible.builtin.shell:
cmd: |
mkdir -p "{{ shell_check_install_path }}"
baseurl="https://github.com/koalaman/shellcheck/releases/download"
curl -Lso "{{ shell_check_install_path }}/sc.tar.xz" \
"${baseurl}/{{ shell_check_version }}/shellcheck-{{ shell_check_version }}.linux.x86_64.tar.xz"
tar -xf "{{ shell_check_install_path }}/sc.tar.xz" -C "{{ shell_check_install_path }}"
mv "{{ shell_check_install_path }}/shellcheck-{{ shell_check_version }}/shellcheck" \
"{{ shell_check_install_path }}/shellcheck"
executable: /bin/bash
changed_when: false

- name: Display shellcheck version
ansible.builtin.shell:
cmd: |
"{{ shell_check_install_path }}/shellcheck" --version
executable: /bin/bash
changed_when: false

- name: Run the check
ansible.builtin.shell:
cmd: |
set -o pipefail
statuscode=0
set -f # temporarily disable globbing so that globs in input aren't expanded
declare -a excludes
excludes+=("! -path *./.git/*")
excludes+=("! -path *.go")
excludes+=("! -path */mvnw")
if [[ -n "{{ shell_check_ignore_paths }}" ]]; then
for path in "{{ shell_check_ignore_paths }}"; do
excludes+=("! -path *./$path/*")
excludes+=("! -path */$path/*")
excludes+=("! -path $path")
done
fi
if [[ -n "{{ shell_check_ignore_names }}" ]]; then
for name in "{{ shell_check_ignore_names }}"; do
excludes+=("! -name $name")
done
fi
echo "excludes=${excludes[@]}"
declare -a filepaths
shebangregex="^#! */[^ ]*/(env *)?[abk]*sh"
while IFS= read -r -d '' file; do
filepaths+=("$file")
done < <(find "{{ shell_check_scan_dir }}" \
${excludes} \
-type f \
'(' \
-name '*.bash' \
-o -name '.bashrc' \
-o -name 'bashrc' \
-o -name '.bash_aliases' \
-o -name '.bash_completion' \
-o -name '.bash_login' \
-o -name '.bash_logout' \
-o -name '.bash_profile' \
-o -name 'bash_profile' \
-o -name '*.ksh' \
-o -name 'suid_profile' \
-o -name '*.zsh' \
-o -name '.zlogin' \
-o -name 'zlogin' \
-o -name '.zlogout' \
-o -name 'zlogout' \
-o -name '.zprofile' \
-o -name 'zprofile' \
-o -name '.zsenv' \
-o -name 'zsenv' \
-o -name '.zshrc' \
-o -name 'zshrc' \
-o -name '*.sh' \
-o -path '*/.profile' \
-o -path '*/profile' \
-o -name '*.shlib' \
')' \
-print0)
while IFS= read -r -d '' file; do
head -n1 "$file" | grep -Eqs "$shebangregex" || continue
filepaths+=("$file")
done < <(find "{{ shell_check_scan_dir }}" \
-type f ! -name '*.*' -perm /111 \
-print0)
for file in "${filepaths[@]}"; do
"{{ shell_check_install_path }}/shellcheck" \
--format={{ shell_check_format }} \
"$file" || statuscode=$?
done
echo "filepaths=${filepaths[@]}"
set +f # re-enable globbing
exit $statuscode
executable: /bin/bash
chdir: "{{ zuul_work_dir }}"
changed_when: false

0 comments on commit 4eb6b8c

Please sign in to comment.