Skip to content

Commit

Permalink
chore: Add cron action to create release PR (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinja2 authored Oct 15, 2024
1 parent 7de5854 commit 254fc8f
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 2 deletions.
66 changes: 66 additions & 0 deletions .github/workflows/draft-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Draft Release PR

# Description:
# This workflow will draft a new release PR for the helm chart if there is a new runner image/appVersion .
# A release PR will be created in these cases.
# - When a user kicks offs this workflow manually. A user can specify the CHART_VERSION and APP_VERSION used for the new release.
# - When the cron schedule kicks off the job and there is a new runner image tag available.

on:
schedule:
# Run every 12 hours at 55 minutes past the hour.
- cron: "55 */12 * * *"
workflow_dispatch:
inputs:
CHART_VERSION:
description: 'Optionally overrides the chart version in Chart.yaml.'
required: false
default: ''
APP_VERSION:
description: 'Optionally overrides the app version in Chart.yaml.'
required: false
default: ''

jobs:
draft-release:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
env:
CHART_VERSION: ${{ github.event.inputs.CHART_VERSION }}
APP_VERSION: ${{ github.event.inputs.APP_VERSION }}
steps:
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1

- name: Prepare Release
run: |
make CHART_VERSION=$CHART_VERSION APP_VERSION=$APP_VERSION prepare-release
make docker-docs
- name: Check if PR is needed
run: |
echo "NEEDS_PR=1" >> "$GITHUB_OUTPUT"
git fetch origin
# Directly check if the branch exists and has the same changes in the remote repository
if git ls-remote --heads origin new-version-release > /dev/null; then
if git diff --no-ext-diff --quiet origin/new-version-release -- charts; then
echo "NEEDS_PR=0" >> "$GITHUB_OUTPUT"
fi
fi
- name: Open PR for Version Update
id: open_pr
if: ${{ steps.prepare_release.outputs.NEEDS_UPDATE == 1 && steps.check_if_pr_open.outputs.NEEDS_PR == 1 }}
uses: peter-evans/create-pull-request@5e914681df9dc83aa4e4905692ca88beb2f9e91f # v7.0.5
with:
commit-message: Prepare release v${{ steps.prepare_release.outputs.NEW_CHART_VERSION }}
title: "chore: Prepare release v${{ steps.prepare_release.outputs.NEW_CHART_VERSION }}"
body: |
Description
- Release chart version ${{ steps.prepare_release.outputs.NEW_CHART_VERSION }}
- Update runner app version ${{ steps.prepare_release.outputs.NEW_APP_VERSION }}
branch: new-version-release
base: main
delete-branch: true
draft: always-true
5 changes: 5 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ on:
pull_request:
branches-ignore:
- gh-pages
types:
- opened
- synchronize
- reopened
- ready_for_review # for draft-release.yaml's PR
push:
branches: [ main ]

Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ docs: ## Update chart docs
.PHONY: install-tools
install-tools: ## Install tools (macOS)
LOCALBIN=$(LOCALBIN) scripts/install-tools.sh || exit 1

.PHONY: prepare-release
prepare-release: ## Prepare new release if needed
@echo "Preparing release..."
./scripts/prepare-release.sh || exit 1
3 changes: 1 addition & 2 deletions scripts/install-tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Notes:
# - Should be executed via the `make install-tools` command.
# - Supports macOS for installations via `brew install`
# Include the base utility functions for setting and debugging variables
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# Function to install a tool
Expand Down Expand Up @@ -68,7 +67,7 @@ install_helm_plugin() {
}

# install brew-based tools
for tool in yamllint chart-testing helm kubectl pre-commit norwoodj/tap/helm-docs; do
for tool in yq jq yamllint chart-testing helm kubectl pre-commit norwoodj/tap/helm-docs; do
install "$tool" brew
done

Expand Down
56 changes: 56 additions & 0 deletions scripts/prepare-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash

# Description: Checks if new runner image tag is available and
# updates the appVersion and version in the Chart.yaml file if necessary.
# User can override the new appVersion and chart version by setting the
# APP_VERSION and CHART_VERSION environment variables respectively.

# Requires yq and jq to be installed.

set -euo pipefail
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
CHART_YAML_PATH="${SCRIPT_DIR}/../charts/splunk-synthetics-runner/Chart.yaml"

CURRENT_APP_VERSION=$(yq eval '.appVersion' ${CHART_YAML_PATH})
CURRENT_CHART_VERSION=$(yq eval '.version' ${CHART_YAML_PATH})
NEW_APP_VERSION=""

# Fetch the latest runner image tag from Quay.io
LATEST_RUNNER_TAG=$(curl -s "https://quay.io/api/v1/repository/signalfx/splunk-synthetics-runner/tag/?onlyActiveTags=true" \
-H "Accept: application/json" | \
jq -r '. as $orig |
.tags[] | select (.name == "latest") |
.manifest_digest as $latest | $orig.tags[] |
select(.manifest_digest == $latest and .name != "latest") | .name')
echo "The latest runner image tag available in quay.io is $LATEST_RUNNER_TAG"

NEW_APP_VERSION=${APP_VERSION:-$LATEST_RUNNER_TAG}

# Exit if current and new app versions are the same
if [ "$CURRENT_APP_VERSION" == "$NEW_APP_VERSION" ]; then
echo "Runner image tag is already up to date (appVersion $CURRENT_APP_VERSION)."
if [[ -n "${GITHUB_ACTIONS-}" ]]; then
echo "NEEDS_UPDATE=0" >> "$GITHUB_ENV"
fi
exit 0
else
echo "Runner image tag is not up to date. Current appVersion is $CURRENT_APP_VERSION and the new appVersion is $NEW_APP_VERSION."
if [[ -n "${GITHUB_ACTIONS-}" ]]; then
echo "NEEDS_UPDATE=1" >> "$GITHUB_ENV"
echo "NEW_APP_VERSION=$NEW_APP_VERSION" >> "$GITHUB_ENV"
fi
fi

# Update the appVersion
NEW_APP_VERSION=${NEW_APP_VERSION} yq eval -i '.appVersion = strenv(NEW_APP_VERSION)' ${CHART_YAML_PATH}

# Update the chart version by bumping up the patch version
BUMPED_PATCH_VERSION=$(echo ${CURRENT_CHART_VERSION} | awk -F. '{$NF = $NF + 1;} 1' | sed 's/ /./g')
NEW_CHART_VERSION=${CHART_VERSION:-$BUMPED_PATCH_VERSION}

echo "Updating the chart version to $NEW_CHART_VERSION"
NEW_CHART_VERSION=${NEW_CHART_VERSION} yq eval -i '.version = strenv(NEW_CHART_VERSION)' ${CHART_YAML_PATH}

if [[ -n "${GITHUB_ACTIONS-}" ]]; then
echo "NEW_CHART_VERSION=$NEW_CHART_VERSION" >> "$GITHUB_ENV"
fi

0 comments on commit 254fc8f

Please sign in to comment.