Alex Haller
Alex Haller

Reputation: 315

Can you change code in a GitLab pipeline?

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.

Upvotes: 10

Views: 8198

Answers (1)

Kunal
Kunal

Reputation: 366

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

Upvotes: 17

Related Questions