Is it possible for a GitLab CI/CD pipeline to commit code changes?
I would like to run a stage that uses black to format my code automatically whenever I push my work.
image: python:3.6
stages:
- test
before_script:
- python3 -m pip install -r requirements.txt
test:linting:
script:
- black ./
I made sure to include a file that needs reformatting to test if this works.
$ black ./
reformatted test.py
All done! ✨ 🍰 ✨
1 file reformatted.
The file in my repository remains unchanged which leads me to believe that this might not be possible.
In this simple project with a job application, you can use the Web IDE to make a code change and push it to a feature branch. Select the file you would like to change from the menu on the left. Once you've modified the text in that file, add a commit message and create a new branch.
A GitLab pipeline executes several jobs, stage by stage, with the help of automated code. A continuous integration pipeline involves building something from the scratch and testing the same in a development environment.
Black won't automatically commit corrected python code unless you use pre-commit hook.
Best way to run black in CI is to include something like :
black . --check --verbose --diff --color
This will fail the test if python code fail to adhere to code format and force user to fix formatting.
please checkout black --help
for all tags.
This is a good reference on Black : https://www.mattlayman.com/blog/2018/python-code-black/
Repo : https://github.com/psf/black
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With