diff --git a/.github/workflows/pre-commit-hook.yaml b/.github/workflows/pre-commit-hook.yaml deleted file mode 100644 index 27071d5..0000000 --- a/.github/workflows/pre-commit-hook.yaml +++ /dev/null @@ -1,26 +0,0 @@ -name: run-pre-commit-checks - -on: - pull_request: - branches: - - main - -jobs: - pre-commit-checks: - runs-on: ubuntu-latest - permissions: - actions: read - contents: read - - steps: - - name: Checkout Repository - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - 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 diff --git a/.github/workflows/run-checks.yaml b/.github/workflows/run-checks.yaml new file mode 100644 index 0000000..f809d98 --- /dev/null +++ b/.github/workflows/run-checks.yaml @@ -0,0 +1,32 @@ +name: run-checks + +on: + pull_request: + branches: + - main + +jobs: + run-checks: + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Install packages + run: | + sudo apt-get update + sudo apt-get -y install gettext git + - name: Install Promtool + run: | + PROM_VERSION=$(curl -s https://api.github.com/repos/prometheus/prometheus/releases/latest | jq -r .tag_name) + wget https://github.com/prometheus/prometheus/releases/download/${PROM_VERSION}/prometheus-${PROM_VERSION#"v"}.linux-amd64.tar.gz + tar -xvf prometheus-${PROM_VERSION#"v"}.linux-amd64.tar.gz + sudo mv prometheus-${PROM_VERSION#"v"}.linux-amd64/promtool /usr/local/bin/ + rm -rf prometheus-${PROM_VERSION}.linux-amd64 prometheus-${PROM_VERSION}.linux-amd64.tar.gz + - name: Run ./check-rules script + run: ./scripts/check-rules diff --git a/Makefile b/Makefile index 6af3c13..66ab167 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,6 @@ hooks-install: -rm .git/hooks/pre-commit (cd .git/hooks/ && ln -s ../../scripts/pre-commit pre-commit) -hooks-pre-commit-run: - @GIT_CMD="git diff --name-only --cached --diff-filter=d origin/main" \ +hooks-check-rules-run: + @GIT_CMD="git diff --name-only --diff-filter=d origin/main" \ ./scripts/pre-commit diff --git a/scripts/check-rules b/scripts/check-rules new file mode 100755 index 0000000..e038ed1 --- /dev/null +++ b/scripts/check-rules @@ -0,0 +1,27 @@ +#!/bin/bash + +# Redirect output to stderr. +exec 1>&2 + +EXIT_CODE=0 + +# List of changed files, excluding deleted ones +if [[ -z "${GIT_CMD}" ]]; then + GIT_CMD="git diff --name-only --cached --diff-filter=d" +else + GIT_CMD=${GIT_CMD} +fi + +FILES=$(${GIT_CMD}) + +for f in ${FILES}; do + # https://askubuntu.com/a/926695 + if [[ $(head -1 "${f}" | tr '\0' '\n') =~ "PROMETHEUS RULES" ]]; then + NODE_ROLL_WINDOW=5m ENVIRONMENT=local PROVIDER=local envsubst '$NODE_ROLL_WINDOW,$ENVIRONMENT,$PROVIDER' < ${f} | promtool check rules + if [ $? -ne 0 ]; then + EXIT_CODE=1 + fi + fi +done + +exit $EXIT_CODE diff --git a/scripts/pre-commit b/scripts/pre-commit index 50ecd8e..ba46e5c 100755 --- a/scripts/pre-commit +++ b/scripts/pre-commit @@ -1,60 +1,8 @@ #!/bin/bash -# Redirect output to stderr. -exec 1>&2 - exit_code=0 -# List of changed files, excluding deleted ones - -if [[ -z "${GIT_CMD}" ]]; then - GIT_CMD="git diff --name-only --cached --diff-filter=d" -else - GIT_CMD=${GIT_CMD} -fi - -PROM_VERSION="v2.47.0" - -function is_yaml_extension() { - local file=$1 - if [[ "${file}" == *yaml || "${file}" == *yml || "${file}" == *yaml.tmpl || "${file}" == *yml.tmpl ]]; then - return 0 - fi - - return 1 -} - -function is_valid_yaml() { - local file=$1 - if is_yaml_extension "${file}"; then - if yq -v "${file}" ; then - return 0 - fi - exit_code=1 - fi - - return 1 -} - -# Check alerts -function check_alerts() { - local files=$(${GIT_CMD}) - for f in ${files} - do - 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 - echo "err: Found misconfigured alerts staged in ${f}" - docker run -i --entrypoint promtool -v $PWD/${f}:$PWD/${f} docker.io/prom/prometheus:${PROM_VERSION} check rules $PWD/${f} - exit_code=1 - fi - fi - fi - done -} - -# checks -check_alerts +./scripts/check-rules +test $? -eq 0 || exit_code=1 exit ${exit_code}