Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: fix logql analyzer (backport release-3.1.x) [manual backport] #14519

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions .github/workflows/logql-analyzer.yml
Original file line number Diff line number Diff line change
@@ -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
55 changes: 55 additions & 0 deletions .github/workflows/scripts/helm-tagged-release.sh
Original file line number Diff line number Diff line change
@@ -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/(.*\<AUTOMATED_UPDATES_LOCATOR\>.*)/\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"
114 changes: 114 additions & 0 deletions .github/workflows/scripts/helm-weekly-release.sh
Original file line number Diff line number Diff line change
@@ -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/(.*\<AUTOMATED_UPDATES_LOCATOR\>.*)/\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/(.*\<AUTOMATED_UPDATES_LOCATOR\>.*)/\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
Loading