Commit 9a21b5b7 authored by Markus Frosch's avatar Markus Frosch 📣

Add shellcheck CI and fix shellcheck issues

parent ebf089ad
Pipeline #2251 passed with stage
in 16 seconds
stages:
- lint
shellcheck:
stage: lint
image: koalaman/shellcheck-alpine:stable
tags:
- docker-build
script:
- ./test/shellcheck.sh
......@@ -4,7 +4,7 @@
. "$(dirname "$(readlink -f "$0")")"/functions-common.sh
detect_project() {
dists=($(grep -E ^Source: */debian/control | awk '{print $2}' | sort -u))
dists=( "$(grep -E ^Source: ./*/debian/control | awk '{print $2}' | sort -u)" )
if [ "${#dists[@]}" -eq 0 ]; then
echo "No Debian control files found!" >&2
......@@ -78,14 +78,14 @@ require_var() {
echo
}
: ${ICINGA_BUILD_PROJECT:=`detect_project`}
: ${ICINGA_BUILD_OS:=`detect_os`}
: ${ICINGA_BUILD_DIST:=`detect_dist`}
: ${ICINGA_BUILD_ARCH:=`detect_arch`}
: ${ICINGA_BUILD_DEB_FLAVOR:="$ICINGA_BUILD_DIST"}
: ${ICINGA_BUILD_TYPE:="release"}
: ${ICINGA_BUILD_UPSTREAM_BRANCH:="master"}
: ${ICINGA_BUILD_IGNORE_LINT:=1}
: "${ICINGA_BUILD_PROJECT:="$(detect_project)"}"
: "${ICINGA_BUILD_OS:="$(detect_os)"}"
: "${ICINGA_BUILD_DIST:="$(detect_dist)"}"
: "${ICINGA_BUILD_ARCH:="$(detect_arch)"}"
: "${ICINGA_BUILD_TYPE:="release"}"
: "${ICINGA_BUILD_UPSTREAM_BRANCH:="master"}"
: "${ICINGA_BUILD_DEB_FLAVOR:="$ICINGA_BUILD_DIST"}"
: "${ICINGA_BUILD_IGNORE_LINT:=1}"
print_build_env
......@@ -93,7 +93,8 @@ require_var ICINGA_BUILD_PROJECT ICINGA_BUILD_OS ICINGA_BUILD_DIST ICINGA_BUILD_
export_build_env
export LANG=C
WORKDIR=`pwd`
WORKDIR="$(pwd)"
BUILDDIR='build'
export WORKDIR BUILDDIR
# vi: ts=2 sw=2 expandtab
......@@ -4,7 +4,7 @@
. "$(dirname "$(readlink -f "$0")")"/functions-common.sh
detect_project() {
specs=($(ls *.spec))
IFS=" " read -r -a specs <<< "$(ls ./*.spec)"
if [ "${#specs[@]}" -eq 0 ]; then
echo "No Spec file found!" >&2
......@@ -13,7 +13,7 @@ detect_project() {
echo "More than one spec file found!" >&2
exit 1
else
echo "$(basename "${specs[0]}" .spec)"
basename "${specs[0]}" .spec
fi
}
......@@ -30,7 +30,7 @@ detect_os() {
fi
elif [ -f /etc/redhat-release ]; then
# before el7
os=$(cat /etc/redhat-release | awk '{ print tolower($1) }')
os=$(awk '{ print tolower($1) }' /etc/redhat-release)
if [ -n "$os" ]; then
echo "$os"
......@@ -59,7 +59,7 @@ detect_dist() {
fi
elif [ -f /etc/redhat-release ]; then
# before el7
dist=$(cat /etc/redhat-release | grep -Po '(?<=\s)(\d+(\.\d+)+)(?=\s)' | cut -d. -f1)
dist=$(grep -Po '(?<=\s)(\d+(\.\d+)+)(?=\s)' /etc/redhat-release | cut -d. -f1)
if [ -n "$dist" ]; then
echo "$dist"
......@@ -76,7 +76,7 @@ detect_dist() {
detect_arch() {
# TODO: verify for SUSE
arches=( "$(rpm -qa rpm "*-release" --qf "%{arch}\n" | sort -u | grep -v noarch)" )
IFS=" " read -r -a arches <<< "$(rpm -qa rpm "*-release" --qf "%{arch}\\n" | sort -u | grep -v noarch)"
if [ "${#arches[@]}" -eq 0 ]; then
echo "No basearch found while looking for *-release or rpm packages!" >&2
......@@ -107,16 +107,16 @@ require_var() {
}
get_rpmbuild() {
local RPMBUILD dist setarch
dist=`rpm -E '%{?dist}' | sed 's/\(\.centos\)\?$/.icinga/'`
setarch=''
# TODO: target_arch
if [ -n "$target_arch" ]; then
setarch="setarch ${target_arch}"
fi
local RPMBUILD dist
dist="$(rpm -E '%{?dist}' | sed 's/\(\.centos\)\?$/.icinga/')"
# TODO: target_arch needed?
#local setarch=''
#if [ -n "$target_arch" ]; then
# setarch="setarch ${target_arch}"
#fi
# ${setarch} \
RPMBUILD=(
${setarch} \
/usr/bin/rpmbuild \
--define "vendor Icinga.com" \
--define "dist $dist" \
......@@ -135,16 +135,17 @@ rpmbuild() {
find_compilers() {
local location=${1:-/usr/bin}
cd "$location"
cd "$location" || return 1
ls {cc,cpp,[gc]++,gcc}{,-*} 2>/dev/null || true
}
# repair/prepare ccache (needed on some distros like CentOS 5 + 6, SUSE, OpenSUSE)
preconfigure_ccache() {
CCACHE_LINKS=`rpm -E %_libdir`/ccache
compilers=($(find_compilers))
CCACHE_LINKS="$(rpm -E %_libdir)"/ccache
IFS=" " read -r -a compilers <<< "$(find_compilers)"
if [ -e /opt/rh/devtoolset-2/enable ]; then
compilers+=($(find_compilers /opt/rh/devtoolset-2/root/usr/bin))
IFS=" " read -r -a extra_compilers <<< "$(find_compilers /opt/rh/devtoolset-2/root/usr/bin)"
compilers+=("${extra_compilers[@]}")
fi
sudo sh -ex <<CCACHEREPAIR
......@@ -171,13 +172,13 @@ SUDOSCRIPT
fi
}
: ${ICINGA_BUILD_PROJECT:=`detect_project`}
: ${ICINGA_BUILD_OS:=`detect_os`}
: ${ICINGA_BUILD_DIST:=`detect_dist`}
: ${ICINGA_BUILD_ARCH:=`detect_arch`}
: ${ICINGA_BUILD_TYPE:="release"}
: ${ICINGA_BUILD_UPSTREAM_BRANCH:="master"}
: ${ICINGA_BUILD_IGNORE_LINT:=1}
: "${ICINGA_BUILD_PROJECT:="$(detect_project)"}"
: "${ICINGA_BUILD_OS:="$(detect_os)"}"
: "${ICINGA_BUILD_DIST:="$(detect_dist)"}"
: "${ICINGA_BUILD_ARCH:="$(detect_arch)"}"
: "${ICINGA_BUILD_TYPE:="release"}"
: "${ICINGA_BUILD_UPSTREAM_BRANCH:="master"}"
: "${ICINGA_BUILD_IGNORE_LINT:=1}"
[ -n "${ICINGA_NO_ENV}" ] || print_build_env
......@@ -185,7 +186,8 @@ require_var ICINGA_BUILD_PROJECT ICINGA_BUILD_OS ICINGA_BUILD_DIST ICINGA_BUILD_
export_build_env
export LANG=C
WORKDIR=`pwd`
WORKDIR="$(pwd)"
BUILDDIR='build'
export WORKDIR BUILDDIR
# vi: ts=2 sw=2 expandtab
#!/bin/bash
set -e
source "$(dirname "$(readlink -f "$0")")"/functions-debian.sh
# shellcheck source=functions-debian.sh
. "$(dirname "$(readlink -f "$0")")"/functions-debian.sh
# enabling ccache
echo "[ enabling ccache ]"
......@@ -20,7 +21,7 @@ ccache -z # (--zero-stats)
# use eatmydata if present
eatmydata=
if which eatmydata &>/dev/null; then
if command -v eatmydata >/dev/null; then
eatmydata=eatmydata
fi
......@@ -32,7 +33,7 @@ echo "[ Extracting source to build directory ]"
set -ex
cd "${BUILDDIR}/"
rm -rf "${ICINGA_BUILD_PROJECT}"
dpkg-source -x *.dsc "${ICINGA_BUILD_PROJECT}"
dpkg-source -x ./*.dsc "${ICINGA_BUILD_PROJECT}"
)
echo "[ Installing build dependencies ]"
......@@ -69,5 +70,5 @@ echo "[ Running Lintian ]"
echo "[ Cleanup Build Directory ]"
(
set -ex
rm -rf "${BUILDDIR}/${ICINGA_BUILD_PROJECT}/"
rm -rf "${BUILDDIR:?}/${ICINGA_BUILD_PROJECT}/"
)
......@@ -2,27 +2,28 @@
set -e
SCRIPT_HOME="$(dirname "$(readlink -f "$0")")"
source "${SCRIPT_HOME}"/functions-debian.sh
# shellcheck source=functions-debian.sh
. "${SCRIPT_HOME}"/functions-debian.sh
(
set -ex
rm -f *.version *.tar*
rm -rf "${BUILDDIR}/"
rm -f ./*.version ./*.tar*
rm -rf "${BUILDDIR:?}/"
)
echo "[ git show ]"
GIT_PAGER=cat git show -s || true
GIT_PAGER='cat' git show -s || true
echo
echo "[ git status ]"
GIT_PAGER=cat git status || true
GIT_PAGER='cat' git status || true
echo
# creating project directory
(
set -ex
mkdir "${BUILDDIR}" "${BUILDDIR}/${ICINGA_BUILD_PROJECT}"
cp -r ${ICINGA_BUILD_DEB_FLAVOR}/debian "${BUILDDIR}/${ICINGA_BUILD_PROJECT}"/
cp -r ./"${ICINGA_BUILD_DEB_FLAVOR}"/debian "${BUILDDIR}/${ICINGA_BUILD_PROJECT}"/
)
# download archive
......@@ -39,7 +40,7 @@ else
#export UPSTREAM_GIT_NOUPDATE=1 UPSTREAM_GIT_NOREPO=1
#GIT_DIR=packaging/.git
UPSTREAM_GIT_BRANCH="$ICINGA_BUILD_UPSTREAM_BRANCH" ./get_snapshot
mv -t "${BUILDDIR}/" *.version *.tar*
mv -t "${BUILDDIR}/" ./*.version ./*.tar*
)
fi
......@@ -47,20 +48,20 @@ set -x
# figure out version
if [ "${ICINGA_BUILD_TYPE}" == "release" ] ; then
tarball=$(ls ${BUILDDIR}/${ICINGA_BUILD_PROJECT}_*.orig.tar.*)
tarball=$(ls "${BUILDDIR}/${ICINGA_BUILD_PROJECT}_"*.orig.tar.*)
debian_version=`dpkg-parsechangelog -l"${BUILDDIR}/${ICINGA_BUILD_PROJECT}/debian/changelog" | egrep '^Version:' | cut -f 2- -d ' '`
version=`echo "$debian_version" | cut -d- -f1`
debian_version="$(dpkg-parsechangelog -l"${BUILDDIR}/${ICINGA_BUILD_PROJECT}/debian/changelog" | grep -E '^Version:' | cut -f 2- -d ' ')"
version="$(echo "$debian_version" | cut -d- -f1)"
package_version="${debian_version}.${ICINGA_BUILD_DIST}"
target_dist="icinga-${ICINGA_BUILD_DIST}"
else
# snapshots
version=`cat "${BUILDDIR}/${ICINGA_BUILD_PROJECT}.version" | cut -f1`
git_commit=`cat "${BUILDDIR}/${ICINGA_BUILD_PROJECT}.version" | cut -f2`
tarball=`cat "${BUILDDIR}/${ICINGA_BUILD_PROJECT}.version" | cut -f3`
version="$(cut -f1 "${BUILDDIR}/${ICINGA_BUILD_PROJECT}.version")"
git_commit="$(cut -f2 "${BUILDDIR}/${ICINGA_BUILD_PROJECT}.version")"
tarball="$(cut -f3 "${BUILDDIR}/${ICINGA_BUILD_PROJECT}.version")"
build_suffix=".`date +%Y%m%d.%H%M`+${ICINGA_BUILD_DIST}"
build_suffix=".$(date +%Y%m%d.%H%M)+${ICINGA_BUILD_DIST}"
package_version="${version}${build_suffix}-0"
target_dist="icinga-${ICINGA_BUILD_DIST}-${ICINGA_BUILD_TYPE}"
......@@ -128,5 +129,5 @@ echo "[ Running Lintian ]"
echo "[ Cleanup Build Directory ]"
(
set -ex
rm -rf "${BUILDDIR}/${ICINGA_BUILD_PROJECT}/"
rm -rf "${BUILDDIR:?}/${ICINGA_BUILD_PROJECT}/"
)
#!/bin/bash
set -e
source "$(dirname "$(readlink -f "$0")")"/functions-debian.sh
# shellcheck source=functions-debian.sh
. "$(dirname "$(readlink -f "$0")")"/functions-debian.sh
(
set -ex
......
......@@ -3,10 +3,10 @@
set -e
## config
: ${ICINGA_DOCKER_REGISTRY:="registry.icinga.com"}
: ${ICINGA_DOCKER_ORG:="build-docker"}
: ${ICINGA_DOCKER_PULL:=1}
: ${ICINGA_SCRIPT_DEVEL:=0}
: "${ICINGA_DOCKER_REGISTRY:="registry.icinga.com"}"
: "${ICINGA_DOCKER_ORG:="build-docker"}"
: "${ICINGA_DOCKER_PULL:=1}"
: "${ICINGA_SCRIPT_DEVEL:=0}"
SCRIPT_HOME="$(dirname "$(readlink -f "$0")")"
......@@ -46,8 +46,8 @@ if [ "${ICINGA_SCRIPT_DEVEL}" -eq 1 ]; then
dockeropt+=(-v "${SCRIPT_HOME}":/usr/local/bin:ro)
fi
env_file=`mktemp`
trap "rm -f ${env_file}" EXIT INT
env_file="$(mktemp)"
trap 'rm -f ${env_file}' EXIT INT
env | grep -e ^ICINGA > "${env_file}" || true
env | grep -e ^APTLY >> "${env_file}" || true
......@@ -56,7 +56,7 @@ echo "[ Running build in Docker ]"
set -ex
docker run -i --rm "${dockeropt[@]}" \
--env-file "${env_file}" \
-v `pwd`:/tmp/build \
-v "$(pwd)":/tmp/build \
-v "$HOME/.netrc:/home/jenkins/.netrc:ro" \
-v "$HOME/.curlrc:/home/jenkins/.curlrc:ro" \
--workdir /tmp/build \
......
......@@ -2,7 +2,7 @@
set -e
: ${ICINGA_SCRIPT_DEVEL:=0}
: "${ICINGA_SCRIPT_DEVEL:=0}"
if [ "${ICINGA_SCRIPT_DEVEL}" -eq 0 ]; then
echo "Updating build scripts..."
......
#!/bin/bash
# detecting files
FILES_SPEC="$(ls *.spec 2>/dev/null)"
FILES_DEBIAN="$(ls -d */debian 2>/dev/null)"
FILES_SPEC="$(ls ./*.spec 2>/dev/null)"
FILES_DEBIAN="$(ls -d ./*/debian 2>/dev/null)"
if [ -n "${FILES_SPEC}" ]; then
echo "Detected RPM source: ${FILES_SPEC}"
echo
echo "[ running source build ]"
icinga-build-rpm-source
if [ $? -ne 0 ]; then
if ! icinga-build-rpm-source; then
echo "[ source build failed! ]"
exit 1
fi
echo "[ source build completed ]"
echo
echo "[ running binary build ]"
icinga-build-rpm-binary
if [ $? -ne 0 ]; then
if ! icinga-build-rpm-binary; then
echo "[ binary build failed! ]"
exit 1
fi
......@@ -26,8 +24,7 @@ elif [ -n "${FILES_DEBIAN}" ]; then
echo "Detected Debian source files: ${FILES_DEBIAN}"
echo
echo "[ running source build ]"
icinga-build-deb-source
if [ $? -ne 0 ]; then
if ! icinga-build-deb-source; then
echo "[ source build failed! ]"
exit 1
fi
......@@ -35,8 +32,7 @@ elif [ -n "${FILES_DEBIAN}" ]; then
echo
echo "[ running binary build ]"
icinga-build-deb-binary
if [ $? -ne 0 ]; then
if ! icinga-build-deb-binary; then
echo "[ binary build failed! ]"
exit 1
fi
......
#!/bin/bash
set -e
source "$(dirname "$(readlink -f "$0")")"/functions-rpm.sh
# shellcheck source=functions-rpm.sh
. "$(dirname "$(readlink -f "$0")")"/functions-rpm.sh
source_rpm="$(ls "${BUILDDIR}"/SRPMS/*.src.rpm)"
if [ $(echo "${source_rpm}" | wc -l) -gt 1 ]; then
if [ "$(echo "${source_rpm}" | wc -l)" -gt 1 ]; then
echo "More than one spec file found:" >&2
ls -al "${BUILDDIR}/SRPMS" >&2
exit 1
......@@ -39,12 +40,13 @@ case "$ICINGA_BUILD_OS" in
opensuse|sles)
(
set -ex
sudo zypper --non-interactive install `rpm -qpR "${source_rpm}"`
# shellcheck disable=SC2046
sudo zypper --non-interactive install $(rpm -qpR "${source_rpm}")
)
;;
*)
(
if which dnf-builddep &>/dev/null; then
if command -v dnf-builddep >/dev/null; then
set -ex
sudo dnf-builddep -y "${source_rpm}"
else
......
#!/bin/bash
set -e
ICINGA_NO_ENV=1
source "$(dirname "$(readlink -f "$0")")"/functions-rpm.sh
export ICINGA_NO_ENV=1
# shellcheck source=functions-rpm.sh
. "$(dirname "$(readlink -f "$0")")"/functions-rpm.sh
if [ ! -f "${BUILDDIR}/${ICINGA_BUILD_PROJECT}"-package.version ]; then
echo "${BUILDDIR}/${ICINGA_BUILD_PROJECT}-package.version artifact is required!" >&2
exit 1
fi
package_version=$(cat "${BUILDDIR}/${ICINGA_BUILD_PROJECT}"-package.version | tr -d '\r\n')
package_version=$(tr -d '\r\n' "${BUILDDIR}/${ICINGA_BUILD_PROJECT}"-package.version)
versioned=1
# TODO: unused
#versioned=1
if [ "$1" = "-n" ] || [ "$1" = "--no-version" ]; then
shift
versioned=0
# TODO: unused
#versioned=0
fi
packages=()
......
#!/bin/bash
set -e
source "$(dirname "$(readlink -f "$0")")"/functions-rpm.sh
# shellcheck source=functions-rpm.sh
. "$(dirname "$(readlink -f "$0")")"/functions-rpm.sh
(
set -ex
rm -f *.version *.tar*
rm -rf "${BUILDDIR}/" rpmbuild/
rm -f ./*.version ./*.tar*
rm -rf "${BUILDDIR:?}/" rpmbuild/
)
echo "[ git show ]"
GIT_PAGER=cat git show -s || true
GIT_PAGER='cat' git show -s || true
echo
echo "[ git status ]"
GIT_PAGER=cat git status || true
GIT_PAGER='cat' git status || true
echo
(
set -ex
mkdir "${BUILDDIR}"/{,SPECS,SOURCES,BUILD,SRPMS,RPMS}
cp -v *.spec "${BUILDDIR}/SPECS/"
find -maxdepth 1 -type f ! -name "*.spec" -exec cp -vt "${BUILDDIR}/SOURCES/" {} \;
cp -v ./*.spec "${BUILDDIR}/SPECS/"
find . -maxdepth 1 -type f ! -name "*.spec" -exec cp -vt "${BUILDDIR}/SOURCES/" {} \;
)
if ! grep -Pq ^--netrc /etc/rpmdevtools/curlrc; then
......@@ -49,7 +50,7 @@ else
(
set -ex
UPSTREAM_GIT_BRANCH="$ICINGA_BUILD_UPSTREAM_BRANCH" ./get_snapshot
mv -t "${BUILDDIR}/SOURCES/" *.version *.tar*
mv -t "${BUILDDIR}/SOURCES/" ./*.version ./*.tar*
)
echo "[ Building snapshot version ]"
......@@ -57,16 +58,16 @@ else
set -ex
cd "${BUILDDIR}/"
version=`cat SOURCES/"${ICINGA_BUILD_PROJECT}.version" | cut -f1`
revision="0.`date +%Y%m%d.%H%M`"
git_commit=`cat SOURCES/"${ICINGA_BUILD_PROJECT}.version" | cut -f2`
tarball=`cat SOURCES/"${ICINGA_BUILD_PROJECT}.version" | cut -f3`
version="$(cut -f1 SOURCES/"${ICINGA_BUILD_PROJECT}.version")"
revision="0.$(date +%Y%m%d.%H%M)"
git_commit="$(cut -f2 SOURCES/"${ICINGA_BUILD_PROJECT}.version")"
tarball="$(cut -f3 SOURCES/"${ICINGA_BUILD_PROJECT}.version")"
sed -i \
-e "0,/^Version: /{s/Version: .*/Version: ${version}/}" \
-e "0,/^Release: /{s/Release: .*/Release: ${revision}%{?dist}/}" \
-e "0,/^Source0\?: /{s/\(Source0\?\): .*/Source0: ${tarball}/}" \
-e "0,/^%global\s\+\(git_\)\?commit /{s/\(%global\s\+\(git_\)\?commit\).*/%global commit ${git_commit}/}" \
-e '0,/^Source0\?: /{s/\(Source0\?\): .*/Source0: '"${tarball}"'/}' \
-e '0,/^%global\s\+\(git_\)\?commit /{s/\(%global\s\+\(git_\)\?commit\).*/%global commit '"${git_commit}"'/}' \
"SPECS/${ICINGA_BUILD_PROJECT}.spec"
)
fi
......
#!/bin/bash
set -e
source "$(dirname "$(readlink -f "$0")")"/functions-rpm.sh
# shellcheck source=functions-rpm.sh
. "$(dirname "$(readlink -f "$0")")"/functions-rpm.sh
repository="${WORKDIR}/${BUILDDIR}"/RPMS
......
#!/bin/bash
# detecting files
FILES_SPEC="$(ls *.spec 2>/dev/null)"
FILES_DEBIAN="$(ls -d */debian 2>/dev/null)"
FILES_SPEC="$(ls ./*.spec 2>/dev/null)"
FILES_DEBIAN="$(ls -d ./*/debian 2>/dev/null)"
if [ -n "${FILES_SPEC}" ]; then
echo "Detected RPM source: ${FILES_SPEC}"
echo
icinga-build-rpm-test
if [ $? -ne 0 ]; then
if ! icinga-build-rpm-test; then
echo "[ test failed! ]"
exit 1
fi
......@@ -16,8 +15,7 @@ if [ -n "${FILES_SPEC}" ]; then
elif [ -n "${FILES_DEBIAN}" ]; then
echo "Detected Debian source files: ${FILES_DEBIAN}"
echo
icinga-build-deb-test
if [ $? -ne 0 ]; then
if ! icinga-build-deb-test; then
echo "[ test failed! ]"
exit 1
fi
......
#!/bin/sh
set -eu
rc=0
tmp="$(mktemp)"
trap 'rm -f ${tmp}' EXIT INT TERM
find . -maxdepth 1 -type f -perm +1 -or -name \*.sh >"${tmp}"
while IFS= read -r file
do
shebang="$(head -n1 "$file")"
echo "$shebang" | grep -qE "/(ba)?sh" || continue
echo "Checking $file"
if ! shellcheck -x "$file"; then
rc=1
fi
done < "${tmp}"
exit $rc
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment