|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Script to automate bumping the Go version across all Docker images |
| 4 | +# Usage: ./scripts/bump_go_version.sh <target_go_version> |
| 5 | +# Example: ./scripts/bump_go_version.sh 1.24.6 |
| 6 | + |
| 7 | +set -euo pipefail |
| 8 | + |
| 9 | +# Colors for output |
| 10 | +RED='\033[0;31m' |
| 11 | +GREEN='\033[0;32m' |
| 12 | +YELLOW='\033[1;33m' |
| 13 | +NC='\033[0m' # No Color |
| 14 | + |
| 15 | +function info() { |
| 16 | + echo -e "${GREEN}[INFO]${NC} $1" |
| 17 | +} |
| 18 | + |
| 19 | +function error() { |
| 20 | + echo -e "${RED}[ERROR]${NC} $1" >&2 |
| 21 | +} |
| 22 | + |
| 23 | +function warning() { |
| 24 | + echo -e "${YELLOW}[WARNING]${NC} $1" |
| 25 | +} |
| 26 | + |
| 27 | +# Check if target version is provided |
| 28 | +if [ $# -ne 1 ]; then |
| 29 | + error "Usage: $0 <target_go_version>" |
| 30 | + error "Example: $0 1.24.6" |
| 31 | + exit 1 |
| 32 | +fi |
| 33 | + |
| 34 | +TARGET_VERSION="$1" |
| 35 | + |
| 36 | +# Validate version format (should be like 1.24.6) |
| 37 | +if ! [[ "$TARGET_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 38 | + error "Invalid version format: $TARGET_VERSION" |
| 39 | + error "Expected format: X.Y.Z (e.g., 1.24.6)" |
| 40 | + exit 1 |
| 41 | +fi |
| 42 | + |
| 43 | +info "Target Go version: $TARGET_VERSION" |
| 44 | + |
| 45 | +# Get the repository root |
| 46 | +REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 47 | +cd "$REPO_ROOT" |
| 48 | + |
| 49 | +# Ensure we're on a clean working tree |
| 50 | +if ! git diff-index --quiet HEAD -- 2>/dev/null; then |
| 51 | + error "Working tree is not clean. Please commit or stash your changes first." |
| 52 | + exit 1 |
| 53 | +fi |
| 54 | + |
| 55 | +# Fetch the Go download page |
| 56 | +info "Fetching Go download information from https://go.dev/dl/..." |
| 57 | +GO_DL_PAGE=$(curl -sSL "https://go.dev/dl/") |
| 58 | + |
| 59 | +# Extract SHA256 for linux-amd64 |
| 60 | +# The HTML structure looks like: <tr>...<td>go1.24.6.linux-amd64.tar.gz</td>...<tt>SHA256_HASH</tt>...</tr> |
| 61 | +SHA256=$(echo "$GO_DL_PAGE" | grep -A 50 "go${TARGET_VERSION}.linux-amd64.tar.gz" | sed -n 's/.*<tt>\([a-f0-9]\{64\}\)<\/tt>.*/\1/p' | head -1) |
| 62 | + |
| 63 | +if [ -z "$SHA256" ]; then |
| 64 | + error "Failed to retrieve SHA256 checksum for Go version $TARGET_VERSION" |
| 65 | + error "Please verify the version exists at https://go.dev/dl/" |
| 66 | + error "Note: Archived versions may be further down the page" |
| 67 | + exit 1 |
| 68 | +fi |
| 69 | + |
| 70 | +info "Found SHA256: $SHA256" |
| 71 | + |
| 72 | +# Files to update |
| 73 | +DOCKERFILES=( |
| 74 | + "images/stackrox-build.Dockerfile" |
| 75 | + "images/stackrox-ui-test.Dockerfile" |
| 76 | + "images/scanner-build.Dockerfile" |
| 77 | +) |
| 78 | + |
| 79 | +# Update each Dockerfile |
| 80 | +info "Updating Dockerfiles..." |
| 81 | +for dockerfile in "${DOCKERFILES[@]}"; do |
| 82 | + if [ ! -f "$dockerfile" ]; then |
| 83 | + warning "File not found: $dockerfile (skipping)" |
| 84 | + continue |
| 85 | + fi |
| 86 | + |
| 87 | + info " - Updating $dockerfile" |
| 88 | + |
| 89 | + # Update GOLANG_VERSION |
| 90 | + if grep -q "ARG GOLANG_VERSION=" "$dockerfile"; then |
| 91 | + sed -i.bak "s/ARG GOLANG_VERSION=.*/ARG GOLANG_VERSION=${TARGET_VERSION}/" "$dockerfile" |
| 92 | + else |
| 93 | + warning " GOLANG_VERSION not found in $dockerfile" |
| 94 | + fi |
| 95 | + |
| 96 | + # Update GOLANG_SHA256 |
| 97 | + if grep -q "ARG GOLANG_SHA256=" "$dockerfile"; then |
| 98 | + sed -i.bak "s/ARG GOLANG_SHA256=.*/ARG GOLANG_SHA256=${SHA256}/" "$dockerfile" |
| 99 | + else |
| 100 | + warning " GOLANG_SHA256 not found in $dockerfile" |
| 101 | + fi |
| 102 | + |
| 103 | + # Remove backup files |
| 104 | + rm -f "${dockerfile}.bak" |
| 105 | +done |
| 106 | + |
| 107 | +info "All files updated successfully!" |
| 108 | + |
| 109 | +# Show the changes |
| 110 | +info "Changes made:" |
| 111 | +git diff |
| 112 | + |
| 113 | +# Create a new branch |
| 114 | +BRANCH_NAME="bump-go-${TARGET_VERSION}" |
| 115 | +info "Creating branch: $BRANCH_NAME" |
| 116 | +git checkout -b "$BRANCH_NAME" |
| 117 | + |
| 118 | +# Stage the changes |
| 119 | +info "Staging changes..." |
| 120 | +for dockerfile in "${DOCKERFILES[@]}"; do |
| 121 | + if [ -f "$dockerfile" ]; then |
| 122 | + git add "$dockerfile" |
| 123 | + fi |
| 124 | +done |
| 125 | + |
| 126 | +# Commit the changes |
| 127 | +COMMIT_MSG="Bump Go version to ${TARGET_VERSION} |
| 128 | +
|
| 129 | +Updates GOLANG_VERSION and GOLANG_SHA256 across all Docker images. |
| 130 | +
|
| 131 | +- GOLANG_VERSION: ${TARGET_VERSION} |
| 132 | +- GOLANG_SHA256: ${SHA256} |
| 133 | +- Archive: go${TARGET_VERSION}.linux-amd64.tar.gz" |
| 134 | + |
| 135 | +info "Committing changes..." |
| 136 | +git commit -m "$COMMIT_MSG" |
| 137 | + |
| 138 | +# Push the branch |
| 139 | +info "Pushing branch to origin..." |
| 140 | +if ! git push -u origin "$BRANCH_NAME"; then |
| 141 | + error "Failed to push branch. You may need to push manually:" |
| 142 | + error " git push -u origin $BRANCH_NAME" |
| 143 | + exit 1 |
| 144 | +fi |
| 145 | + |
| 146 | +# Create a pull request using GitHub CLI |
| 147 | +info "Creating pull request..." |
| 148 | +PR_TITLE="Bump Go version to ${TARGET_VERSION}" |
| 149 | +PR_BODY="This PR updates the Go version to ${TARGET_VERSION} across all Docker images. |
| 150 | +
|
| 151 | +## Changes |
| 152 | +- Updated \`GOLANG_VERSION\` to ${TARGET_VERSION} |
| 153 | +- Updated \`GOLANG_SHA256\` to ${SHA256} |
| 154 | +
|
| 155 | +## Verification |
| 156 | +- Archive: \`go${TARGET_VERSION}.linux-amd64.tar.gz\` |
| 157 | +- SHA256: \`${SHA256}\` |
| 158 | +- Source: https://go.dev/dl/ |
| 159 | +
|
| 160 | +## Affected Files |
| 161 | +$(for dockerfile in "${DOCKERFILES[@]}"; do echo "- \`$dockerfile\`"; done)" |
| 162 | + |
| 163 | +if command -v gh &> /dev/null; then |
| 164 | + PR_URL=$(gh pr create --title "$PR_TITLE" --body "$PR_BODY" --web 2>&1 | tee /dev/tty | grep -o 'https://github.com/[^[:space:]]*' || true) |
| 165 | + |
| 166 | + if [ -n "$PR_URL" ]; then |
| 167 | + info "Pull request created successfully!" |
| 168 | + info "PR URL: $PR_URL" |
| 169 | + else |
| 170 | + warning "GitHub CLI installed but PR creation may have failed." |
| 171 | + warning "Please check the output above or create the PR manually at:" |
| 172 | + warning " https://github.com/stackrox/rox-ci-image/compare/$BRANCH_NAME" |
| 173 | + fi |
| 174 | +else |
| 175 | + warning "GitHub CLI (gh) is not installed." |
| 176 | + warning "Please create a pull request manually at:" |
| 177 | + warning " https://github.com/stackrox/rox-ci-image/compare/$BRANCH_NAME" |
| 178 | + warning "" |
| 179 | + warning "Or install GitHub CLI: https://cli.github.com/" |
| 180 | +fi |
| 181 | + |
| 182 | +info "Done! 🎉" |
0 commit comments