diff --git a/.github/workflows/update-version.yml b/.github/workflows/update-version.yml new file mode 100644 index 0000000..465db65 --- /dev/null +++ b/.github/workflows/update-version.yml @@ -0,0 +1,55 @@ +name: Update version + +on: + schedule: + - cron: '0 6 * * *' + workflow_dispatch: + +jobs: + update-version: + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Check if update is needed + id: check + run: | + current=$(jq -r '.tag_name' updater/version.json) + latest=$(curl -fsSL https://api.github.com/repos/ModbusScope/ModbusScope/releases/latest | jq -r '.tag_name') + if [ "$current" != "$latest" ]; then + echo "needs_update=true" >> "$GITHUB_OUTPUT" + else + echo "needs_update=false" >> "$GITHUB_OUTPUT" + fi + + - name: Run update script + if: steps.check.outputs.needs_update == 'true' + run: | + chmod +x scripts/update_version.sh + ./scripts/update_version.sh + + - name: Commit and open pull request + if: steps.check.outputs.needs_update == 'true' + env: + GH_TOKEN: ${{ github.token }} + run: | + new_tag=$(jq -r '.tag_name' updater/version.json) + html_url=$(jq -r '.html_url' updater/version.json) + branch="update-version-${new_tag}" + + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + git checkout -b "$branch" + git add updater/version.json index.html + git commit -m "Update version to ${new_tag}" + git push -u origin "$branch" + + gh pr create \ + --title "Update version to ${new_tag}" \ + --body "Automated update: bumps all version references and \`updater/version.json\` to [${new_tag}](${html_url})." \ + --base main \ + --head "$branch" diff --git a/scripts/update_version.sh b/scripts/update_version.sh new file mode 100755 index 0000000..6865d9b --- /dev/null +++ b/scripts/update_version.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" + +RELEASE=$(curl -fsSL https://api.github.com/repos/ModbusScope/ModbusScope/releases/latest) +TAG_NAME=$(echo "$RELEASE" | jq -r '.tag_name') +HTML_URL=$(echo "$RELEASE" | jq -r '.html_url') +PUBLISHED_AT=$(echo "$RELEASE" | jq -r '.published_at') + +# Update updater/version.json +jq \ + --arg tag "$TAG_NAME" \ + --arg url "$HTML_URL" \ + --arg date "$PUBLISHED_AT" \ + '.tag_name = $tag | .html_url = $url | .published_at = $date' \ + "$REPO_ROOT/updater/version.json" > "$REPO_ROOT/updater/version.json.tmp" +mv "$REPO_ROOT/updater/version.json.tmp" "$REPO_ROOT/updater/version.json" + +# Derive "Month YYYY" for footer +MONTH_YEAR=$(date -d "$PUBLISHED_AT" '+%B %Y') + +# Replace all X.Y.Z version occurrences in index.html +sed -i "s/[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*/${TAG_NAME}/g" \ + "$REPO_ROOT/index.html" + +# Update the month/year suffix in the footer release line +sed -i "s/Latest release: v${TAG_NAME} ([^)]*)/Latest release: v${TAG_NAME} (${MONTH_YEAR})/g" \ + "$REPO_ROOT/index.html" + +echo "Updated to ${TAG_NAME}"