Skip to content

Commit 49d466a

Browse files
jsburckhardtCopilot
andcommitted
feat: add copilot-persistence dev container feature
Preserve ~/.copilot folder across container instances using a Docker volume and symlink, avoiding the need to reconfigure Copilot CLI after rebuilding the dev container. Co-authored-by: Copilot <[email protected]>
1 parent 1b1bcb6 commit 49d466a

6 files changed

Lines changed: 147 additions & 0 deletions

File tree

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ This repository contains a _collection_ of Features.
3030
| Codex-cli | https://github.com/openai/codex | Codex CLI is an experimental project under active development. |
3131
| ccc | https://github.com/jsburckhardt/co-config | A TUI tool to interactively configure and view GitHub Copilot CLI settings. |
3232
| Yazi | https://github.com/sxyazi/yazi | Blazing fast terminal file manager written in Rust, based on async I/O. |
33+
| Copilot Persistence || Preserve ~/.copilot folder across container instances (avoids losing Copilot configuration after rebuilding). |
3334

3435

3536

@@ -373,3 +374,21 @@ Running `yazi --version` inside the built container will print the version of ya
373374
```bash
374375
yazi --version
375376
```
377+
378+
### `copilot-persistence`
379+
380+
Persists the `~/.copilot` directory across container rebuilds using a Docker volume and symlink, so you don't lose your Copilot configuration when rebuilding your dev container.
381+
382+
```jsonc
383+
{
384+
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
385+
"features": {
386+
"ghcr.io/jsburckhardt/devcontainer-features/copilot-persistence:1": {}
387+
}
388+
}
389+
```
390+
391+
```bash
392+
# Verify the symlink exists
393+
test -L "$HOME/.copilot" && echo "copilot-persistence is active"
394+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "Copilot Persistence",
3+
"id": "copilot-persistence",
4+
"version": "1.0.0",
5+
"description": "Preserve ~/.copilot folder across container instances (avoids losing Copilot configuration after rebuilding)",
6+
"options": {},
7+
"mounts": [
8+
{
9+
"source": "${devcontainerId}-copilot",
10+
"target": "/dc/copilot",
11+
"type": "volume"
12+
}
13+
],
14+
"onCreateCommand": {
15+
"copilot-persistence": "/usr/local/share/jsburckhardt-devcontainer-features/copilot-persistence/scripts/oncreate.sh"
16+
}
17+
}

src/copilot-persistence/install.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/sh
2+
set -e
3+
4+
FEATURE_DIR="/usr/local/share/jsburckhardt-devcontainer-features/copilot-persistence"
5+
LIFECYCLE_SCRIPTS_DIR="$FEATURE_DIR/scripts"
6+
LOG_FILE="$FEATURE_DIR/log.txt"
7+
8+
mkdir -p "${FEATURE_DIR}"
9+
echo "" > "$LOG_FILE"
10+
11+
log() {
12+
echo "$1"
13+
echo "$1" >> "$LOG_FILE"
14+
}
15+
16+
log "Activating feature 'copilot-persistence'"
17+
log "User: ${_REMOTE_USER} User home: ${_REMOTE_USER_HOME}"
18+
19+
# Skip if already installed
20+
if [ -f "$FEATURE_DIR/markers/install" ]; then
21+
log "Feature 'copilot-persistence' already installed, skipping"
22+
exit 0
23+
fi
24+
25+
# Back up existing .copilot folder if present
26+
got_old_copilot_folder=false
27+
if [ -e "$_REMOTE_USER_HOME/.copilot" ]; then
28+
log "Moving existing .copilot folder to .copilot-old"
29+
mv "$_REMOTE_USER_HOME/.copilot" "$_REMOTE_USER_HOME/.copilot-old"
30+
got_old_copilot_folder=true
31+
else
32+
log "No existing .copilot folder found at '$_REMOTE_USER_HOME/.copilot'"
33+
fi
34+
35+
# Create symlink from ~/.copilot to the mounted volume
36+
ln -s /dc/copilot/ "$_REMOTE_USER_HOME/.copilot"
37+
chown -R "${_REMOTE_USER}:${_REMOTE_USER}" "$_REMOTE_USER_HOME/.copilot"
38+
39+
# Copy lifecycle scripts
40+
if [ -f oncreate.sh ]; then
41+
mkdir -p "${LIFECYCLE_SCRIPTS_DIR}"
42+
cp oncreate.sh "${LIFECYCLE_SCRIPTS_DIR}/oncreate.sh"
43+
chmod +x "${LIFECYCLE_SCRIPTS_DIR}/oncreate.sh"
44+
fi
45+
46+
# Mark as installed
47+
log "Adding marker file to indicate persistence is installed"
48+
mkdir -p "$FEATURE_DIR/markers"
49+
touch "$FEATURE_DIR/markers/install"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
FEATURE_DIR="/usr/local/share/jsburckhardt-devcontainer-features/copilot-persistence"
5+
LOG_FILE="$FEATURE_DIR/log.txt"
6+
7+
log() {
8+
echo "$1"
9+
echo "$1" >> "$LOG_FILE"
10+
}
11+
12+
# Fix permissions on the log file so the remote user can write to it
13+
if command -v sudo > /dev/null; then
14+
sudo chown -R "$(id -u):$(id -g)" "$LOG_FILE"
15+
else
16+
chown -R "$(id -u):$(id -g)" "$LOG_FILE"
17+
fi
18+
19+
log "In OnCreate script"
20+
21+
# Skip if oncreate actions already ran
22+
if [ -f "$FEATURE_DIR/markers/oncreate" ]; then
23+
log "Feature 'copilot-persistence' oncreate actions already run, skipping"
24+
exit 0
25+
fi
26+
27+
fix_permissions() {
28+
local dir="${1}"
29+
if [ ! -w "${dir}" ]; then
30+
echo "Fixing permissions of '${dir}'..."
31+
sudo chown -R "$(id -u):$(id -g)" "${dir}"
32+
echo "Done!"
33+
else
34+
echo "Permissions of '${dir}' are OK!"
35+
fi
36+
}
37+
38+
fix_permissions "/dc/copilot"
39+
40+
# Mark oncreate as done
41+
log "Adding marker file to indicate oncreate actions have been run"
42+
mkdir -p "$FEATURE_DIR/markers"
43+
if command -v sudo > /dev/null; then
44+
sudo touch "$FEATURE_DIR/markers/oncreate"
45+
else
46+
touch "$FEATURE_DIR/markers/oncreate"
47+
fi
48+
49+
log "Done"

test/_global/scenarios.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,11 @@
165165
"version": "v26.1.22"
166166
}
167167
}
168+
},
169+
"copilot-persistence": {
170+
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
171+
"features": {
172+
"copilot-persistence": {}
173+
}
168174
}
169175
}

test/copilot-persistence/test.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
source dev-container-features-test-lib
6+
check "copilot-persistence" test -L "$HOME/.copilot"
7+
reportResults

0 commit comments

Comments
 (0)