The software building automation starts at the developer workstation.
-
Follow the project's README.md hello_world to run locally and test the project. Our README.md is very basic.
-
Create a file with name
Makefile
, an input file formake
:touch Makefile
make
is the most commonly used tool for automating common tasks for your project. -
We should now wrap commands that we execute often working with our project - installing dependencies, running the app locally, linting and testing. Start with:
.PHONY: deps test deps: pip install -r requirements.txt; \ pip install -r test_requirements.txt
Notice: we use tabs in
Makefile
. What does.PHONE
?You should add:
test: # here put the command for testing from README.md run: # command to run the app locally lint: # command to execute flake8 from README.md
-
Test each of the
Makefile
targets. -
Create a git repository for the hello_world application in your github account, e.g., python_simple_ci.
-
Please do not foget about adding a
.gitignore
, you could start with:# Byte-compiled / optimized __pycache__/ *.py[cod] # venv .venv venv/
-
Push all the code to the github repository.
-
You github repository should look like that:
|- hello_world/ |- test/ |- main.py |- Makefile |- README.md |- requirements.txt \- test_requirements.txt
We will show how to build a Continuous Integration pipeline with Github Actions. Basic concepts after docs:
- Event - trigger workflows
- Workflow -
.github/workflows
- Jobs
- Steps
- Actions - reusable complex task
-
Create in the root of your project a directory -
.github/workflows
:mkdir -p .github/workflows
-
Let's create the first automation -
.github/workflows/ci.yaml
(after github docs):name: Package Project # Controls when the workflow will run on: [ push ] jobs: build: runs-on: ubuntu-latest steps: # get the code under $GITHUB_WORKSPACE directory - uses: actions/checkout@v2 # get the python - name: Set up Python 3 uses: actions/setup-python@v3 - name: Install deps run: pip3 install -r requirements.txt
-
Push the workflow file
ci.yaml
to your github. Please open Actions in your repository web view and see whether the automation works. -
Now, let's add more tooling. Please call lint and run tests with Github Actions.
-
Replace calling directly
pip3
, linter and executing tests by callingMakefile
targets.
-
Test coverage:
echo 'pytest-cov' >> test_requirements.txt pip install -r test_requirements.txt
PYTHONPATH=. py.test --verbose -s --cov=hello_world --cov-report=xml
-
black uncompromising code formatter:
pip install black black hello_world # to see the changes git diff
Notice, if you are still learning Python, do not use black. With flake8, you must to fix the issues yourself and learn. You will miss this learning oportunity with black and learn slower.
- https://github.com/mre/awesome-static-analysis#python
- Commit messages:
- Imperative: https://chris.beams.io/posts/git-commit/
- Semantic:
- Gaining some popularity: https://github.com/carloscuesta/gitmoji-cli