Skip to content

Commit 21dcf7a

Browse files
committed
CI: run pre-commit hooks in CI
This also means flake8 will output to stdout in CI now. It didn't before because it output to an XML format for publishing. Pre-commit creates its own environment in which it installs hooks. To speed up runs, the pipeline will cache this for use with future jobs. The cache will update if .pre-commit-config.yaml changes. The flake8 pre-commit hook invokes flake8 via `pipenv run flake8`. It's normally useful to use pipenv here cause it ensures flake8 is invoked within the context of the venv. However, in CI, there is no venv - dependencies are installed directly to the system site-packages. `pipenv run` does not work in such case because it tries to create a new venv if one doesn't exist (it doesn't consider the system interpreter to be a venv). This workaround (okay, it's a hack) creates an executable shell script which replaces the original pipenv binary. The shell script simply ignores the first argument (i.e. ignores `run` in `pipenv run`) and executes the rest of the arguments as a command. It essentially makes `pipenv run flake8` equivalent to just having ran `flake8`. When pre-commit executes pipenv, the aforementioned script is what will run.
1 parent 5ed6e71 commit 21dcf7a

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

azure-pipelines.yml

+24-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ jobs:
88

99
variables:
1010
PIP_CACHE_DIR: .cache/pip
11+
PRE_COMMIT_HOME: $(Pipeline.Workspace)/pre-commit-cache
1112

1213
steps:
1314
- task: UsePythonVersion@0
1415
displayName: 'Set Python Version'
16+
name: PythonVersion
1517
inputs:
1618
versionSpec: '3.7.x'
1719
addToPath: true
@@ -31,8 +33,28 @@ jobs:
3133
pip install flake8-formatter-junit-xml
3234
displayName: 'Install Project Environment'
3335
34-
- script: flake8 --format junit-xml --output-file TEST-lint.xml
35-
displayName: 'Run Linter'
36+
# Create an executable shell script which replaces the original pipenv binary.
37+
# The shell script ignores the first argument and executes the rest of the args as a command.
38+
# It makes the `pipenv run flake8` command in the pre-commit hook work by circumventing
39+
# pipenv entirely, which is too dumb to know it should use the system interpreter rather than
40+
# creating a new venv.
41+
- script: |
42+
printf '%s\n%s' '#!/bin/bash' '"${@:2}"' > $(PythonVersion.pythonLocation)/bin/pipenv \
43+
&& chmod +x $(PythonVersion.pythonLocation)/bin/pipenv
44+
displayName: 'Mock pipenv binary'
45+
46+
- task: Cache@2
47+
displayName: 'Restore pre-commit environment'
48+
inputs:
49+
key: pre-commit | "$(PythonVersion.pythonLocation)" | .pre-commit-config.yaml
50+
restoreKeys: |
51+
pre-commit | "$(PythonVersion.pythonLocation)"
52+
path: $(PRE_COMMIT_HOME)
53+
54+
# flake8 runs so it can generate the XML output. pre-commit will run it again to show stdout.
55+
# flake8 standalone runs first to avoid any fixes pre-commit hooks may make.
56+
- script: flake8 --format junit-xml --output-file TEST-lint.xml; pre-commit run --all-files
57+
displayName: 'Run pre-commit hooks'
3658

3759
- script: |
3860
python3 manage.py makemigrations --check

0 commit comments

Comments
 (0)