Skip to content

Commit a5f0a05

Browse files
author
Alexey Tsivunin
committed
12: Add basic CI with linters
We want PRs to have an explicit indicator for linter errors in the branch/PR. It will allow us to see the state of the branch without explicitly running linters locally if we don't want it. In the scope of this task we need to add a basic CI pipeline with linters and status indicator on GitHub. Steps to do: - add `Dockerfile` for the application (we don't want to add `ENTRYPOINT` in it for now, since we suppose that it is better to have container entrypoints inside the `docker-compose` file) - add `docker-compose` file Compose file should have following services: - `forum123-build` (will be used to build application) - `forum123-mypy` (to run `mypy`) - `forum123-flake8` (to run `flake8`) - `forum123-pylint` (to run `pylint`) We want them to be separated on different services to be able to run build in one job, and then run all linters in a parallel way in three different jobs (parallel execution will be implemented sometimes later, now we need only to have different services in compose file). - setup CI pipeline using GitHub Actions Also we want to put this configuration in a separate folder like `envs/dev` to indicate that this is only for development purposes. And when we will need to add some production infrastructure it will go into `envs/prod`. Seems like using different folders is more convenient than having a bunch of Dockerfile's and docker-compose files with suffixes like `.dev` and `.prod`. We decided to put mypy stubs in `requirements-dev.txt`, because we had a troubles with installing types on CI. In our case we had to run `mypy` twice - first time, to populate `mypy`'s cache and define what types are missing, and second time to install types and check for errors. The cause of this isssue is that github always run its jobs in a new clean containers, so each new `mypy` run will not have `mypy`'s cache from the previous run. Therefore we had to run `mypy` twice for every job to have type stubs installed. More about related problems with missing type stubs on CI from other people you can read here: python/mypy#10600 Worth to mention that now we're not going to make this pipeline optimized by performance. We are planning to add caching of intermediate docker layers later in the scope of another task. For now we just need this pipeline to work and nothing more.
1 parent 61f072c commit a5f0a05

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

.github/workflows/lint.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
on: push
2+
jobs:
3+
lint:
4+
name: lint
5+
runs-on: ubuntu-latest
6+
steps:
7+
- name: Check out code
8+
uses: actions/checkout@v3
9+
10+
- name: Build linters and application images
11+
run: |
12+
docker-compose -f envs/dev/docker-compose.yml build forum123-build
13+
docker-compose -f envs/dev/docker-compose.yml build forum123-mypy
14+
docker-compose -f envs/dev/docker-compose.yml build forum123-flake8
15+
docker-compose -f envs/dev/docker-compose.yml build forum123-pylint
16+
17+
- name: Run mypy
18+
run: docker-compose -f envs/dev/docker-compose.yml run forum123-mypy
19+
20+
- name: Run flake8
21+
run: docker-compose -f envs/dev/docker-compose.yml run forum123-flake8
22+
23+
- name: Run pylint
24+
run: docker-compose -f envs/dev/docker-compose.yml run forum123-pylint

envs/dev/Dockerfile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM python:3.10-slim
2+
3+
WORKDIR /forum123
4+
5+
# Setup and activate python3.10 virtual environment. It will allow us to avoid this warning from pip:
6+
# WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system
7+
# package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
8+
RUN python3.10 -m venv .venv
9+
ENV PATH="/forum123/.venv/bin:$PATH"
10+
11+
COPY requirements.txt requirements-dev.txt ./
12+
13+
RUN apt-get update \
14+
&& pip install --upgrade pip \
15+
&& pip install -r requirements.txt \
16+
-r requirements-dev.txt
17+
18+
COPY . ./
19+
20+
ENV FLASK_APP=forum123

envs/dev/docker-compose.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version: "3"
2+
3+
services:
4+
5+
forum123-build: &forum123-build
6+
build:
7+
context: ../.. # path from the current file to the project root directory
8+
dockerfile: envs/dev/Dockerfile # path from the project root directory to the Dockerfile
9+
10+
forum123-mypy:
11+
<<: *forum123-build
12+
entrypoint: mypy
13+
14+
forum123-flake8:
15+
<<: *forum123-build
16+
entrypoint: flake8
17+
18+
forum123-pylint:
19+
<<: *forum123-build
20+
entrypoint: pylint src

requirements-dev.txt

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Development dependencies. Please keep this list in alphabetical order.
2+
# Note that mypy type stubs listed in a separate section below.
23

34
darglint==1.8.1 # Checks whether a docstring's description matches the actual function/method implementation
45
dlint==0.12.0 # Tool for encouraging best coding practices and helping ensure Python code is secure.
@@ -28,3 +29,8 @@ flake8-type-checking==1.0.3 # Plugin for managing type-checking imports & forwa
2829
flake8==3.9.2
2930
mypy==0.991
3031
pylint==2.15.9
32+
33+
34+
# Mypy types. Please keep this list in alphabetical order.
35+
36+
types-cryptography==3.3.23.2

0 commit comments

Comments
 (0)