Skip to content

Commit b52aa96

Browse files
jlianCopilot
andauthored
Fix Codespaces prebuild k3d startup (#189)
## Summary - keep `onCreateCommand` prebuild-safe by moving k3d cluster creation out of it - create the k3d cluster lazily from `postStartCommand` for real codespaces - make the `postStartCommand` environment setup idempotent across restarts ## Why Recent Codespaces prebuilds for `.devcontainer/devcontainer.json` have been intermittently failing after the devcontainer setup completes, during GitHub's template/snapshot phase: ```text Jobs failed, exiting the agent. Job 'ShrinkExt4' did not succeed. ``` The prebuild logs show `onCreateCommand` successfully creating a k3d cluster before GitHub tries to snapshot/shrink the filesystem. Codespaces prebuilds run `onCreateCommand`, so leaving nested Docker/k3d state around during the snapshot step is risky and appears to be the source of the flake. GitHub docs confirm prebuilds run setup through `onCreateCommand` before taking the snapshot: https://docs.github.com/en/codespaces/prebuilding-your-codespaces/about-github-codespaces-prebuilds#the-prebuild-process ## Validation - `bash -n .devcontainer/onCreateCommand.sh` - `bash -n .devcontainer/postStartCommand.sh` - Ran `postStartCommand.sh` twice with `SKIP_K3D_CLUSTER_CREATE=1` and a temp home to verify `.bashrc` updates are idempotent. --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 0072eb6 commit b52aa96

2 files changed

Lines changed: 50 additions & 11 deletions

File tree

.devcontainer/onCreateCommand.sh

100644100755
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
#!/bin/sh
1+
#!/usr/bin/env bash
22

33
set -o errexit
44
set -o nounset
55
set -o pipefail
66

77
echo "Starting On Create Command"
88

9-
# Copy the custom first run notice over
9+
# Copy the custom first run notice over.
1010
sudo cp .devcontainer/welcome.txt /usr/local/etc/vscode-dev-containers/first-run-notice.txt
1111

12-
# Create k3d cluster and forwarded ports
13-
k3d cluster delete
14-
k3d cluster create \
15-
-p '1883:1883@loadbalancer' \
16-
-p '8883:8883@loadbalancer'
12+
# Keep this script prebuild-safe. GitHub Codespaces prebuilds run onCreateCommand
13+
# before snapshotting the filesystem, so don't start Docker/k3d workloads here.
14+
# The k3d cluster is created lazily in postStartCommand.sh for real codespaces.
1715

1816
echo "Ending On Create Command"

.devcontainer/postStartCommand.sh

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,46 @@
1-
echo 'export CODESPACES="FALSE"' >> ~/.bashrc
2-
echo 'export CLUSTER_NAME=${CODESPACE_NAME%-*}-codespace' >> ~/.bashrc
3-
source ~/.bashrc
1+
#!/usr/bin/env bash
42

5-
echo -e "Environment: \nSUBSCRIPTION_ID: $SUBSCRIPTION_ID \nRESOURCE_GROUP: $RESOURCE_GROUP \nLOCATION: $LOCATION \nCLUSTER_NAME: $CLUSTER_NAME"
3+
set -o errexit
4+
set -o nounset
5+
set -o pipefail
6+
7+
BASHRC="${HOME}/.bashrc"
8+
touch "${BASHRC}"
9+
10+
# Keep the helper environment exports idempotent across codespace restarts.
11+
grep -qxF 'export CODESPACES="FALSE"' "${BASHRC}" || echo 'export CODESPACES="FALSE"' >> "${BASHRC}"
12+
if ! grep -qxF '# devcontainer-cluster-name' "${BASHRC}"; then
13+
cat >> "${BASHRC}" <<'EOF'
14+
# devcontainer-cluster-name
15+
if [[ -n "${CODESPACE_NAME:-}" ]]; then
16+
export CLUSTER_NAME="${CODESPACE_NAME%-*}-codespace"
17+
fi
18+
EOF
19+
fi
20+
21+
export CODESPACES="FALSE"
22+
if [[ -n "${CODESPACE_NAME:-}" ]]; then
23+
export CLUSTER_NAME="${CODESPACE_NAME%-*}-codespace"
24+
else
25+
export CLUSTER_NAME="${CLUSTER_NAME:-k3s-default}"
26+
fi
27+
28+
printf 'Environment:\nSUBSCRIPTION_ID: %s\nRESOURCE_GROUP: %s\nLOCATION: %s\nCLUSTER_NAME: %s\n' \
29+
"${SUBSCRIPTION_ID:-}" \
30+
"${RESOURCE_GROUP:-}" \
31+
"${LOCATION:-}" \
32+
"${CLUSTER_NAME}"
33+
34+
if [[ "${SKIP_K3D_CLUSTER_CREATE:-}" == "1" ]]; then
35+
echo "Skipping k3d cluster creation because SKIP_K3D_CLUSTER_CREATE=1."
36+
exit 0
37+
fi
38+
39+
if k3d cluster list -o json 2>/dev/null | grep -q '"name"[[:space:]]*:[[:space:]]*"k3s-default"'; then
40+
echo "k3d cluster 'k3s-default' already exists."
41+
else
42+
echo "Creating k3d cluster 'k3s-default'."
43+
k3d cluster create \
44+
-p '1883:1883@loadbalancer' \
45+
-p '8883:8883@loadbalancer'
46+
fi

0 commit comments

Comments
 (0)