Curl request ci lint .gitlab-ci.yml

Hi,

Trying to make a curl request to gitlab.com api for linting .gitlab-ci.yaml file but receiving bad request response: {"status":400,"error":"Bad Request"}

#!/usr/bin/env bash


PAYLOAD=$( cat << JSON 
{ "content":
$(<$PWD/../.gitlab-ci.yml)
JSON
)

echo "Payload is $PAYLOAD"

curl --include --show-error --request POST --header "Content-Type: application/json" --header "Accept: application/json" "https://gitlab.com/api/v4/ci/lint" --data-binary "$PAYLOAD"

Has anyone managed to successfully lint a .gitlab-ci.yml via a bash script?

Solved it and documented how at the end of the document in this link .

Hope this assists others.

2 Likes

@dcs3spp Thanks so much for coming back and sharing your solution!

1 Like

@slee24 Many thanks, appreciated. I hope it helps…

Since the time of asking this question, I have developed and released a Ruby gem, served on rubygems.org. Details of the gem are available from here.

Install with:

gem install gitlab-lint-client

Usage for the CLI is:

glab-lint -h

The source code is available from here. The repository also serves a pre-commit hook rule called validate-gitlab-ci. Add a .pre-commit-config.yml file to the root of a repository and copy and paste the following into it:

repos:
- repo: https://github.com/dcs3spp/validate-gitlab-ci
  rev: v0.0.1
  hooks:
  - id: validate-gitlab-ci
    args: [--yaml=.gitlab-ci.yml, --base-url=https://gitlab.com]
    pass_filenames: false
    types: [yaml]
    files: .gitlab-ci.yml
    stages: [commit]

Install pre-commit:

pip install pre-commit

Request the pre-commit tool to download and configure the git hook:

pre-commit install

Optionally, explictly try the hook with:

pre-commit run validate-gitlab-ci --all-files

Upon first use this will configure a ruby environment and install the git pre-commit hook. Initially, this may take a few minutes but once installed the environment is reused.

From within a test repository, try it out by editing the .gitlab-ci.yml to have some invalid content. Then, try commiting the invalid .gitlab-ci.yml file. The commit should be rejected, preventing it from being pushed to the repository and breaking the CI build!

1 Like