Skip to content

Commit 1ba2c68

Browse files
committed
ci: use pre-commit framework to run file checks & fix offenses
1 parent f2ab442 commit 1ba2c68

13 files changed

+208
-179
lines changed

.circleci/config.yml

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---
12
# https://circleci.com/docs/docker
23
version: 2
34
jobs:
@@ -9,6 +10,11 @@ jobs:
910
steps:
1011
- checkout
1112

13+
- run:
14+
name: Bootstrap the devenv
15+
command: |
16+
ci/bootstrap
17+
1218
- run:
1319
name: Build the image
1420
command: |
@@ -21,6 +27,7 @@ jobs:
2127
2228
- deploy:
2329
name: Deploy from master branch
30+
# yamllint disable rule:line-length
2431
command: |
2532
if [[ ${CIRCLE_BRANCH} = master ]] && [[ -z ${CIRCLE_PR_NUMBER} ]]; then
2633
ci/publish

.pre-commit-config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/.pre-commit-config.yaml

ci/bootstrap

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/bash
2+
set -eEu
3+
set -o pipefail
4+
5+
. src/bootstrap
6+
7+
main() {
8+
mkbin
9+
install_shellcheck
10+
install_shfmt
11+
}
12+
13+
mkbin() {
14+
mkdir -p ~/bin || :
15+
if ! grep -E ':~/bin[:$]' <<<"$(printenv PATH)" &>/dev/null; then
16+
export PATH="${PATH}:~/bin"
17+
fi
18+
}
19+
20+
install_shellcheck() {
21+
if ! command -v shellcheck; then
22+
# Install latest statically-linked version of
23+
# https://github.com/koalaman/shellcheck
24+
local -r tarball="shellcheck-latest.linux.x86_64.tar.xz"
25+
26+
cd /tmp
27+
curl -L -ssL -O https://storage.googleapis.com/shellcheck/${tarball}
28+
tar xvJf ${tarball}
29+
cp /tmp/shellcheck-latest/shellcheck ~/bin/
30+
rm -fr /tmp/shellcheck*
31+
fi
32+
}
33+
34+
install_shfmt() {
35+
if ! command -v shfmt; then
36+
# Install statically-linked version of shfmt from
37+
# https://github.com/mvdan/sh
38+
local -r SHFMT_VERSION="v2.3.0"
39+
curl -L -ssL -o ~/bin/shfmt \
40+
"https://github.com/mvdan/sh/releases/download/${SHFMT_VERSION}/shfmt_${SHFMT_VERSION}_linux_amd64"
41+
chmod 0755 ~/bin/shfmt
42+
fi
43+
}
44+
45+
main

ci/build

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
#!/bin/bash
2-
set -e
3-
set -u
4-
5-
. ci/functions.sh
6-
verbosity=1
2+
set -eEu
3+
set -o pipefail
74

85
echo
96
echo Remove mb container if it has been created.
10-
docker rm mb &> /dev/null || :
7+
docker rm mb &>/dev/null || :
118

129
echo
1310
echo Build mb image.
@@ -24,4 +21,4 @@ docker-compose build
2421

2522
echo
2623
echo Show images.
27-
docker images | egrep 'jumanjiman/cci\b'
24+
docker images | grep -E 'jumanjiman/cci\b'

ci/check-files

-115
This file was deleted.

ci/functions.sh

-29
This file was deleted.

ci/publish

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
#!/bin/bash
2-
set -e
3-
set -u
4-
5-
. ci/functions.sh
6-
verbosity=1
2+
set -eEu
3+
set -o pipefail
74

85
BUILD_DATE=$(date +%Y%m%dT%H%M)
96
VCS_REF=$(git describe --abbrev=7 --tags --always)
107
TAG=${BUILD_DATE}-git-${VCS_REF}
118

12-
docker login -u ${user} -p ${pass}
13-
docker tag jumanjiman/cci jumanjiman/cci:${TAG}
14-
docker push jumanjiman/cci:${TAG}
9+
# shellcheck disable=SC2154
10+
docker login -u "${user}" -p "${pass}"
11+
docker tag jumanjiman/cci "jumanjiman/cci:${TAG}"
12+
docker push "jumanjiman/cci:${TAG}"
1513
docker push jumanjiman/cci:latest
1614
docker logout

ci/test

+28-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
#!/bin/bash
2-
set -e
3-
set -u
2+
set -eEu
3+
set -o pipefail
44

5-
. ci/functions.sh
5+
. ci/bootstrap
66

7-
echo
8-
echo Run CI tests.
9-
docker-compose run ci
7+
main() {
8+
run_ci
9+
run_precommit
10+
}
1011

11-
echo
12-
echo Check for thinkgs like trailing whitespace.
13-
# Hint: -v1 shows just the names of offending files.
14-
# -v2 shows lines with trailing whitespace.
15-
ci/check-files -v1
12+
run_ci() {
13+
echo
14+
echo Run CI tests.
15+
docker-compose run ci
16+
}
17+
18+
run_precommit() {
19+
echo
20+
echo Run various file checks.
21+
22+
# http://pre-commit.com/#pre-commit-run
23+
readonly DEFAULT_PRECOMMIT_OPTS="--all-files --verbose"
24+
25+
# Allow user to override our defaults by setting an env var.
26+
readonly PRECOMMIT_OPTS="${PRECOMMIT_OPTS:-$DEFAULT_PRECOMMIT_OPTS}"
27+
28+
# shellcheck disable=SC2086
29+
/usr/bin/time pre-commit run ${PRECOMMIT_OPTS}
30+
}
31+
32+
main

docker-compose-mb.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---
12
version: '2.1'
23

34
services:

docker-compose.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---
12
version: '2.1'
23

34
services:

src/.pre-commit-config.yaml

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
# This file configures https://pre-commit.com/
3+
# to use specific hooks and options.
4+
5+
fail_fast: false
6+
7+
repos:
8+
- repo: https://github.com/jumanjihouse/pre-commit-hooks
9+
sha: 1.5.1
10+
hooks:
11+
- id: forbid-binary
12+
- id: git-check # Configure in .gitattributes
13+
- id: git-dirty # Configure in .gitignore
14+
- id: shellcheck
15+
- id: shfmt
16+
17+
- repo: https://github.com/adrienverge/yamllint.git
18+
sha: v1.11.1
19+
hooks:
20+
- id: yamllint
21+
args: ['--format', 'parsable', '--strict']
22+
23+
- repo: https://github.com/pre-commit/pre-commit-hooks
24+
sha: v1.2.3
25+
hooks:
26+
- id: check-added-large-files
27+
- id: check-case-conflict
28+
- id: check-executables-have-shebangs
29+
- id: check-json
30+
- id: detect-private-key
31+
32+
- repo: https://github.com/Lucas-C/pre-commit-hooks
33+
sha: v1.1.5
34+
hooks:
35+
- id: forbid-crlf
36+
- id: forbid-tabs
37+
exclude: >
38+
(?x)^(
39+
Makefile|
40+
.*\.go
41+
)$
42+
43+
- repo: https://github.com/chriskuehl/puppet-pre-commit-hooks.git
44+
sha: v2.0.1
45+
hooks:
46+
- id: puppet-validate
47+
- id: erb-validate
48+
- id: epp-validate
49+
- id: puppet-lint
50+
args:
51+
- --fail-on-warnings
52+
- --no-documentation-check

0 commit comments

Comments
 (0)