WIP: include lhcb-hooks in gitlab-ci
This change is part of the simplification process of some scripts that should run before changes are merged to a branch. This is especially important for the master branch.
Up until now, every repository needed to include their own jobs that would contain an implementation of the said scripts. For example, if i wanted to check that the formatting is ok in my files before merging, i would have to create a job that includes a script that does that. And if i wanted to have this in my other repository as well, i would have to write it there too.
We have found a way to make this simpler and with much less duplication. This solution comes in the form of a python package called "pre-commit". I will not go into much detail on what pre-commit is, as you can find more information here (https://pre-commit.com/) and another explanation as well as a guide on exactly how to use it, here (https://gitlab.cern.ch/lhcb-core/pre-commit-hooks).
So, each repository should have its own .pre-commit-config.yaml file, which shows what scripts need to be run before commiting/merging and where to find them. If a repository does not contain a .pre-commit-config.yaml file, an lhcb default .pre-commit-config.yaml is used instead, containing some standard scripts (a check for copyright comment on the start of the file, a check for the formatting of files and a check for a license file in the repository). If all the checks pass, the pipeline continues normally, usually by running the unit tests. If a check fails, the whole pipeline fails as well, indicating that these changes should not be merged.
The logic above, is captured in the included yaml file. That way, a common way to include this kind of scripts is provided, while customization is still available by having your own .pre-commit-config.yaml. This limits duplication while needing to include only 2-3 lines in your gitlab-ci.yaml
To use this yaml file properly, the stage "pre-commit-hooks" needs to be added to the pipeline if other stages are already defined.
Merge request reports
Activity
- Resolved by Rosen Matev
Yes, i know. What is the standard process in these cases? Just make it a WIP? Or write a comment that it is being investigated?
added Build new feature labels
@msaur since this only affects the GitLab CI pipelines, there is no need for testing in the nightlies.
- Resolved by Rosen Matev
mentioned in issue Moore#198 (closed)
assigned to @rmatev
- Resolved by Rosen Matev
@gtsalama I wonder why the script step takes more than a minute when no files are changed, locally it seems much faster.
Would it make sense to use the CI cache for the venv to speed up the job? I guess in that case we should remove the condition https://gitlab.cern.ch/lhcb-core/pre-commit-hooks/-/blob/c25454936ce05fff5b2efd8b9a8217d2cd8afd4a/run_hooks.sh#L8 ?
- Resolved by Rosen Matev
For some legacy branches (not here), we'll only run the licence/copyright checks and not the formatting check. It would be good if we can override the default behaviour with a list of hooks to run. We can use variables (as in this example) to pass the information of which hooks we want to run to the included yaml. It can then pass something to
run_hooks.sh
Edited by Rosen Matev
- Resolved by Georgios-Christos Tsalamagkakis
I experimented a bit (see master...include-pre-commit-hooks-in-ci-test) and it seems that the job doesn't fail when I expect it to fail
Currently you add the job in the
test
stage, but I think this name is not very appropriate. I think we always want the hooks to run first, in a their own stage. Can we achieve this by just changing the stage name fromtest
tocheck
(like in Moore)?I'm not sure I completely understand, but if we have to avoid conflicts how about this feature: https://docs.gitlab.com/ee/ci/yaml/#pre-and-post (issue at https://gitlab.com/gitlab-org/gitlab/-/issues/31441)
https://gitlab.com/gitlab-org/gitlab/-/issues/29980 is also of interest. If I understand correctly, defining the stage at all in the included yml is not a good idea. Instead, we should fix a name that doesn't clash (eg
pre-commit-hooks
) and have every project add this stage explicitly.
@gtsalama Thanks for the very welcome developments, I can't wait to start using the hooks locally!
@clemenci please chime in on the points I raised and add your own
Edited by Rosen Matev- Resolved by Rosen Matev