From e2e1399cd7d07a966b2ee1c9fb659cd9a77f5c70 Mon Sep 17 00:00:00 2001 From: Romain Date: Sat, 11 Jul 2020 17:06:00 +0200 Subject: [PATCH 1/3] pre-commit hook added + minor fixes ## pre-commit pre-commit added to check - `bash` files with bashate see #1109 - `yaml` files with yamlint see #1108 - `docker` files with hadolint see #1100 # hadolint version of the linter added as a variable in the `Makefile` --- .pre-commit-config.yaml | 27 +++++++++++++++++++++++++++ Makefile | 3 ++- requirements-dev.txt | 2 +- 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..4c89f16d --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,27 @@ +--- +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.1.0 + hooks: + #- id: flake8 + - id: check-yaml + files: .*\.(yaml|yml)$ + - repo: https://github.com/adrienverge/yamllint.git + rev: v1.23.0 + hooks: + - id: yamllint + files: \.(yaml|yml)$ + exclude: ^.travis + - repo: https://github.com/openstack-dev/bashate.git + rev: 2.0.0 + hooks: + - id: bashate + - repo: local + hooks: + - id: hadolint + name: Hadolint linter + description: Runs Hadolint to check for Dockerfile best practices + language: system + types: + - dockerfile + entry: make lint-all diff --git a/Makefile b/Makefile index 5ab9caf1..f5c80f74 100644 --- a/Makefile +++ b/Makefile @@ -26,6 +26,7 @@ ALL_IMAGES:=$(ALL_STACKS) # Linter HADOLINT="${HOME}/hadolint" +HADOLINT_VERSION="v1.18.0" help: # http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html @@ -88,7 +89,7 @@ lint-build-test-all: $(foreach I,$(ALL_IMAGES),lint/$(I) arch_patch/$(I) build/$ lint-install: ## install hadolint @echo "Installing hadolint at $(HADOLINT) ..." - @curl -sL -o $(HADOLINT) "https://github.com/hadolint/hadolint/releases/download/v1.18.0/hadolint-$(shell uname -s)-$(shell uname -m)" + @curl -sL -o $(HADOLINT) "https://github.com/hadolint/hadolint/releases/download/$(HADOLINT_VERSION)/hadolint-$(shell uname -s)-$(shell uname -m)" @chmod 700 $(HADOLINT) @echo "Installation done!" @$(HADOLINT) --version diff --git a/requirements-dev.txt b/requirements-dev.txt index e51280fb..5f47c68d 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,4 +1,5 @@ docker +pre-commit pytest recommonmark==0.5.0 requests @@ -6,4 +7,3 @@ sphinx>=1.6 sphinx-intl tabulate transifex-client - From 17cdb90fac982d21ab3d5be870f95cf2c3798ad1 Mon Sep 17 00:00:00 2001 From: Romain Date: Tue, 21 Jul 2020 21:45:36 +0200 Subject: [PATCH 2/3] Changes following review - Remove inconsistency of callink make lint-all from the pre-commit hookj - Remove comment in pre-commit conf - Add specific targets in makefile to install and run pre-commit - Change yamlint default conf to avoid line too long (for example on travis file) --- .pre-commit-config.yaml | 5 ++--- Makefile | 9 ++++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4c89f16d..58901750 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,15 +3,14 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v3.1.0 hooks: - #- id: flake8 - id: check-yaml files: .*\.(yaml|yml)$ - repo: https://github.com/adrienverge/yamllint.git rev: v1.23.0 hooks: - id: yamllint + args: ['-d {extends: relaxed, rules: {line-length: disable}}', '-s'] files: \.(yaml|yml)$ - exclude: ^.travis - repo: https://github.com/openstack-dev/bashate.git rev: 2.0.0 hooks: @@ -24,4 +23,4 @@ repos: language: system types: - dockerfile - entry: make lint-all + entry: hadolint diff --git a/Makefile b/Makefile index f5c80f74..53e6285c 100644 --- a/Makefile +++ b/Makefile @@ -75,7 +75,7 @@ dev/%: ## run a foreground container for a stack docker run -it --rm -p $(PORT):8888 $(DARGS) $(OWNER)/$(notdir $@) $(ARGS) dev-env: ## install libraries required to build docs and run tests - pip install -r requirements-dev.txt + @pip install -r requirements-dev.txt lint/%: ARGS?= lint/%: ## lint the dockerfile(s) for a stack @@ -118,6 +118,13 @@ n-docs-diff: ## number of docs/ files changed since branch from master n-other-diff: ## number of files outside docs/ changed since branch from master @git diff --name-only $(DIFF_RANGE) -- ':!docs/' | wc -l | awk '{print $$1}' +pre-commit-all: ## run pre-commit hook on all files + @pre-commit run --all + +pre-commit-install: ## set up the git hook scripts + @pre-commit --version + @pre-commit install + pull/%: DARGS?= pull/%: ## pull a jupyter image docker pull $(DARGS) $(OWNER)/$(notdir $@) From bf39616d866fdd10f1a0d45e2287c8c8becbb3cb Mon Sep 17 00:00:00 2001 From: Romain Date: Fri, 24 Jul 2020 05:14:49 +0200 Subject: [PATCH 3/3] documentation --- docs/contributing/lint.md | 50 +++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/docs/contributing/lint.md b/docs/contributing/lint.md index 7ce07bdd..2d3bed17 100644 --- a/docs/contributing/lint.md +++ b/docs/contributing/lint.md @@ -1,8 +1,43 @@ -# Image Lint +# Lint + +In order to enforce some rules **linters** are used in this project. +Linters can be run either during the **development phase** (by the developer) and during **integration phase** (by Travis). +To integrate and enforce this process in the project lifecycle we are using **git hooks** through [pre-commit][pre-commit]. + +## Pre-commit hook + +### Installation + +pre-commit is a Python package that needs to be installed. +This can be achieved by using the generic task used to install all Python development dependencies. + +```sh +# Install all development dependencies for the project +$ make dev-env +# It can also be installed directly +$ pip install pre-commit +``` + +Then the git hooks scripts configured for the project in `.pre-commit-config.yaml` need to be installed in the local git repository. + +```sh +$ make pre-commit-install +``` + +### Run + +Now pre-commit (and so configured hooks) will run automatically on `git commit` on each changed file. +However it is also possible to trigger it against all files. + +```sh +$ make pre-commit-all +``` + +## Image Lint To comply with [Docker best practices][dbp], we are using the [Hadolint][hadolint] tool to analyse each `Dockerfile` . -## Installation +### Installation There is a specific `make` target to install the linter. By default `hadolint` will be installed in `${HOME}/hadolint`. @@ -15,9 +50,9 @@ $ make lint-install # Haskell Dockerfile Linter v1.17.6-0-gc918759 ``` -## Lint +### Linting -### Per Stack +#### Per Stack The linter can be run per stack. @@ -41,7 +76,7 @@ Optionally you can pass arguments to the linter. $ make lint/scipy-notebook ARGS="--format codeclimate" ``` -### All the Stacks +#### All the Stacks The linter can be run against all the stacks. @@ -49,7 +84,7 @@ The linter can be run against all the stacks. $ make lint-all ``` -## Ignoring Rules +### Ignoring Rules Sometimes it is necessary to ignore [some rules][rules]. The following rules are ignored by default and sor for all images in the `.hadolint.yaml` file. @@ -75,4 +110,5 @@ RUN cd /tmp && echo "hello!" [dbp]: https://docs.docker.com/develop/develop-images/dockerfile_best-practices [rules]: https://github.com/hadolint/hadolint#rules [DL3006]: https://github.com/hadolint/hadolint/wiki/DL3006 -[DL3008]: https://github.com/hadolint/hadolint/wiki/DL3008 \ No newline at end of file +[DL3008]: https://github.com/hadolint/hadolint/wiki/DL3008 +[pre-commit]: https://pre-commit.com/ \ No newline at end of file