-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildx-local.sh
executable file
·168 lines (137 loc) · 5.15 KB
/
buildx-local.sh
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
#!/bin/bash
TARGET_TYPE=""
TARGET_VERSION=""
FORCE_BUILD=false
BUILD_ALL=false
# Parse command-line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
--type) TARGET_TYPE="$2"; shift ;;
--version) TARGET_VERSION="$2"; shift ;;
--force) FORCE_BUILD=true ;;
--all) BUILD_ALL=true ;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
done
# If --all is passed, disregard TARGET_TYPE and TARGET_VERSION
if $BUILD_ALL; then
TARGET_TYPE=""
TARGET_VERSION=""
else
# Check for missing parameters only if --all is not set
if [[ -z "$TARGET_TYPE" ]]; then
echo "Error: Missing --type parameter."
exit 1
fi
if [[ -z "$TARGET_VERSION" ]]; then
echo "Error: Missing --version parameter."
exit 1
fi
fi
DATE_CMD=date
if [[ "$(uname)" == "Darwin" ]]; then
# If on macOS, check if gdate is available
if command -v gdate > /dev/null; then
DATE_CMD=gdate
else
echo "Error: GNU date (gdate) is not installed. Install it using Homebrew (brew install coreutils)."
exit 1
fi
fi
# Exit immediately if a command exits with a non-zero status
set -e
# Handle script termination gracefully
cleanup() {
echo "Cleaning up..."
docker context use default || true
docker builder ls | awk 'NR>1 {print $1}' | grep -v "default" | grep -v "builder" | xargs -I {} docker builder rm {} || true
docker context rm builder || true
}
# Function to check when the image was last created locally
was_created_last_day() {
local image="$1"
# Get the image creation time using docker inspect
local timestamp=$(docker inspect --format '{{.Created}}' "$image")
# Convert the timestamp to seconds
local created_time=$($DATE_CMD --date="$timestamp" +%s)
local current_time=$($DATE_CMD +%s)
local one_day_in_seconds=86400
# Calculate the difference in time
local time_diff=$((current_time - created_time))
# If the time difference is less than a day (86400 seconds), return 0 (true)
if [ "$time_diff" -lt "$one_day_in_seconds" ]; then
return 0
else
return 1
fi
}
trap 'echo "Error on line $LINENO"' ERR
trap cleanup SIGINT SIGTERM
cleanup
docker context create builder
# Enable Docker experimental features
export DOCKER_CLI_EXPERIMENTAL=enabled
# Create a new builder instance
docker buildx create --use builder
# Variables
TYPES=("cli" "fpm")
PHP_VERSIONS=("7.1" "7.2" "7.3" "7.4" "8.0" "8.1" "8.2" "8.3" "8.4")
TARGET_PHP_VERSIONS=("${PHP_VERSIONS[@]}")
TARGET_TYPES=("${TYPES[@]}")
[ ! -z "$TARGET_VERSION" ] && TARGET_PHP_VERSIONS=("$TARGET_VERSION")
[ ! -z "$TARGET_TYPE" ] && TARGET_TYPES=("$TARGET_TYPE")
for VERSION in "${TARGET_PHP_VERSIONS[@]}"; do
for TYPE in "${TARGET_TYPES[@]}"; do
DIR="${TYPE}/${VERSION}"
echo "Processing version: ${VERSION}, type: ${TYPE}"
if [[ -f "${DIR}/.env" ]]; then
# Source environment variables from the .env file
set -a
source "${DIR}/.env"
set +a
TAG_NAME="mxmd/php:${PHP_VERSION}-${TYPE}"
# Disable the 'exit on error' behavior
set +e
# Attempt to pull the image and capture the output/error
PULL_OUTPUT=$(docker pull "mxmd/php:${PHP_VERSION}-${TYPE}" 2>&1)
PULL_STATUS=$?
# Print the output for debugging
echo "Pull output for mxmd/php:${PHP_VERSION}-${TYPE}:"
echo "--------------------------------------"
echo "$PULL_OUTPUT"
echo "--------------------------------------"
# Check for "No such object" error in the pull output
if [[ $PULL_OUTPUT == *"Error: No such object:"* ]]; then
echo "Warning: Image mxmd/php:${PHP_VERSION}-${TYPE} not found."
# Check for "manifest unknown" error in the pull output
elif [[ $PULL_OUTPUT == *"manifest unknown: manifest unknown"* ]]; then
echo "Warning: Image mxmd/php:${PHP_VERSION}-${TYPE} manifest unknown."
# Check for other errors based on the pull command exit status
elif [[ $PULL_STATUS -ne 0 ]]; then
echo "Error pulling mxmd/php:${PHP_VERSION}-${TYPE}. Exiting."
exit 1
else
if $FORCE_BUILD; then
echo "Force build enabled. Building mxmd/php:${PHP_VERSION}-${TYPE} regardless of its creation date."
elif was_created_last_day "mxmd/php:${PHP_VERSION}-${TYPE}"; then
echo "Image mxmd/php:${PHP_VERSION}-${TYPE} was created within the last day. Skipping build."
continue
fi
fi
# Exit immediately if a command exits with a non-zero status
set -e
docker buildx build \
--push \
--platform linux/amd64,linux/arm64 \
--tag "${TAG_NAME}" \
--build-arg PHP_VERSION="${PHP_VERSION}" \
--build-arg ALPINE_VERSION="${ALPINE_VERSION}" \
--build-arg ALPINE_IMAGE="alpine:${ALPINE_VERSION}" \
--file "${DIR}/Dockerfile" \
$TYPE/
fi
done
done
cleanup
exit;