-
Notifications
You must be signed in to change notification settings - Fork 0
/
local-single-arch.sh
executable file
·157 lines (127 loc) · 4.29 KB
/
local-single-arch.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
#!/bin/bash
TARGET_TYPE=""
TARGET_VERSION=""
FORCE_BUILD=false
BUILD_ALL=false
TARGETPLATFORM=linux/amd64
# 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 ;;
--platform) TARGETPLATFORM="$2"; shift ;;
*) 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..."
exit
}
was_created_last_day() {
local image="$1"
local timestamp=$(docker inspect --format '{{.Created}}' "$image")
local created_time=$($DATE_CMD --date="$timestamp" +%s)
local current_time=$($DATE_CMD +%s)
local one_day_in_seconds=86400
local time_diff=$((current_time - created_time))
if [ "$time_diff" -lt "$one_day_in_seconds" ]; then
return 0
else
return 1
fi
}
trap cleanup SIGINT SIGTERM
# 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
echo "PHP_VERSION: ${PHP_VERSION}"
echo "ALPINE_VERSION: ${ALPINE_VERSION}"
TAG_NAME="mode-dev/php:${PHP_VERSION}-${TYPE}"
echo "TAG_NAME: ${TAG_NAME}"
echo "DIR: ${DIR}"
echo "Build context: $TYPE/"
echo "Platform: $TARGETPLATFORM"
# Check if the image exists locally
set +e
echo "Inspecting image: $TAG_NAME"
ERROR_MSG=$(docker inspect "$TAG_NAME" 2>&1)
IMAGE_EXISTS=$?
if [ $IMAGE_EXISTS -ne 0 ]; then
echo "Error inspecting image: $ERROR_MSG"
fi
set -e
if [[ $IMAGE_EXISTS -ne 0 ]]; then
echo "Image $TAG_NAME doesn't exist locally. Building..."
else
if $FORCE_BUILD; then
echo "Force build enabled. Building $TAG_NAME regardless of its creation date."
elif was_created_last_day "$TAG_NAME"; then
echo "Image $TAG_NAME was created within the last day. Skipping build."
continue
else
echo "Image $TAG_NAME is older than a day. Building..."
fi
fi
if [[ -f "${DIR}/Dockerfile" ]]; then
echo "${DIR}/Dockerfile exists"
else
echo "Error: ${DIR}/Dockerfile does not exist"
exit 1
fi
# Build the Docker image locally
docker build --no-cache \
--tag "${TAG_NAME}" \
--build-arg PHP_VERSION="${PHP_VERSION}" \
--build-arg ALPINE_VERSION="${ALPINE_VERSION}" \
--build-arg ALPINE_IMAGE="alpine:${ALPINE_VERSION}" \
--build-arg TARGETPLATFORM="${TARGETPLATFORM}" \
--file "${DIR}/Dockerfile" \
$TYPE/
fi
done
done
cleanup