|  | 
|  | 1 | +#!/bin/bash | 
|  | 2 | + | 
|  | 3 | +# Copyright 2022 The Kubernetes Authors. | 
|  | 4 | +# | 
|  | 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 6 | +# you may not use this file except in compliance with the License. | 
|  | 7 | +# You may obtain a copy of the License at | 
|  | 8 | +# | 
|  | 9 | +#     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 10 | +# | 
|  | 11 | +# Unless required by applicable law or agreed to in writing, software | 
|  | 12 | +# distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 14 | +# See the License for the specific language governing permissions and | 
|  | 15 | +# limitations under the License. | 
|  | 16 | + | 
|  | 17 | + | 
|  | 18 | +# Helper script to update golang to latest version | 
|  | 19 | + | 
|  | 20 | +set -o errexit | 
|  | 21 | +set -o nounset | 
|  | 22 | +set -o pipefail | 
|  | 23 | + | 
|  | 24 | +REPO_ROOT=$(git rev-parse --show-toplevel) | 
|  | 25 | +cd ${REPO_ROOT} | 
|  | 26 | + | 
|  | 27 | +# TODO: Should we update to the latest go, or the latest go in a particular minor? | 
|  | 28 | +GO_TOOLCHAIN=$(curl https://go.dev/dl/?mode=json | jq -r '[.[] | select(.stable == true)] | .[0].version') | 
|  | 29 | +echo "GO_TOOLCHAIN=$GO_TOOLCHAIN" | 
|  | 30 | + | 
|  | 31 | +git switch --force-create codebot-update-golang-version-${GO_TOOLCHAIN} | 
|  | 32 | + | 
|  | 33 | +# GO_VERSION is the GO_TOOLCHAIN without the go prefix | 
|  | 34 | +GO_VERSION=${GO_TOOLCHAIN#go} | 
|  | 35 | +echo "GO_VERSION=$GO_VERSION" | 
|  | 36 | + | 
|  | 37 | +# Update go.mod files | 
|  | 38 | +# Tidy each individual go.mod | 
|  | 39 | +for gomod_file in $(find "${REPO_ROOT}" -name "go.mod"); do | 
|  | 40 | +    dir=$(dirname ${gomod_file}) | 
|  | 41 | +    cd "${dir}" | 
|  | 42 | +    echo "Updating $gomod_file to toolchain $GO_TOOLCHAIN" | 
|  | 43 | +    go mod edit -toolchain=${GO_TOOLCHAIN} | 
|  | 44 | +done | 
|  | 45 | + | 
|  | 46 | +# Update Docker images | 
|  | 47 | +for dockerfile in $(find "${REPO_ROOT}" -name "Dockerfile*"); do | 
|  | 48 | +  echo "Updating Dockerfile $dockerfile to go image $GO_VERSION" | 
|  | 49 | +  sed -i -e "s/FROM golang:[0-9.]*/FROM golang:$GO_VERSION/g" $dockerfile | 
|  | 50 | +done | 
|  | 51 | + | 
|  | 52 | +if $(git diff --quiet); then | 
|  | 53 | +  echo "No changes" | 
|  | 54 | +else | 
|  | 55 | +  cd ${REPO_ROOT} | 
|  | 56 | +  git add . | 
|  | 57 | +  git commit -m "codebot: update go to ${GO_VERSION}" | 
|  | 58 | +fi | 
0 commit comments