Skip to content

Commit

Permalink
Add yaml format checking with yq
Browse files Browse the repository at this point in the history
Use yq to verify yaml files instead of relying solely on file format
  • Loading branch information
DTLP committed Oct 4, 2023
1 parent 9dcd585 commit 1817cbe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/pre-commit-hook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ jobs:
- name: Install packages
run: |
sudo apt-get -y install git
- name: Set up yq
uses: frenck/action-setup-yq@v1
- name: Run pre-commit checks script
run: make hooks-pre-commit-run
13 changes: 11 additions & 2 deletions scripts/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fi

PROM_VERSION="v2.28.1"

function is_yaml() {
function is_yaml_extension() {
local file=$1
if [[ "${file}" == *yaml || "${file}" == *yml || "${file}" == *yaml.tmpl || "${file}" == *yml.tmpl ]]; then
return 0
Expand All @@ -24,12 +24,21 @@ function is_yaml() {
return 1
}

function is_valid_yaml() {
local file=$1
if is_yaml_extension "${file}" && yq -v "${file}" ; then
return 0
fi

exit_code=1
}

# Check alerts
function check_alerts() {
local files=$(${GIT_CMD})
for f in ${files}
do
if is_yaml "${f}"; then
if is_valid_yaml "${f}"; then
if [[ $(head -1 "${f}") =~ "PROMETHEUS RULES" ]]; then
docker run -i --entrypoint promtool -v $PWD/${f}:$PWD/${f} docker.io/prom/prometheus:${PROM_VERSION} check rules $PWD/${f} &> /dev/null
if [ $? -ne 0 ]; then
Expand Down

0 comments on commit 1817cbe

Please sign in to comment.