-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·163 lines (152 loc) · 4.73 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·163 lines (152 loc) · 4.73 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
#!/bin/bash
set -e
# Usage function
usage() {
echo "Usage: $0 [-l] [-f <ftl_branch>] [-c <core_branch>] [-w <web_branch>] [-t <tag>] [use_cache]"
echo "Options:"
echo " -f, --ftlbranch <branch> Specify FTL branch (cannot be used in conjunction with -l)"
echo " -c, --corebranch <branch> Specify Core branch"
echo " -w, --webbranch <branch> Specify Web branch"
echo " -p, --paddbranch <branch> Specify PADD branch"
echo " -t, --tag <tag> Specify Docker image tag (default: pihole:local)"
echo " -l, --local Use locally built FTL binary (requires src/pihole-FTL file)"
echo " use_cache Enable caching (by default --no-cache is used)"
echo ""
echo "If no options are specified, the following command will be executed:"
echo " docker buildx build src/. --tag pihole:local --load --no-cache"
exit 1
}
# Set default values
TAG="pihole:local"
FTL_FLAG=false
USE_CACHE=false
CORE_FORK="pi-hole"
WEB_FORK="pi-hole"
PADD_FORK="pi-hole"
# Check if buildx is installed
if ! docker buildx version >/dev/null 2>&1; then
echo "Error: Docker buildx is required to build this image. For installation instructions, see:"
echo " https://github.com/docker/buildx#installing"
exit 1
fi
# Function to check if a custom branch entered by the user is valid
check_branch_exists() {
local repo=$1
local branch=$2
local fork=$3
local url
if [ "$repo" == "ftl" ]; then
# Special case for FTL - we check for the binary instead of just the branch - in case it is not yet built.
url="https://ftl.pi-hole.net/${branch}/pihole-FTL-amd64"
else
url="https://github.com/${fork}/${repo}/blob/${branch}/README.md"
fi
local http_code
http_code=$(curl -sI --max-time 10 "$url" -o /dev/null -w "%{http_code}")
if [ "${http_code}" -ne 200 ]; then
echo "Error: $repo branch '$branch' not found. Exiting."
exit 1
fi
}
# Collect extra --build-arg values from flags
BUILD_ARGS=()
# Parse command line arguments
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-l | --local)
if [ ! -f "src/pihole-FTL" ]; then
echo "File 'src/pihole-FTL' not found. Exiting."
exit 1
fi
if [ "$FTL_FLAG" = true ]; then
echo "Error: Both -l and -f cannot be used together."
usage
fi
FTL_FLAG=true
BUILD_ARGS+=(--build-arg "FTL_SOURCE=local")
shift
;;
-f | --ftlbranch)
if [ "$FTL_FLAG" = true ]; then
echo "Error: Both -l and -f cannot be used together."
usage
fi
FTL_FLAG=true
FTL_BRANCH="$2"
check_branch_exists "ftl" "$FTL_BRANCH"
BUILD_ARGS+=(--build-arg "FTL_BRANCH=$FTL_BRANCH")
shift
shift
;;
-c | --corebranch)
CORE_BRANCH="$2"
check_branch_exists "pi-hole" "$CORE_BRANCH" "$CORE_FORK"
BUILD_ARGS+=(--build-arg "CORE_BRANCH=$CORE_BRANCH")
shift
shift
;;
-w | --webbranch)
WEB_BRANCH="$2"
check_branch_exists "web" "$WEB_BRANCH" "$WEB_FORK"
BUILD_ARGS+=(--build-arg "WEB_BRANCH=$WEB_BRANCH")
shift
shift
;;
-p | --paddbranch)
PADD_BRANCH="$2"
check_branch_exists "padd" "$PADD_BRANCH"
BUILD_ARGS+=(--build-arg "PADD_BRANCH=$PADD_BRANCH")
shift
shift
;;
-cf | --corefork)
CORE_FORK="$2"
BUILD_ARGS+=(--build-arg "CORE_FORK=$CORE_FORK")
shift
shift
;;
-wf | --webfork)
WEB_FORK="$2"
BUILD_ARGS+=(--build-arg "WEB_FORK=$WEB_FORK")
shift
shift
;;
-pf | --paddfork)
PADD_FORK="$2"
BUILD_ARGS+=(--build-arg "PADD_FORK=$PADD_FORK")
shift
shift
;;
-t | --tag)
TAG="$2"
shift
shift
;;
use_cache)
USE_CACHE=true
shift
;;
*)
echo "Unknown option: $1"
usage
;;
esac
done
# Build the command as an array to avoid eval and shell injection
DOCKER_BUILD_CMD=(docker buildx build src/. --tag "${TAG}" --load)
[ "$USE_CACHE" = false ] && DOCKER_BUILD_CMD+=(--no-cache)
DOCKER_BUILD_CMD+=("${BUILD_ARGS[@]}")
# Execute the docker build command
echo "Executing command: ${DOCKER_BUILD_CMD[*]}"
if ! "${DOCKER_BUILD_CMD[@]}"; then
echo ""
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "!! ERROR: Docker build failed, please review logs above !!"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
exit 1
else
echo ""
echo "Successfully built Docker image with tag '$TAG'"
docker images "${TAG}"
fi