-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuild_updated.sh
More file actions
executable file
·172 lines (144 loc) · 5.92 KB
/
build_updated.sh
File metadata and controls
executable file
·172 lines (144 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/bash
# Exit on error
set -e
# Logging function
log() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')] $@"
}
# Function to validate version format
validate_version() {
local version=$1
local regex="^[0-9]+\.[0-9]+\.[0-9]+(-canary\.[0-9]{8}\.[0-9]+)?$"
if [[ ! $version =~ $regex ]]; then
echo "Invalid version format: $version"
exit 1
fi
}
# Retry function
retry() {
local retries=${RETRIES:-3}
local count=0
until "$@"; do
exit_code=$?
count=$((count + 1))
if [ $count -lt $retries ]; then
log "Retrying ($count/$retries)..."
sleep 5
else
log "Failed after $count attempts."
return $exit_code
fi
done
return 0
}
# Convert comma-separated strings to arrays
IFS=',' read -ra NODE_VERSIONS <<<"$NODE_VERSIONS_TO_BUILD"
IFS=',' read -ra BUN_VERSIONS <<<"$BUN_VERSIONS_TO_BUILD"
IFS=',' read -ra DISTROS <<<"$DISTROS"
# If NODE_VERSIONS_TO_BUILD is empty, but BUN_VERSIONS_TO_BUILD is not,
# build all versions from versions.json
# If NODE_VERSIONS_TO_BUILD is empty, but BUN_VERSIONS_TO_BUILD is not,
# we used to build all versions from versions.json.
# But for automatic updates, we want to build NOTHING if no Node versions are updated.
if [ -z "$NODE_VERSIONS_TO_BUILD" ]; then
log "No Node.js versions to build."
NODE_VERSIONS=()
fi
log "Building Node versions: ${NODE_VERSIONS[*]}"
# If BUN_VERSIONS_TO_BUILD is empty, but NODE_VERSIONS_TO_BUILD is not,
# build all versions from versions.json
# If BUN_VERSIONS_TO_BUILD is empty, but NODE_VERSIONS_TO_BUILD is not,
# we used to build all versions from versions.json.
# But for automatic updates, we want to build NOTHING if no Bun versions are updated.
if [ -z "$BUN_VERSIONS_TO_BUILD" ]; then
log "No Bun versions to build."
BUN_VERSIONS=()
fi
log "Building Bun versions: ${BUN_VERSIONS[*]}"
# Validate versions
for version in "${NODE_VERSIONS[@]}"; do
validate_version "$version"
done
for version in "${BUN_VERSIONS[@]}"; do
validate_version "$version"
done
# Read the JSON file
json_data=$(cat versions.json)
# Function to generate tags
generate_tags() {
local bun_version=$1
local node_version=$2
local distro=$3
local node_major=${node_version%%.*}
local node_minor=${node_version%.*}
local bun_major=${bun_version%%.*}
local bun_minor=${bun_version%.*}
local is_canary=false
if [[ $bun_version == *"-canary"* ]]; then
is_canary=true
bun_version="canary"
fi
echo "$REGISTRY/bun-node:${bun_version}-${node_version}-${distro}"
if [ $is_canary == false ]; then
echo "$REGISTRY/bun-node:${bun_minor}-${node_version}-${distro}"
echo "$REGISTRY/bun-node:${bun_major}-${node_version}-${distro}"
echo "$REGISTRY/bun-node:${bun_version}-${node_minor}-${distro}"
echo "$REGISTRY/bun-node:${bun_version}-${node_major}-${distro}"
elif [[ $bun_version == "canary" ]]; then
echo "$REGISTRY/bun-node:canary-${node_minor}-${distro}"
echo "$REGISTRY/bun-node:canary-${node_major}-${distro}"
fi
local codename=$(echo "${json_data}" | jq -r ".nodejs.\"${node_major}\".name")
echo "$REGISTRY/bun-node:${bun_version}-${codename}-${distro}"
if [[ $is_canary == false ]]; then
echo "$REGISTRY/bun-node:latest-${node_version}-${distro}"
echo "$REGISTRY/bun-node:latest-${node_major}-${distro}"
echo "$REGISTRY/bun-node:latest-${codename}-${distro}"
fi
# Only tag "latest" if this is the latest Node.js version and Debian distro
# We need to check if the current node_major is the latest one in versions.json
# This is a bit tricky in bash without parsing JSON again, but we can assume the loop order or check against a known latest.
# Better approach: The caller knows if it's the latest.
# For now, let's restrict it to the highest known major version we support (e.g. 25) or check if it's the last one in the list?
# Actually, the issue says "latest tag should be the last tag built".
# If we build in order, the last one overwrites 'latest'.
# BUT, if we build multiple versions, we might overwrite 'latest' with an older version if the loop isn't sorted or if we build an old version update.
# A safer way is to explicitly check if this node version is the "latest" defined in versions.json.
local latest_node_major=$(echo "${json_data}" | jq -r '.nodejs | keys | map(tonumber) | max')
if [[ $is_canary == false && "$node_major" == "$latest_node_major" && $distro == "debian" ]]; then
echo "$REGISTRY/bun-node:latest"
fi
if [[ $is_canary == false ]]; then
echo "$REGISTRY/bun-node:${node_major}-${distro}"
fi
}
# Build, tag, and push loop
for bun_version in "${BUN_VERSIONS[@]}"; do
for node_version in "${NODE_VERSIONS[@]}"; do
for distro in "${DISTROS[@]}"; do
tag_distro=$distro
if [ "$distro" == "debian-slim" ]; then
tag_distro="slim"
fi
tags=($(generate_tags "$bun_version" "$node_version" "$tag_distro"))
node_major=${node_version%%.*}
log "Building image for Bun version $bun_version, Node version $node_version, Distro $distro"
image_name="$REGISTRY/bun-node:${bun_version}-${node_version}-${tag_distro}"
for tag in "${tags[@]}"; do
log "Tagging $image_name as $tag"
retry docker buildx build --sbom=true --provenance=true --platform "$PLATFORMS" -t "$image_name" -t "$tag" "./src/base/${node_major}/${distro}" --push --provenance=mode=max
if [ "$distro" == "alpine" ]; then
log "Building and Tagging Alpine image with Git"
retry docker buildx build --sbom=true --provenance=true --platform "$PLATFORMS" -t "$image_name-git" -t "$tag-git" "./src/git/${node_major}/${distro}" --push --provenance=mode=max
fi
done
log "Updating versions.json file"
bun_tag="latest"
if [[ $bun_version == *"-canary"* ]]; then
bun_tag="canary"
fi
json_data=$(echo "${json_data}" | jq ".nodejs.\"${node_major}\".version = \"v${node_version}\"" | jq ".bun.\"${bun_tag}\" = \"v${bun_version}\"")
echo "${json_data}" >versions.json
done
done
done