|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# This script ensures that the same version of Go is referenced in all of the |
| 4 | +# following files: |
| 5 | +# - go.mod |
| 6 | +# - dogfood/coder/Dockerfile |
| 7 | +# - flake.nix |
| 8 | +# - .github/actions/setup-go/action.yml |
| 9 | +# The version of Go in go.mod is considered the source of truth. |
| 10 | + |
| 11 | +set -euo pipefail |
| 12 | +source "$(dirname "${BASH_SOURCE[0]}")/lib.sh" |
| 13 | +cdroot |
| 14 | + |
| 15 | +# At the time of writing, Nix only has go 1.22.x. |
| 16 | +# We don't want to fail the build for this reason. |
| 17 | +IGNORE_NIX=${IGNORE_NIX:-false} |
| 18 | + |
| 19 | +GO_VERSION_GO_MOD=$(grep -Eo 'go [0-9]+\.[0-9]+\.[0-9]+' ./go.mod | cut -d' ' -f2) |
| 20 | +GO_VERSION_DOCKERFILE=$(grep -Eo 'ARG GO_VERSION=[0-9]+\.[0-9]+\.[0-9]+' ./dogfood/coder/Dockerfile | cut -d'=' -f2) |
| 21 | +GO_VERSION_SETUP_GO=$(yq '.inputs.version.default' .github/actions/setup-go/action.yaml) |
| 22 | +GO_VERSION_FLAKE_NIX=$(grep -Eo '\bgo_[0-9]+_[0-9]+\b' ./flake.nix) |
| 23 | +# Convert to major.minor format. |
| 24 | +GO_VERSION_FLAKE_NIX_MAJOR_MINOR=$(echo "$GO_VERSION_FLAKE_NIX" | cut -d '_' -f 2-3 | tr '_' '.') |
| 25 | +log "INFO : go.mod : $GO_VERSION_GO_MOD" |
| 26 | +log "INFO : dogfood/coder/Dockerfile : $GO_VERSION_DOCKERFILE" |
| 27 | +log "INFO : setup-go/action.yaml : $GO_VERSION_SETUP_GO" |
| 28 | +log "INFO : flake.nix : $GO_VERSION_FLAKE_NIX_MAJOR_MINOR" |
| 29 | + |
| 30 | +if [ "$GO_VERSION_GO_MOD" != "$GO_VERSION_DOCKERFILE" ]; then |
| 31 | + error "Go version mismatch between go.mod and dogfood/coder/Dockerfile:" |
| 32 | +fi |
| 33 | + |
| 34 | +if [ "$GO_VERSION_GO_MOD" != "$GO_VERSION_SETUP_GO" ]; then |
| 35 | + error "Go version mismatch between go.mod and .github/actions/setup-go/action.yaml" |
| 36 | +fi |
| 37 | + |
| 38 | +# At the time of writing, Nix only constrains the major.minor version. |
| 39 | +# We need to check that specifically. |
| 40 | +if [ "$IGNORE_NIX" = "false" ]; then |
| 41 | + GO_VERSION_GO_MOD_MAJOR_MINOR=$(echo "$GO_VERSION_GO_MOD" | cut -d '.' -f 1-2) |
| 42 | + if [ "$GO_VERSION_FLAKE_NIX_MAJOR_MINOR" != "$GO_VERSION_GO_MOD_MAJOR_MINOR" ]; then |
| 43 | + error "Go version mismatch between go.mod and flake.nix" |
| 44 | + fi |
| 45 | +else |
| 46 | + log "INFO : Ignoring flake.nix, as IGNORE_NIX=${IGNORE_NIX}" |
| 47 | +fi |
| 48 | + |
| 49 | +log "Go version check passed, all versions are $GO_VERSION_GO_MOD" |
0 commit comments