From 5535197a4888c821957742f6897d4915226ca570 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20Jedlicska?= <57442769+gjedlicska@users.noreply.github.com> Date: Thu, 11 Aug 2022 13:49:47 +0200 Subject: [PATCH] gergo/pre commit (#906) * Adds hadolint as pre-commit * Addresses all hadolint comments * Hadolint docker works when entrypoint explicitly provided * Use noninteractive apt-get frontend and clean after install * build(circleci): filters for pre-commit should be same as for test-server * remove cache prefix as not currently necessary due to pre-commit-config.yaml changing * build(circleci): enable remote docker for pre-commit * build(circleci): use speckle pre-commit runner with built-in hadolint * build(server): dockerfile RUN statements are consolidated Each RUN statement in a Dockerfile creates a new layer. Hadolint rule DL3059 suggests they should be consolidated. * build(server): dockerfile RUN statements are consolidated Each RUN statement in a Dockerfile creates a new layer. Hadolint rule DL3059 suggests they should be consolidated. * Improve husky bash script to catch errors * Integrates pre-commit with husky * pre-commit should now be run by husky on every commit * pre-commit which requires additional installed dependencies is moved to separate file * Update README for revised developer instructions * Updates pre-commit yarn script * refactor(pre-commit): make everyone happy with loosly integrating husky and pre-commit scripts * chore(clean up pre-commit configs): clean some more pre-commit mess * chore(pre-commit): run pre-commit in ci too * fix(husky pre-commit): fix ci build husky invocation, the script is not commited to git * fix(circleci config): install yarn packages for linting in pre-commit * fix(pre-commit): fix shellcheck disable comment placement * fix(pre-commit): add shellcheck ignore * fix(pre-commit husyk): fix shellcheck ignore version Co-authored-by: Iain Sproat <68657+iainsproat@users.noreply.github.com> --- .circleci/config.yml | 19 +++++++++++--- .husky/pre-commit | 40 +++++++++++++++++++++--------- .pre-commit-config.deployment.yaml | 21 ---------------- .pre-commit-config.yaml | 37 +++++++++++---------------- package.json | 1 - 5 files changed, 57 insertions(+), 61 deletions(-) delete mode 100644 .pre-commit-config.deployment.yaml diff --git a/.circleci/config.yml b/.circleci/config.yml index 1067eb9d9..bdbdeea29 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -211,12 +211,23 @@ jobs: key: cache-pre-commit-<>-{{ checksum "<>" }} paths: - ~/.cache/pre-commit + - restore_cache: + name: Restore Yarn Package Cache + keys: + - yarn-packages-{{ checksum "yarn.lock" }} + - run: + name: Install Dependencies + command: yarn + + - save_cache: + name: Save Yarn Package Cache + key: yarn-packages-{{ checksum "yarn.lock" }} + paths: + - .yarn/cache + - .yarn/unplugged - run: name: Run pre-commit - command: pre-commit run --all-files --config <> - - run: - name: Run deployment pre-commit - command: pre-commit run --all-files --config <> + command: ./.husky/pre-commit - run: command: git --no-pager diff name: git diff diff --git a/.husky/pre-commit b/.husky/pre-commit index 1363567bd..8cddd4ca6 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,16 +1,32 @@ -#!/usr/bin/env bash -set -eo pipefail -[ -n "$CI" ] && exit 0 +#!/usr/bin/env sh +# shellcheck disable=SC1091 +set -e -#shellcheck source=/dev/null -. "$(dirname "$0")/_/husky.sh" +if [ -n "$CI" ] +then + echo "running eslint" + yarn lint + yarn prettier:check +else +# shellcheck disable=SC1090 + . "$(dirname "$0")/_/husky.sh" + yarn lint-staged +fi -yarn lint-staged -if ! command -v pre-commit &> /dev/null; then exit 0; fi -echo "🔍 Detected pre-commit on this system. Running pre-commit checks..." -pre-commit run --all-files --config .pre-commit-config.yaml +echo "🔍 looking for additional linter dependencies" -if ! command -v hadolint &> /dev/null || ! command -v helm &> /dev/null || ! command -v shellcheck &> /dev/null; then exit 0; fi -echo "🔍 Detected additional dependencies (hadolint, helm, and shellcheck) on this system. Running additional pre-commit checks..." -pre-commit run --all-files --config .pre-commit-config.deployment.yaml +check_dependencies_available() { + for i in "${@}" + do + if ! command -v "${i}"; then + echo "No ${i} executable found skipping additional checks" >&2 + exit 0 + fi + done +} + +check_dependencies_available pre-commit hadolint helm shellcheck + +echo "All systems functional, running additional pre-commit checks..." +pre-commit run --all-files diff --git a/.pre-commit-config.deployment.yaml b/.pre-commit-config.deployment.yaml deleted file mode 100644 index 23f2a21aa..000000000 --- a/.pre-commit-config.deployment.yaml +++ /dev/null @@ -1,21 +0,0 @@ -# pre-commit for deployment related resources -# e.g. shell files, dockerfiles, helm chart etc.. -repos: - - repo: https://github.com/hadolint/hadolint - rev: 'v2.10.0' - hooks: - - id: hadolint - - # Cannot use official repo as it relies on Docker, which cannot be supported by either pre-commit.ci or CircleCI - - repo: https://github.com/Jarmos-san/shellcheck-precommit - rev: 'v0.2.0' - hooks: - - id: shellcheck-system - - - repo: https://github.com/gruntwork-io/pre-commit - rev: 'v0.1.17' - hooks: - - id: helmlint - -ci: - autoupdate_schedule: quarterly diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index aeda0f90d..9e7e912a6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,29 +1,20 @@ -# default pre-commit file, checks node.js code and basic file formatting +# pre-commit for deployment related resources +# e.g. shell files, dockerfiles, helm chart etc.. repos: - - repo: https://github.com/pre-commit/mirrors-eslint - rev: 'v8.19.0' # Use the sha / tag you want to point at + - repo: https://github.com/hadolint/hadolint + rev: 'v2.10.0' hooks: - - id: eslint - types: [file] - files: \.[jt]s$|vue$ # *.js, *.ts and vue - exclude: '(\/|^)((generated\/.*)|(\..*\.([jt]sx?|vue)))$' - args: - - '--max-warnings=0' - additional_dependencies: - - eslint@8.11.0 - - eslint-config-prettier@8.5.0 - - eslint-plugin-vue@8.5.0 - - '@babel/eslint-parser@7.18.2' - - '@babel/preset-env@t 7.16.11' - - '@typescript-eslint/eslint-plugin@5.21.0' - - '@typescript-eslint/parser@5.21.0' - - typescript@4.5.4 - - '@rushstack/eslint-patch@1.1.3' - - '@vue/eslint-config-typescript@11.0.0' + - id: hadolint + # Cannot use official repo as it relies on Docker, which cannot be supported by either pre-commit.ci or CircleCI + - repo: https://github.com/Jarmos-san/shellcheck-precommit + rev: 'v0.2.0' + hooks: + - id: shellcheck-system - - repo: https://github.com/pre-commit/mirrors-prettier - rev: 'v2.7.1' # Use the sha / tag you want to point at + - repo: https://github.com/gruntwork-io/pre-commit + rev: 'v0.1.17' hooks: - - id: prettier + - id: helmlint + ci: autoupdate_schedule: quarterly diff --git a/package.json b/package.json index 1907f4dd6..0bc20cff4 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,6 @@ "lint": "eslint . --ext .js,.ts,.vue --max-warnings=0", "prettier:check": "prettier --check .", "prettier:fix": "prettier --write .", - "pre-commit": "pre-commit run --all-files --config .pre-commit-config.yaml && pre-commit run --all-files --config .pre-commit-config.deployment.yaml", "circleci:check": "circleci config validate ./.circleci/config.yml", "dev:docker:up": "docker-compose -f ./docker-compose-deps.yml -f ./docker-compose-dev.yml up -d", "dev:docker:down": "docker-compose -f ./docker-compose-deps.yml -f ./docker-compose-dev.yml down",