Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you change code in a GitLab pipeline?

Tags:

gitlab

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.

gitlab-ci.yml

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.

Job output

 $ 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.

like image 676
Alex Haller Avatar asked Apr 19 '20 10:04

Alex Haller


People also ask

Can you edit code in GitLab?

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.

What is the purpose of a GitLab pipeline?

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.


1 Answers

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

like image 125
Kunal Avatar answered Sep 27 '22 18:09

Kunal