How to write automated test for Gitlab CI/CD configuration?

Viewed 546

I have notified the complexity of my configuration for Gitlab CI/CD grows fairly fast. Normally, in a either a programming language or infrastructure code I could write automated test for the code.

Is it possible to write automated test for the gitlab-ci.yml file itself?

Does it exist any library or testing framework for it?

Ideally I would like to setup the different environments variables, execute the gitlab-ci.yml file, and do assertions on the output.

2 Answers

I am not aware of any tool currently to really test behaviour. But i ended up with two approaches:

1. testing in smaller chunks aka unit testing my building blocks.

I extracted my builds into smaller chunks which i can include, and test seperately. This works fine for script blocks etc. but is not really sufficient for rule blocks - but offers great value, eg i use the templates for some steps as verification for the generated docker image, which will be used by those steps.

2. verification via gitlab-ci-local

https://github.com/firecow/gitlab-ci-local is a tool which allows you to test it locally, and which allows you to provide environment variables. There are some minor issues with branch name resolution in pr pipelines but besides that it works great. I use it to test gitlab ci files in GitHub Actions.

I hope that this helps somehow

Gitlab have a linter for gitlab-ci.yaml To access the CI Lint tool, navigate to CI/CD > Pipelines or CI/CD > Jobs in your project and click CI lint.

Docs

Related