Use git hooks to avoid faulty commits
I regularly edit GitLab CI config files. These are used to tell the GitLab Runner to - for example - compile and package the project on every commit. This is a handy feature, but it breaks whenever I accidentally introduce an error into the YAML config file.
To avoid this, I created a helper script which reads the file and passes it to GitLab’s API, which contains a command to validate the contents of the file. Calling this script before committing any changes reduces the faulty pipelines, but it does not completely avoid them. The reason is that most of the time I simply forget to use my script. Luckily, we can tell git to do this automatically.
Using Global git commit hooks,
I defined a very simple script which looks for the
.gitlab-ci.yml file; if it exists, it checks
it against the API. On failure, the script returns -1, which effectively aborts the commit.
After setting everything up, we still need to run
git init for every repository which should
use the hook. I am not sure what the decision behind this behavior is, but of course this can be achieved
with a simple
for x in $(find ~/projects -type d -name .git); do pushd $x/.. > /dev/null; git init; popd > /dev/null; done