-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdev-build.sh
More file actions
executable file
Β·278 lines (245 loc) Β· 8.64 KB
/
dev-build.sh
File metadata and controls
executable file
Β·278 lines (245 loc) Β· 8.64 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/bin/bash
# Build all Docker images locally and tag them as if from GitHub Container Registry
# This allows local testing before pushing to GHCR
#
# Usage: ./dev-build.sh [--no-cache] [--include-all] [--include-dotnet] [--include-<variant>...]
set -e
REGISTRY="ghcr.io/gordonbeeming/copilot_here"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
NO_CACHE=""
INCLUDE_ALL=false
declare -a INCLUDE_VARIANTS=()
# Detect container runtime from local config or auto-detect
detect_runtime() {
local runtime=""
# Check local .copilot_here/runtime.conf
if [[ -f "${SCRIPT_DIR}/.copilot_here/runtime.conf" ]]; then
runtime=$(cat "${SCRIPT_DIR}/.copilot_here/runtime.conf" | tr -d '[:space:]')
fi
# Check global config if not set locally
if [[ -z "$runtime" ]] && [[ -f "$HOME/.config/copilot_here/runtime.conf" ]]; then
runtime=$(cat "$HOME/.config/copilot_here/runtime.conf" | tr -d '[:space:]')
fi
# Auto-detect if not configured
if [[ -z "$runtime" ]] || [[ "$runtime" == "auto" ]]; then
if command -v docker &> /dev/null; then
runtime="docker"
elif command -v podman &> /dev/null; then
runtime="podman"
else
echo "Error: No container runtime found (docker or podman)"
exit 1
fi
fi
echo "$runtime"
}
CONTAINER_RUNTIME=$(detect_runtime)
# Detect OS and architecture for native binary
detect_rid() {
local os=""
local arch=""
case "$(uname -s)" in
Linux*) os="linux" ;;
Darwin*) os="osx" ;;
MINGW*|MSYS*|CYGWIN*) os="win" ;;
*) echo "Unknown OS"; return 1 ;;
esac
case "$(uname -m)" in
x86_64) arch="x64" ;;
aarch64|arm64) arch="arm64" ;;
*) arch="x64" ;;
esac
echo "${os}-${arch}"
}
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--no-cache)
NO_CACHE="--no-cache"
shift
;;
--include-all)
INCLUDE_ALL=true
shift
;;
--include-*)
variant="${1#--include-}"
INCLUDE_VARIANTS+=("$variant")
shift
;;
*)
echo "Unknown option: $1"
echo "Usage: ./dev-build.sh [--no-cache] [--include-all] [--include-dotnet] [--include-<variant>...]"
exit 1
;;
esac
done
# Generate Dockerfiles from snippets (requires PowerShell)
if ! command -v pwsh &> /dev/null; then
echo "Error: PowerShell (pwsh) is required to generate Dockerfiles."
echo "Install it via: brew install powershell (macOS) or see https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell"
exit 1
fi
echo "π§ Generating Dockerfiles from snippets..."
pwsh -File "${SCRIPT_DIR}/docker/generate-dockerfiles.ps1"
echo ""
# If --include-all, discover all variants from generated Dockerfiles
if $INCLUDE_ALL; then
for variant_file in "${SCRIPT_DIR}/docker/generated/Dockerfile."*; do
if [[ -f "$variant_file" ]]; then
variant_name=$(basename "$variant_file" | sed 's/Dockerfile\.//')
if [[ "$variant_name" != "default" ]]; then
INCLUDE_VARIANTS+=("$variant_name")
fi
fi
done
fi
echo "========================================"
echo " Building Container Images Locally"
echo "========================================"
echo ""
echo "Registry: $REGISTRY"
echo "Runtime: $CONTAINER_RUNTIME"
echo ""
# Build native binary
echo "π§ Building native CLI binary..."
RID=$(detect_rid)
PUBLISH_DIR="${SCRIPT_DIR}/publish/${RID}"
BIN_DIR="$HOME/.local/bin"
# Check for running copilot_here containers
RUNNING_CONTAINERS=$($CONTAINER_RUNTIME ps --filter "name=copilot_here-" -q)
if [ -n "$RUNNING_CONTAINERS" ]; then
echo "β οΈ copilot_here is currently running in $CONTAINER_RUNTIME"
printf " Stop running containers to continue? [y/N]: "
read -r response
if [ "$response" = "y" ] || [ "$response" = "Y" ] || [ "$response" = "yes" ] || [ "$response" = "YES" ]; then
echo "π Stopping copilot_here containers..."
$CONTAINER_RUNTIME stop $RUNNING_CONTAINERS 2>/dev/null
echo " β Stopped"
else
echo "β Cannot build while containers are running (binary is in use)"
exit 1
fi
echo ""
fi
dotnet publish "${SCRIPT_DIR}/app/CopilotHere.csproj" \
-c Release \
-r "$RID" \
-o "$PUBLISH_DIR" \
--nologo \
-v q
# Copy to local bin
mkdir -p "$BIN_DIR"
if [[ "$RID" == win-* ]]; then
cp "${PUBLISH_DIR}/copilot_here.exe" "$BIN_DIR/"
echo " β Copied to ${BIN_DIR}/copilot_here.exe"
else
cp "${PUBLISH_DIR}/copilot_here" "$BIN_DIR/"
chmod +x "$BIN_DIR/copilot_here"
echo " β Copied to ${BIN_DIR}/copilot_here"
fi
echo ""
# Copy airlock config files to ~/.config/copilot_here
echo "π Copying config files to ~/.config/copilot_here..."
CONFIG_DIR="$HOME/.config/copilot_here"
mkdir -p "$CONFIG_DIR"
if [ -f "${SCRIPT_DIR}/docker/tools/github-copilot/default-airlock-rules.json" ]; then
cp "${SCRIPT_DIR}/docker/tools/github-copilot/default-airlock-rules.json" "$CONFIG_DIR/default-airlock-rules.json"
echo " β Copied default-airlock-rules.json"
fi
echo ""
# Copy and source shell script
echo "π Updating shell functions..."
SHELL_SCRIPT_PATH="$HOME/.copilot_here.sh"
cp "${SCRIPT_DIR}/copilot_here.sh" "$SHELL_SCRIPT_PATH"
echo " β Copied copilot_here.sh to $SHELL_SCRIPT_PATH"
# Source the updated script
source "$SHELL_SCRIPT_PATH"
echo " β Sourced updated shell functions"
echo ""
# Build proxy image
echo "π§ Building proxy image..."
$CONTAINER_RUNTIME build $NO_CACHE \
-t "${REGISTRY}:proxy" \
-f "${SCRIPT_DIR}/docker/Dockerfile.proxy" \
"${SCRIPT_DIR}"
echo " β Tagged as ${REGISTRY}:proxy"
echo ""
# Build base image (default)
echo "π§ Building base image (GitHub Copilot)..."
$CONTAINER_RUNTIME build $NO_CACHE \
-t "${REGISTRY}:latest" \
-t "${REGISTRY}:copilot-latest" \
-f "${SCRIPT_DIR}/docker/generated/Dockerfile.default" \
"${SCRIPT_DIR}"
echo " β Tagged as ${REGISTRY}:latest"
echo " β Tagged as ${REGISTRY}:copilot-latest"
echo ""
# Build variant images (only if explicitly requested)
build_variant() {
local variant_name="$1"
local variant_file="${SCRIPT_DIR}/docker/generated/Dockerfile.${variant_name}"
if [[ -f "$variant_file" ]]; then
echo "π§ Building variant: $variant_name (GitHub Copilot)..."
$CONTAINER_RUNTIME build $NO_CACHE \
-t "${REGISTRY}:${variant_name}" \
-t "${REGISTRY}:copilot-${variant_name}" \
-f "$variant_file" \
"${SCRIPT_DIR}"
echo " β Tagged as ${REGISTRY}:${variant_name}"
echo " β Tagged as ${REGISTRY}:copilot-${variant_name}"
echo ""
else
echo "β οΈ Variant not found: $variant_name"
echo ""
fi
}
if [[ ${#INCLUDE_VARIANTS[@]} -gt 0 ]]; then
for variant_name in "${INCLUDE_VARIANTS[@]}"; do
build_variant "$variant_name"
done
else
echo "βΉοΈ Skipping variants (use --include-<variant> to build, e.g., --include-dotnet)"
echo ""
fi
echo "========================================"
echo " Build Complete!"
echo "========================================"
echo ""
echo "Built images:"
$CONTAINER_RUNTIME images --format " {{.Repository}}:{{.Tag}}\t{{.Size}}" | grep "$REGISTRY" | sort
echo ""
echo "Run integration tests with:"
echo " ./tests/integration/test_airlock.sh --use-local"
echo ""
echo "π‘ To use locally built images (skip pulling from registry):"
echo " copilot_here --no-pull"
echo " copilot_yolo --no-pull"
echo ""
# Emit explicit COPILOT_HERE_PROXY_IMAGE / COPILOT_HERE_APP_IMAGE export hints
# AND write them to a sourceable env file. Without this, an airlock + --dind
# session that needs the freshly-rebuilt proxy (e.g. one that depends on the
# socat bridge in proxy-entrypoint.sh) silently keeps using whatever was
# previously cached under :proxy on the host. The exports give an
# unambiguous override the next `copilot_here --enable-airlock --dind` will
# pick up via the COPILOT_HERE_PROXY_IMAGE / COPILOT_HERE_APP_IMAGE env vars.
ENV_FILE="${SCRIPT_DIR}/.dev-build.env"
{
echo "# Generated by dev-build.sh on $(date -u +%Y-%m-%dT%H:%M:%SZ)"
echo "export COPILOT_HERE_PROXY_IMAGE=\"${REGISTRY}:proxy\""
} > "$ENV_FILE"
# If exactly one variant was built, also export an app image override so the
# integration tests / airlock smoke can use it. We don't override when zero
# variants were built (default-only) or when several were built, since
# there's no obvious "the" app image to point at in those cases.
if [[ ${#INCLUDE_VARIANTS[@]} -eq 1 ]]; then
ONLY_VARIANT="${INCLUDE_VARIANTS[0]}"
if [[ -f "${SCRIPT_DIR}/docker/generated/Dockerfile.${ONLY_VARIANT}" ]]; then
echo "export COPILOT_HERE_APP_IMAGE=\"${REGISTRY}:copilot-${ONLY_VARIANT}\"" >> "$ENV_FILE"
fi
fi
echo "π Airlock + --dind: source the generated env file to pin to local builds"
echo " source ${ENV_FILE}"
echo ""
echo " Or paste these directly:"
sed -n '/^export /p' "$ENV_FILE" | sed 's/^/ /'