From a84ba8a35aad412e2f770a2f133d6fb742e67ab7 Mon Sep 17 00:00:00 2001 From: Trevor Whitney Date: Thu, 17 Oct 2024 08:49:40 -0600 Subject: [PATCH] ci: fix logql analyzer (#14511) Signed-off-by: Vladyslav Diachenko Co-authored-by: Vladyslav Diachenko (cherry picked from commit 311797442f0a402b388904ee346f4ee411e8ab03) --- .github/workflows/logql-analyzer.yml | 114 ++++++++++++++++++ .../workflows/scripts/helm-tagged-release.sh | 55 +++++++++ .../workflows/scripts/helm-weekly-release.sh | 114 ++++++++++++++++++ 3 files changed, 283 insertions(+) create mode 100644 .github/workflows/logql-analyzer.yml create mode 100755 .github/workflows/scripts/helm-tagged-release.sh create mode 100755 .github/workflows/scripts/helm-weekly-release.sh diff --git a/.github/workflows/logql-analyzer.yml b/.github/workflows/logql-analyzer.yml new file mode 100644 index 0000000000000..d78d90fb805a1 --- /dev/null +++ b/.github/workflows/logql-analyzer.yml @@ -0,0 +1,114 @@ +name: LogQL Analyzer + +on: + workflow_dispatch: + release: + types: + - released + +permissions: + contents: read + id-token: write + +jobs: + analyze: + runs-on: ubuntu-latest + + env: + BUILD_TIMEOUT: 60 + IMAGE_PREFIX: "grafana" + RELEASE_VERSION: "${{ github.event.release.tag_name || 'test' }}" + steps: + - uses: actions/checkout@v4 + with: + fetch-tags: true + path: loki + + - name: prepare + id: prepare + env: + MAJOR_MINOR_VERSION_REGEXP: '([0-9]+\\.[0-9]+)' + RELEASE_TAG_REGEXP: '^([0-9]+\\.[0-9]+\\.[0-9]+)$' + working-directory: loki + run: | + echo "$(./tools/image-tag)" > .tag + if [[ "$RELEASE_VERSION" == "test" ]]; then + echo "RELEASE_VERSION is not set, using image tag" + RELEASE_VERSION="$(cat .tag)" + fi + echo "RELEASE_VERSION: $RELEASE_VERSION" + + # if the tag matches the pattern `D.D.D` then RELEASE_NAME="D-D-x", otherwise RELEASE_NAME="next" + RELEASE_NAME=$([[ $RELEASE_VERSION =~ $RELEASE_TAG_REGEXP ]] && echo $RELEASE_TAG | grep -oE $MAJOR_MINOR_VERSION_REGEXP | sed "s/\\./-/g" | sed "s/$/-x/" || echo "next") + echo "RELEASE_NAME: $RELEASE_NAME" + + echo "release_version=${RELEASE_VERSION}" >> "$GITHUB_OUTPUT" + echo "release_name=${RELEASE_NAME}" >> "$GITHUB_OUTPUT" + + - id: "get-github-app-token" + name: "get github app token" + uses: "actions/create-github-app-token@v1" + with: + app-id: "${{ secrets.APP_ID }}" + owner: "${{ github.repository_owner }}" + private-key: "${{ secrets.APP_PRIVATE_KEY }}" + + - name: "Set up QEMU" + uses: "docker/setup-qemu-action@v3" + - name: "set up docker buildx" + uses: "docker/setup-buildx-action@v3" + - name: "Login to DockerHub (from vault)" + uses: "grafana/shared-workflows/actions/dockerhub-login@main" + + - name: "Build and push" + timeout-minutes: "${{ fromJSON(env.BUILD_TIMEOUT) }}" + uses: "docker/build-push-action@v6" + with: + build-args: "IMAGE_TAG=${{ steps.prepare.outputs.release_version }}" + context: loki + file: "loki/cmd/logql-analyzer/Dockerfile" + platforms: "linux/amd64" + push: true + tags: "grafana/logql-analyzer:${{ steps.prepare.outputs.release_version }}" + + - name: Log in to Google Artifact Registry + uses: grafana/shared-workflows/actions/login-to-gar@main + with: + registry: "us-docker.pkg.dev" + environment: "prod" + + - name: Update to latest image + env: + GITHUB_TOKEN: ${{ steps.get-github-app-token.outputs.token }} + RELEASE_NAME: ${{ steps.prepare.outputs.release_name }} + RELEASE_VERSION: ${{ steps.prepare.outputs.release_version }} + run: | + set -e -o pipefail + + cat << EOF > config.json + { + "repo_name": "deployment_tools", + "destination_branch": "master", + "git_author_email": "119986603+updater-for-ci[bot]@users.noreply.github.com", + "git_author_name": "version_bumper[bot]", + "git_committer_email": "119986603+updater-for-ci[bot]@users.noreply.github.com", + "git_committer_name": "version_bumper[bot]", + "pull_request_branch_prefix": "logql-analyzer/updater", + "pull_request_enabled": true, + "pull_request_existing_strategy": "replace", + "pull_request_title_prefix": "[logql-analyzer updater] ", + "pull_request_message": "Add logql-analyzer version to ${RELEASE_VERSION} to supported versions", + "update_jsonnet_attribute_configs": [ + { + "file_path": "ksonnet/environments/logql-analyzer/supported-versions.libsonnet", + "jsonnet_key": "${RELEASE_NAME}", + "jsonnet_value": "grafana/logql-analyzer:${RELEASE_VERSION}-amd64", + "upsert": true + } + ] + } + EOF + + docker run --rm \ + -e GITHUB_TOKEN="$GITHUB_TOKEN" \ + -e CONFIG_JSON="$(cat config.json)" us-docker.pkg.dev/grafanalabs-global/docker-deployment-tools-prod/updater |& tee updater-output.log diff --git a/.github/workflows/scripts/helm-tagged-release.sh b/.github/workflows/scripts/helm-tagged-release.sh new file mode 100755 index 0000000000000..4a0d90eec59af --- /dev/null +++ b/.github/workflows/scripts/helm-tagged-release.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: AGPL-3.0-only + +set -exo pipefail + +script_dir=$(cd "$(dirname "$0")" && pwd) +# shellcheck disable=SC2250,SC1091 +source "${script_dir}/common.sh" + +calculate_next_chart_version() { + local current_chart_version=$1 + + local current_chart_semver + current_chart_semver="$(echo "${current_chart_version}" | grep -P -o '^(\d+.){2}\d+')" + local new_chart_semver="${current_chart_semver}" + new_chart_semver=$(increment_semver "${current_chart_semver}" 1) + new_chart_semver=$(set_semver_patch_to_zero "${new_chart_semver}") + echo "${new_chart_semver}" +} + +validate_version_update() { + local new_chart_version=$1 + local current_chart_version=$2 + local latest_loki_tag=$3 + + if [[ "${new_chart_version}" == "${current_chart_version}" ]]; then + echo "New chart version (${new_chart_version}) is the same as current version (${current_chart_version}); not submitting PR" + exit 1 + fi +} + +latest_loki_tag=$(sed -E "s/v(.*)/\1/g" <<<"$1") + +values_file=production/helm/loki/values.yaml +chart_file=production/helm/loki/Chart.yaml + +current_chart_version=$(get_yaml_node "${chart_file}" .version) +new_chart_version=$(calculate_next_chart_version "${current_chart_version}") + +validate_version_update "${new_chart_version}" "${current_chart_version}" "${latest_loki_tag}" + +update_yaml_node "${values_file}" .loki.image.tag "${latest_loki_tag}" + +update_yaml_node "${values_file}" .enterprise.image.tag "${latest_loki_tag}" +update_yaml_node "${chart_file}" .appVersion "${latest_loki_tag}" +update_yaml_node "${chart_file}" .version "${new_chart_version}" + +sed --in-place \ + --regexp-extended \ + "s/(.*\.*)/\1\n\n## ${new_chart_version}\n\n- \[CHANGE\] Changed version of Grafana Loki to ${latest_loki_tag}/g" production/helm/loki/CHANGELOG.md + +make TTY='' helm-docs + +# shellcheck disable=SC2154,SC2250 +echo "new_chart_version=${new_chart_version}" >> "$GITHUB_OUTPUT" diff --git a/.github/workflows/scripts/helm-weekly-release.sh b/.github/workflows/scripts/helm-weekly-release.sh new file mode 100755 index 0000000000000..6b3d6043b604d --- /dev/null +++ b/.github/workflows/scripts/helm-weekly-release.sh @@ -0,0 +1,114 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: AGPL-3.0-only + +set -exo pipefail + +script_dir=$(cd "$(dirname "$0")" && pwd) +# shellcheck disable=SC2250,SC1091 +source "${script_dir}/common.sh" + +# Uses docker hub image tags to figure out what is the latest image tag +find_latest_image_tag() { + local docker_hub_repo=$1 + local regExp="^(k|weekly-k)\d+-[a-z0-9]+" + local crane_results + crane_results="$(crane ls "${docker_hub_repo}" | grep -P "${regExp}" | sed -E "s/([weekly-]*k[[:digit:]]*)-([^-]*).*/\1-\2/g" | sort -Vur)" + set +o pipefail + echo "${crane_results}" | head -1 + set -o pipefail +} + +# takes k197-abcdef and returns r197, k197-abcdef-arm64 and returns k197, weekly-k197-abcdef and returns k197 +extract_k_version() { + sed -E "s/[weekly-]*(k[[:digit:]]*).*/\1/g" <<<"$1" +} + +calculate_next_chart_version() { + local current_chart_version=$1 + local latest_image_tag=$2 + local k_release=$3 + + local current_chart_semver + current_chart_semver=$(echo "${current_chart_version}" | grep -P -o '^(\d+.){2}\d+') + local new_chart_weekly + new_chart_weekly=$(extract_k_version "${latest_image_tag}" | grep -P -o '\d+') + local new_chart_semver="${current_chart_semver}" + if [[ "${current_chart_version}" != *weekly* ]]; then + # If previous version was not a weekly, then it was a stable release. + # _This_ weekly release should have a semver that's one above the stable release. + new_chart_semver=$(increment_semver "${current_chart_semver}" 1) + # Also reset the patch release number to 0. + new_chart_semver=$(set_semver_patch_to_zero "${new_chart_semver}") + fi + + if ${k_release}; then + echo "${new_chart_semver}-weekly.${new_chart_weekly}" + else + echo "${new_chart_semver}" + fi +} + +validate_version_update() { + local new_chart_version=$1 + local current_chart_version=$2 + local latest_gel_tag=$3 + local latest_loki_tag=$4 + + if [[ "${new_chart_version}" == "${current_chart_version}" ]]; then + echo "New chart version (${new_chart_version}) is the same as current version (${current_chart_version}); not submitting weekly PR" + exit 1 + fi + + local gel_weekly_version + gel_weekly_version=$(extract_k_version "${latest_gel_tag}") + local loki_weekly_version + loki_weekly_version=$(extract_k_version "${latest_loki_tag}") + echo "Comparing GEL weekly version (${gel_weekly_version}) with Loki weekly version (${loki_weekly_version})" + if [[ "${gel_weekly_version}" != "${loki_weekly_version}" ]]; then + echo "GEL weekly version (${gel_weekly_version}) does not match Loki weekly version (${loki_weekly_version}); not submitting PR" + exit 1 + fi +} + +k_release=false +if [[ "$1" == "-k" ]]; then + k_release=true + shift +fi + +values_file=production/helm/loki/values.yaml +chart_file=production/helm/loki/Chart.yaml + +latest_loki_tag=$(find_latest_image_tag grafana/loki) +latest_gel_tag=$(find_latest_image_tag grafana/enterprise-logs) +current_chart_version=$(get_yaml_node "${chart_file}" .version) +new_chart_version=$(calculate_next_chart_version "${current_chart_version}" "${latest_loki_tag}" "${k_release}") + +validate_version_update "${new_chart_version}" "${current_chart_version}" "${latest_gel_tag}" "${latest_loki_tag}" + +if ${k_release}; then + update_yaml_node "${values_file}" .loki.image.tag "${latest_loki_tag}" + update_yaml_node "${values_file}" .enterprise.image.tag "${latest_gel_tag}" + update_yaml_node "${chart_file}" .appVersion "$(extract_k_version "${latest_loki_tag}")" +fi + +update_yaml_node "${chart_file}" .version "${new_chart_version}" + +if ${k_release}; then + sed --in-place \ + --regexp-extended \ + "s/(.*\.*)/\1\n\n## ${new_chart_version}\n\n- \[CHANGE\] Changed version of Grafana Loki to ${latest_loki_tag}\n- \[CHANGE\] Changed version of Grafana Enterprise Logs to ${latest_gel_tag}/g" production/helm/loki/CHANGELOG.md +else + sed --in-place \ + --regexp-extended \ + "s/(.*\.*)/\1\n\n## ${new_chart_version}/g" production/helm/loki/CHANGELOG.md +fi + +make TTY='' helm-docs + +# shellcheck disable=SC2154,SC2250 +echo "new_chart_version=${new_chart_version}" >> "$GITHUB_OUTPUT" +if ${k_release}; then + # shellcheck disable=SC2154,SC2250 + echo "weekly=$(extract_k_version "${latest_loki_tag}")" >> "$GITHUB_OUTPUT" +fi