From ce6e749c32a6e1f58e20d1efa5f293370770bd45 Mon Sep 17 00:00:00 2001 From: Rob Dimsdale-Zucker Date: Fri, 31 May 2024 13:22:03 -0700 Subject: [PATCH] Add workflow to update go mod version. --- actions/update-go-mod-version/Dockerfile | 8 ++ actions/update-go-mod-version/action.yml | 15 ++++ actions/update-go-mod-version/entrypoint | 64 +++++++++++++++ .../workflows/update-go-mod-version.yml | 78 +++++++++++++++++++ .../workflows/update-go-mod-version.yml | 78 +++++++++++++++++++ .../workflows/update-go-mod-version.yml | 78 +++++++++++++++++++ .../workflows/update-go-mod-version.yml | 78 +++++++++++++++++++ .../workflows/update-go-mod-version.yml | 78 +++++++++++++++++++ 8 files changed, 477 insertions(+) create mode 100644 actions/update-go-mod-version/Dockerfile create mode 100644 actions/update-go-mod-version/action.yml create mode 100755 actions/update-go-mod-version/entrypoint create mode 100644 builder/.github/workflows/update-go-mod-version.yml create mode 100644 implementation/.github/workflows/update-go-mod-version.yml create mode 100644 language-family/.github/workflows/update-go-mod-version.yml create mode 100644 library/.github/workflows/update-go-mod-version.yml create mode 100644 stack/.github/workflows/update-go-mod-version.yml diff --git a/actions/update-go-mod-version/Dockerfile b/actions/update-go-mod-version/Dockerfile new file mode 100644 index 00000000..d90a1d82 --- /dev/null +++ b/actions/update-go-mod-version/Dockerfile @@ -0,0 +1,8 @@ +FROM alpine + +RUN apk add \ + bash \ + && rm -rf /var/cache/apk/* + +COPY entrypoint /entrypoint +ENTRYPOINT ["/entrypoint"] diff --git a/actions/update-go-mod-version/action.yml b/actions/update-go-mod-version/action.yml new file mode 100644 index 00000000..f93007ee --- /dev/null +++ b/actions/update-go-mod-version/action.yml @@ -0,0 +1,15 @@ +name: "Update Go Mod Version" +description: | + Updates the versions of go and the go toolchain in a go.mod file + +inputs: + toolchain-version: + description: 'Version of the toolchain to write' + required: true + +runs: + using: 'docker' + image: 'Dockerfile' + args: + - "--toolchain-version" + - "${{ inputs.toolchain-version }}" diff --git a/actions/update-go-mod-version/entrypoint b/actions/update-go-mod-version/entrypoint new file mode 100755 index 00000000..fec40d30 --- /dev/null +++ b/actions/update-go-mod-version/entrypoint @@ -0,0 +1,64 @@ +#! /usr/bin/env bash + +set -euo pipefail +shopt -s inherit_errexit + +function main() { + local min_go_version toolchain_version + min_go_version="1.21" # 1.21 is the minimum to use the toolchain directive in go.mod + toolchain_version="" + + while [[ "${#}" != 0 ]]; do + case "${1}" in + --toolchain-version) + toolchain_version="${2}" + shift 2 + ;; + + "") + # skip if the argument is empty + shift 1 + ;; + + *) + echo "Unknown argument" "$@" + exit 1 + esac + done + + if [[ -z "${toolchain_version}" ]]; then + echo "Must provide toolchain version" + exit 1 + fi + + found_go_version="$(grep -E 'go ([0-9]+\.[0-9]+)' < go.mod | sed 's/go //g')" + + if lt "${found_go_version}" "${min_go_version}"; then + echo "Updating go version to the minimum required version '${min_go_version}' (was '${found_go_version}')" + sed -i "s/go ${found_go_version}/go ${min_go_version}/g" go.mod + found_go_version="${min_go_version}" + else + echo "not updating go version as current version (${found_go_version}) is >= minimum required version (${min_go_version})" + fi + + echo "setting toolchain to: '${toolchain_version}'" + if grep -qE 'toolchain go(.*)' < go.mod; then + sed -i "s/toolchain go.*/toolchain go${toolchain_version}/g" go.mod + else + # Write the toolchain directive two lines below the go version + sed -i "/go ${found_go_version}/r"<(printf "\ntoolchain go%s\n" "${toolchain_version}") go.mod + fi +} + +# returns $1 <= $2 +function lte() { + printf '%s\n' "$1" "$2" | sort -CV +} + +# returns $1 < $2 +function lt() { + ! lte "$2" "$1" +} + +main "${@:-}" + diff --git a/builder/.github/workflows/update-go-mod-version.yml b/builder/.github/workflows/update-go-mod-version.yml new file mode 100644 index 00000000..653d9faf --- /dev/null +++ b/builder/.github/workflows/update-go-mod-version.yml @@ -0,0 +1,78 @@ +name: Update Go version + +on: + schedule: + - cron: '53 5 * * MON' # every monday at 5:53 UTC + workflow_dispatch: + +concurrency: update-go + +jobs: + update-go: + name: Update go toolchain in go.mod + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v4 + - name: Checkout PR Branch + uses: paketo-buildpacks/github-config/actions/pull-request/checkout-branch@main + with: + branch: automation/go-mod-update/update-main + - name: Setup Go + id: setup-go + uses: actions/setup-go@v5 + with: + go-version: 'stable' + - name: Get current go toolchain version + id: current-go-version + uses: pivotal-cf/tanzu-cnb-github-config/actions/update-go-mod-version@main + with: + toolchain-version: ${{ steps.setup-go.outputs.go-version }} + - name: Show changes + run: | + echo "head -n10 go.mod " + head -n10 go.mod + + echo "git diff" + git diff + - name: Commit + id: commit + uses: paketo-buildpacks/github-config/actions/pull-request/create-commit@main + with: + message: "Updating go mod version" + pathspec: "." + keyid: ${{ secrets.PAKETO_BOT_GPG_SIGNING_KEY_ID }} + key: ${{ secrets.PAKETO_BOT_GPG_SIGNING_KEY }} + + - name: Push Branch + if: ${{ steps.commit.outputs.commit_sha != '' }} + uses: paketo-buildpacks/github-config/actions/pull-request/push-branch@main + with: + branch: automation/go-mod-update/update-main + + - name: Open Pull Request + if: ${{ steps.commit.outputs.commit_sha != '' }} + uses: paketo-buildpacks/github-config/actions/pull-request/open@main + with: + token: ${{ secrets.PAKETO_BOT_GITHUB_TOKEN }} + title: "Updates go mod version" + branch: automation/go-mod-update/update-main + + failure: + name: Alert on Failure + runs-on: ubuntu-22.04 + needs: [update-go] + if: ${{ always() && needs.update-go.result == 'failure' }} + steps: + - name: File Failure Alert Issue + uses: paketo-buildpacks/github-config/actions/issue/file@main + with: + token: ${{ secrets.GITHUB_TOKEN }} + repo: ${{ github.repository }} + label: "failure:update-go-version" + comment_if_exists: true + issue_title: "Failure: Update Go Mod Version workflow" + issue_body: | + Update GitHub config workflow [failed](https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}). + comment_body: | + Another failure occurred: https://github.com/${{github.repository}}/actions/runs/${{github.run_id}} diff --git a/implementation/.github/workflows/update-go-mod-version.yml b/implementation/.github/workflows/update-go-mod-version.yml new file mode 100644 index 00000000..e9f044f8 --- /dev/null +++ b/implementation/.github/workflows/update-go-mod-version.yml @@ -0,0 +1,78 @@ +name: Update Go version + +on: + schedule: + - cron: '48 4 * * MON' # every monday at 4:48 UTC + workflow_dispatch: + +concurrency: update-go + +jobs: + update-go: + name: Update go toolchain in go.mod + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v4 + - name: Checkout PR Branch + uses: paketo-buildpacks/github-config/actions/pull-request/checkout-branch@main + with: + branch: automation/go-mod-update/update-main + - name: Setup Go + id: setup-go + uses: actions/setup-go@v5 + with: + go-version: 'stable' + - name: Get current go toolchain version + id: current-go-version + uses: pivotal-cf/tanzu-cnb-github-config/actions/update-go-mod-version@main + with: + toolchain-version: ${{ steps.setup-go.outputs.go-version }} + - name: Show changes + run: | + echo "head -n10 go.mod " + head -n10 go.mod + + echo "git diff" + git diff + - name: Commit + id: commit + uses: paketo-buildpacks/github-config/actions/pull-request/create-commit@main + with: + message: "Updating go mod version" + pathspec: "." + keyid: ${{ secrets.PAKETO_BOT_GPG_SIGNING_KEY_ID }} + key: ${{ secrets.PAKETO_BOT_GPG_SIGNING_KEY }} + + - name: Push Branch + if: ${{ steps.commit.outputs.commit_sha != '' }} + uses: paketo-buildpacks/github-config/actions/pull-request/push-branch@main + with: + branch: automation/go-mod-update/update-main + + - name: Open Pull Request + if: ${{ steps.commit.outputs.commit_sha != '' }} + uses: paketo-buildpacks/github-config/actions/pull-request/open@main + with: + token: ${{ secrets.PAKETO_BOT_GITHUB_TOKEN }} + title: "Updates go mod version" + branch: automation/go-mod-update/update-main + + failure: + name: Alert on Failure + runs-on: ubuntu-22.04 + needs: [update-go] + if: ${{ always() && needs.update-go.result == 'failure' }} + steps: + - name: File Failure Alert Issue + uses: paketo-buildpacks/github-config/actions/issue/file@main + with: + token: ${{ secrets.GITHUB_TOKEN }} + repo: ${{ github.repository }} + label: "failure:update-go-version" + comment_if_exists: true + issue_title: "Failure: Update Go Mod Version workflow" + issue_body: | + Update GitHub config workflow [failed](https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}). + comment_body: | + Another failure occurred: https://github.com/${{github.repository}}/actions/runs/${{github.run_id}} diff --git a/language-family/.github/workflows/update-go-mod-version.yml b/language-family/.github/workflows/update-go-mod-version.yml new file mode 100644 index 00000000..93fa37db --- /dev/null +++ b/language-family/.github/workflows/update-go-mod-version.yml @@ -0,0 +1,78 @@ +name: Update Go version + +on: + schedule: + - cron: '13 4 * * MON' # every monday at 4:13 UTC + workflow_dispatch: + +concurrency: update-go + +jobs: + update-go: + name: Update go toolchain in go.mod + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v4 + - name: Checkout PR Branch + uses: paketo-buildpacks/github-config/actions/pull-request/checkout-branch@main + with: + branch: automation/go-mod-update/update-main + - name: Setup Go + id: setup-go + uses: actions/setup-go@v5 + with: + go-version: 'stable' + - name: Get current go toolchain version + id: current-go-version + uses: pivotal-cf/tanzu-cnb-github-config/actions/update-go-mod-version@main + with: + toolchain-version: ${{ steps.setup-go.outputs.go-version }} + - name: Show changes + run: | + echo "head -n10 go.mod " + head -n10 go.mod + + echo "git diff" + git diff + - name: Commit + id: commit + uses: paketo-buildpacks/github-config/actions/pull-request/create-commit@main + with: + message: "Updating go mod version" + pathspec: "." + keyid: ${{ secrets.PAKETO_BOT_GPG_SIGNING_KEY_ID }} + key: ${{ secrets.PAKETO_BOT_GPG_SIGNING_KEY }} + + - name: Push Branch + if: ${{ steps.commit.outputs.commit_sha != '' }} + uses: paketo-buildpacks/github-config/actions/pull-request/push-branch@main + with: + branch: automation/go-mod-update/update-main + + - name: Open Pull Request + if: ${{ steps.commit.outputs.commit_sha != '' }} + uses: paketo-buildpacks/github-config/actions/pull-request/open@main + with: + token: ${{ secrets.PAKETO_BOT_GITHUB_TOKEN }} + title: "Updates go mod version" + branch: automation/go-mod-update/update-main + + failure: + name: Alert on Failure + runs-on: ubuntu-22.04 + needs: [update-go] + if: ${{ always() && needs.update-go.result == 'failure' }} + steps: + - name: File Failure Alert Issue + uses: paketo-buildpacks/github-config/actions/issue/file@main + with: + token: ${{ secrets.GITHUB_TOKEN }} + repo: ${{ github.repository }} + label: "failure:update-go-version" + comment_if_exists: true + issue_title: "Failure: Update Go Mod Version workflow" + issue_body: | + Update GitHub config workflow [failed](https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}). + comment_body: | + Another failure occurred: https://github.com/${{github.repository}}/actions/runs/${{github.run_id}} diff --git a/library/.github/workflows/update-go-mod-version.yml b/library/.github/workflows/update-go-mod-version.yml new file mode 100644 index 00000000..f301c3b7 --- /dev/null +++ b/library/.github/workflows/update-go-mod-version.yml @@ -0,0 +1,78 @@ +name: Update Go version + +on: + schedule: + - cron: '19 3 * * MON' # every monday at 3:19 UTC + workflow_dispatch: + +concurrency: update-go + +jobs: + update-go: + name: Update go toolchain in go.mod + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v4 + - name: Checkout PR Branch + uses: paketo-buildpacks/github-config/actions/pull-request/checkout-branch@main + with: + branch: automation/go-mod-update/update-main + - name: Setup Go + id: setup-go + uses: actions/setup-go@v5 + with: + go-version: 'stable' + - name: Get current go toolchain version + id: current-go-version + uses: pivotal-cf/tanzu-cnb-github-config/actions/update-go-mod-version@main + with: + toolchain-version: ${{ steps.setup-go.outputs.go-version }} + - name: Show changes + run: | + echo "head -n10 go.mod " + head -n10 go.mod + + echo "git diff" + git diff + - name: Commit + id: commit + uses: paketo-buildpacks/github-config/actions/pull-request/create-commit@main + with: + message: "Updating go mod version" + pathspec: "." + keyid: ${{ secrets.PAKETO_BOT_GPG_SIGNING_KEY_ID }} + key: ${{ secrets.PAKETO_BOT_GPG_SIGNING_KEY }} + + - name: Push Branch + if: ${{ steps.commit.outputs.commit_sha != '' }} + uses: paketo-buildpacks/github-config/actions/pull-request/push-branch@main + with: + branch: automation/go-mod-update/update-main + + - name: Open Pull Request + if: ${{ steps.commit.outputs.commit_sha != '' }} + uses: paketo-buildpacks/github-config/actions/pull-request/open@main + with: + token: ${{ secrets.PAKETO_BOT_GITHUB_TOKEN }} + title: "Updates go mod version" + branch: automation/go-mod-update/update-main + + failure: + name: Alert on Failure + runs-on: ubuntu-22.04 + needs: [update-go] + if: ${{ always() && needs.update-go.result == 'failure' }} + steps: + - name: File Failure Alert Issue + uses: paketo-buildpacks/github-config/actions/issue/file@main + with: + token: ${{ secrets.GITHUB_TOKEN }} + repo: ${{ github.repository }} + label: "failure:update-go-version" + comment_if_exists: true + issue_title: "Failure: Update Go Mod Version workflow" + issue_body: | + Update GitHub config workflow [failed](https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}). + comment_body: | + Another failure occurred: https://github.com/${{github.repository}}/actions/runs/${{github.run_id}} diff --git a/stack/.github/workflows/update-go-mod-version.yml b/stack/.github/workflows/update-go-mod-version.yml new file mode 100644 index 00000000..993e50b3 --- /dev/null +++ b/stack/.github/workflows/update-go-mod-version.yml @@ -0,0 +1,78 @@ +name: Update Go version + +on: + schedule: + - cron: '21 4 * * MON' # every monday at 4:21 UTC + workflow_dispatch: + +concurrency: update-go + +jobs: + update-go: + name: Update go toolchain in go.mod + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v4 + - name: Checkout PR Branch + uses: paketo-buildpacks/github-config/actions/pull-request/checkout-branch@main + with: + branch: automation/go-mod-update/update-main + - name: Setup Go + id: setup-go + uses: actions/setup-go@v5 + with: + go-version: 'stable' + - name: Get current go toolchain version + id: current-go-version + uses: pivotal-cf/tanzu-cnb-github-config/actions/update-go-mod-version@main + with: + toolchain-version: ${{ steps.setup-go.outputs.go-version }} + - name: Show changes + run: | + echo "head -n10 go.mod " + head -n10 go.mod + + echo "git diff" + git diff + - name: Commit + id: commit + uses: paketo-buildpacks/github-config/actions/pull-request/create-commit@main + with: + message: "Updating go mod version" + pathspec: "." + keyid: ${{ secrets.PAKETO_BOT_GPG_SIGNING_KEY_ID }} + key: ${{ secrets.PAKETO_BOT_GPG_SIGNING_KEY }} + + - name: Push Branch + if: ${{ steps.commit.outputs.commit_sha != '' }} + uses: paketo-buildpacks/github-config/actions/pull-request/push-branch@main + with: + branch: automation/go-mod-update/update-main + + - name: Open Pull Request + if: ${{ steps.commit.outputs.commit_sha != '' }} + uses: paketo-buildpacks/github-config/actions/pull-request/open@main + with: + token: ${{ secrets.PAKETO_BOT_GITHUB_TOKEN }} + title: "Updates go mod version" + branch: automation/go-mod-update/update-main + + failure: + name: Alert on Failure + runs-on: ubuntu-22.04 + needs: [update-go] + if: ${{ always() && needs.update-go.result == 'failure' }} + steps: + - name: File Failure Alert Issue + uses: paketo-buildpacks/github-config/actions/issue/file@main + with: + token: ${{ secrets.GITHUB_TOKEN }} + repo: ${{ github.repository }} + label: "failure:update-go-version" + comment_if_exists: true + issue_title: "Failure: Update Go Mod Version workflow" + issue_body: | + Update GitHub config workflow [failed](https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}). + comment_body: | + Another failure occurred: https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}