Skip to content

Commit

Permalink
Add workflow to update go mod version.
Browse files Browse the repository at this point in the history
  • Loading branch information
robdimsdale committed May 31, 2024
1 parent 2d96bd5 commit ce6e749
Show file tree
Hide file tree
Showing 8 changed files with 477 additions and 0 deletions.
8 changes: 8 additions & 0 deletions actions/update-go-mod-version/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM alpine

RUN apk add \
bash \
&& rm -rf /var/cache/apk/*

COPY entrypoint /entrypoint
ENTRYPOINT ["/entrypoint"]
15 changes: 15 additions & 0 deletions actions/update-go-mod-version/action.yml
Original file line number Diff line number Diff line change
@@ -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 }}"
64 changes: 64 additions & 0 deletions actions/update-go-mod-version/entrypoint
Original file line number Diff line number Diff line change
@@ -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 "${@:-}"

78 changes: 78 additions & 0 deletions builder/.github/workflows/update-go-mod-version.yml
Original file line number Diff line number Diff line change
@@ -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}}
78 changes: 78 additions & 0 deletions implementation/.github/workflows/update-go-mod-version.yml
Original file line number Diff line number Diff line change
@@ -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}}
78 changes: 78 additions & 0 deletions language-family/.github/workflows/update-go-mod-version.yml
Original file line number Diff line number Diff line change
@@ -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}}
Loading

0 comments on commit ce6e749

Please sign in to comment.