From a6ade6a8c78b62e5a535e94bd50818b43e0d8bcd Mon Sep 17 00:00:00 2001 From: eternal-flame-AD Date: Tue, 15 Oct 2024 17:03:06 -0500 Subject: [PATCH] Add bump-go action Signed-off-by: eternal-flame-AD --- .github/workflows/bump-go.yml | 23 +++++++++++++ ci/bump_go.sh | 64 +++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 .github/workflows/bump-go.yml create mode 100755 ci/bump_go.sh diff --git a/.github/workflows/bump-go.yml b/.github/workflows/bump-go.yml new file mode 100644 index 00000000..a096d66a --- /dev/null +++ b/.github/workflows/bump-go.yml @@ -0,0 +1,23 @@ +name: Bump Go Version +on: + workflow_dispatch: + schedule: + - cron: '0 5 * * *' + +permissions: + contents: read + pull-requests: write + +jobs: + try-bump: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Bump Go Version + uses: actions/setup-go@v5 + with: + go-version: stable + - name: Open a PR if not up to date + run: ci/bump-go.sh diff --git a/ci/bump_go.sh b/ci/bump_go.sh new file mode 100755 index 00000000..6f0c0f8d --- /dev/null +++ b/ci/bump_go.sh @@ -0,0 +1,64 @@ +#!/bin/bash + +REF_BRANCH=master +PR_KEYWORD="[bump-go]" +PR_LABEL="bump-go" + +set -e + +git show-ref --verify --quiet refs/heads/bump-go || git branch -c $REF_BRANCH bump-go + +# The version in the GO_VERSION file +current_version=$(git show $REF_BRANCH:GO_VERSION 2>/dev/null || echo "") +echo "Current version: $current_version" + +# The latest version installed +latest_version=$(go version | sed -E 's/.*go([0-9\.]*).*/\1/') +echo "Installed version: $latest_version" + +# The version already open in a PR +bump_candidate_version=$(git show bump-go:GO_VERSION 2>/dev/null || echo "") +echo "Bump candidate version: $bump_candidate_version" + +existing_prs=$(gh pr list --state open --base main --label "$PR_KEYWORD" --json "number" | jq -r 'map(.number) | .[]') + +if [ "$current_version" == "$latest_version" ]; then + echo "Go is up to date" + exit 0 +elif [ "$latest_version" == "$bump_candidate_version" ] && [ ! -z "$existing_prs" ]; then + echo "A PR is already open" + exit 0 +fi + +if [ -z "$GITHUB_TOKEN" ]; then + echo "GITHUB_TOKEN not set, but this is what I would do:" + echo "git checkout bump-go && git merge --ff-only $REF_BRANCH" + echo "echo \"$latest_version\" > GO_VERSION" + echo "git add GO_VERSION" + echo "git commit -m \"$PR_KEYWORD Bump Go to $latest_version\"" + echo "git push origin bump-go" + if [ -z "$existing_prs" ]; then + echo "gh pr create --base $REF_BRANCH --head bump-go --title \"$PR_KEYWORD Bump Go to $latest_version\" --label \"$PR_LABEL\"" + else + first_id=$(echo "$existing_prs" | head -n 1) + echo "gh pr edit $first_id --title \"$PR_KEYWORD Bump Go to $latest_version\"" + fi +else + git checkout bump-go && git merge --ff-only $REF_BRANCH + echo "$latest_version" > GO_VERSION + git add GO_VERSION + if git diff --quiet --cached; then + echo "No changes to commit" + else + git commit -m "$PR_KEYWORD Bump Go to $latest_version" + fi + git push origin bump-go + if [ -z "$existing_prs" ]; then + gh pr create --base $REF_BRANCH --head bump-go --title "$PR_KEYWORD Bump Go to $latest_version" --label "$PR_LABEL" \ + --body "This PR was automatically created by the bump-go action." + else + first_id=$(echo "$existing_prs" | head -n 1) + gh pr edit $first_id --title "$PR_KEYWORD Bump Go to $latest_version" + fi +fi + \ No newline at end of file