.gitlab-ci.yml command line validation tool

Overview

Some of us like a straight forward command line to check our .gitlab-ci file.

Crude but functional

Usage

> validate.py path_to_gitlab-ci.yml

Script

#! /usr/bin/python
#
# script to validate .gitlab-ci.yml
#
import sys
import requests
import json

def main():
    r = requests.post("https://gitlab.example.com/api/v4/ci/lint", json={ 'content': file(sys.argv[1], 'r').read() })
    if r.status_code != requests.codes['OK']:
        sys.exit(3)
    
    data = r.json() 
    if data['status'] != 'valid':
        for e in data['errors']:
            print >> sys.stderr, e
        sys.exit(1)
    
    sys.exit(0) # valid

if __name__ == '__main__':
    main()
3 Likes

Thank you very much for this nice little script, exactly what I needed. I converted it to python 3.

#!/usr/bin/env python3
#
# script to validate .gitlab-ci.yml
#
import sys
import requests
import json

def main():
    with open(sys.argv[1]) as f:
        r = requests.post("https://gitlab.example.com/api/v4/ci/lint",
                json={ 'content': f.read() }, verify=False)

    if r.status_code != requests.codes['OK']:
        sys.exit(3)

    data = r.json()
    if data['status'] != 'valid':
        for e in data['errors']:
            print(e, file=sys.stderr)
        sys.exit(1)

    sys.exit(0) # valid

if __name__ == '__main__':
    main()
3 Likes

How can I validate yaml file with include?
for example like this:

.gitlab-ci.yml file content

include:

  • project: home-tasks/pipeline/python-tests
    file: django-tests.yml
    ref: v.2.0

when I run python script, an errors is returned

 python3 validate_yaml.py .gitlab-ci.yml
/usr/lib/python3/dist-packages/urllib3/connectionpool.py:860: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning)
Project `home-tasks/pipeline/python-tests` not found or access denied!
 python3 validate_yaml.py .gitlab-ci.yml
/usr/lib/python3/dist-packages/urllib3/connectionpool.py:860: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning)
Project `home-tasks/pipeline/python-tests` not found or access denied!

I’m glad you asked this question because it bumped this super-useful script up to the top and I almost certainly otherwise would not have found it!

Things to check in using this script:

  • you need to change gitlab.example.com to your own instance.
  • If your instance has a verifiable certificate (signed by a known and trusted CA), you can remove the verify=False which will give you the InsecureRequestWarning

For the second line–that’s specific to your code or how you’re running it. I’m not including anything specific to the project in the validation script (I used the Python3 version) and ensured I had requests installed via Pip in my default environment.

2 Likes

@DaulGitHub I never tested it with include files, I suspect this is not possible with the Gitlab API.

I just wanted to say that I stumbled upon another topic that has some interesting scripts and discussions about validating .gitlab-ci.yml files here.