Skip to content

Commit

Permalink
ci: update loki version in helm chart weekly and on Loki release (#14290
Browse files Browse the repository at this point in the history
)
  • Loading branch information
trevorwhitney authored Sep 30, 2024
1 parent d7c0f2b commit 38b01c9
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 55 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/helm-tagged-release-pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: helm-weekly-release-pr

on:
release:
types:
- released

jobs:
weekly-release-pr:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- 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: Update/regenerate files
id: update
run: bash .github/workflows/scripts/helm-tagged-release.sh ${{ github.event.release.tag_name }}

- name: Create Pull Request
uses: peter-evans/create-pull-request@v5
with:
token: ${{ steps.get_github_app_token.outputs.token }}
title: Release loki Helm chart ${{ steps.update.outputs.new_chart_version }}
body: Automated PR created by [helm-tagged-release-pr.yaml](https://github.com/grafana/loki/blob/main/.github/workflows/helm-tagged-release-pr.yaml)
commit-message: Update loki chart to ${{ steps.update.outputs.new_chart_version }}
branch: helm-chart-tagged-${{ steps.update.outputs.new_chart_version }}
base: main
labels: helm
37 changes: 37 additions & 0 deletions .github/workflows/helm-weekly-release-pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: helm-weekly-release-pr

on:
schedule:
- cron: '0 10 * * 1-5' # 10 UTC on weekdays; if we miss published images one day, they should align the day after

workflow_dispatch: # for manual testing

jobs:
weekly-release-pr:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: imjasonh/setup-crane@v0.4

- 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: Update/regenerate files
id: update
run: bash .github/workflows/scripts/helm-weekly-release.sh

- name: Create Pull Request
uses: peter-evans/create-pull-request@v5
with:
token: ${{ steps.get_github_app_token.outputs.token }}
title: Release loki Helm chart ${{ steps.update.outputs.new_chart_version }}
body: Automated PR created by [helm-weekly-release-pr.yaml](https://github.com/grafana/loki/blob/main/.github/workflows/helm-weekly-release-pr.yaml)
commit-message: Update loki chart to ${{ steps.update.outputs.new_chart_version }}
branch: helm-chart-weekly-${{ steps.update.outputs.new_chart_version }}
base: main
labels: helm
45 changes: 45 additions & 0 deletions .github/workflows/scripts/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env bash

set -exo pipefail

# This generates a new file where the yaml node is updated.
# The problem is that yq strips new lines when you update the file.
# So we use a workaround from https://github.com/mikefarah/yq/issues/515 which:
# generates the new file, diffs it with the original, removes all non-whitespace changes, and applies that to the original file.
update_yaml_node() {
local filename=$1
local yaml_node=$2
local new_value=$3
patch "${filename}" <<<"$(diff -U0 -w -b --ignore-blank-lines "${filename}" <(yq eval "${yaml_node} = \"${new_value}\"" "${filename}"))"
}

get_yaml_node() {
local filename=$1
local yaml_node=$2
yq "${yaml_node}" "${filename}"
}

# Increments the part of the semver string
# $1: version itself
# $2: number of part: 0 – major, 1 – minor, 2 – patch
increment_semver() {
local delimiter=.
local array=("$(echo "$1" | tr "${delimiter}" '\n')")
array[$2]=$((array[$2] + 1))
echo "$(
local IFS=${delimiter}
echo "${array[*]}"
)"
}

# Sets the patch segment of a semver to 0
# $1: version itself
set_semver_patch_to_zero() {
local delimiter=.
local array=("$(echo "$1" | tr "${delimiter}" '\n')")
array[2]="0"
echo "$(
local IFS=${delimiter}
echo "${array[*]}"
)"
}
54 changes: 54 additions & 0 deletions .github/workflows/scripts/helm-tagged-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/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

echo "::set-output name=new_chart_version::${new_chart_version}"
84 changes: 84 additions & 0 deletions .github/workflows/scripts/helm-weekly-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/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]+"
crane ls "${docker_hub_repo}" | grep -P "${regExp}" | sed -E "s/([weekly-]*k[[:digit:]]*)-([^-]*).*/\1-\2/g" | uniq | sort -Vur | head -1
}

# 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 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
echo "${new_chart_semver}-weekly.${new_chart_weekly}"
}

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
}

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}")

validate_version_update "${new_chart_version}" "${current_chart_version}" "${latest_gel_tag}" "${latest_loki_tag}"

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}")"
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}\n- \[CHANGE\] Changed version of Grafana Enterprise Logs to ${latest_gel_tag}/g" production/helm/loki/CHANGELOG.md

make TTY='' helm-docs

echo "::set-output name=new_chart_version::${new_chart_version}"
52 changes: 0 additions & 52 deletions .github/workflows/verify-drone.yml

This file was deleted.

2 changes: 1 addition & 1 deletion docs/sources/setup/install/helm/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -5962,7 +5962,7 @@ null
<tr>
<td>loki.image.tag</td>
<td>string</td>
<td>Overrides the image tag whose default is the chart's appVersion TODO: needed for 3rd target backend functionality revert to null or latest once this behavior is relased</td>
<td>Overrides the image tag whose default is the chart's appVersion</td>
<td><pre lang="json">
null
</pre>
Expand Down
2 changes: 0 additions & 2 deletions production/helm/loki/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ loki:
# -- Docker image repository
repository: grafana/loki
# -- Overrides the image tag whose default is the chart's appVersion
# TODO: needed for 3rd target backend functionality
# revert to null or latest once this behavior is relased
tag: null
# -- Overrides the image tag with an image digest
digest: null
Expand Down

0 comments on commit 38b01c9

Please sign in to comment.