Skip to content

Commit f8ca570

Browse files
Copilotjhulten
andcommitted
Add SSH key mounting support for private repository access
Co-authored-by: jhulten <[email protected]>
1 parent f716f20 commit f8ca570

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,21 @@ Build without initializing a dotfiles repo:
1919
docker build -t my-chezmoi ./chezmoi
2020
```
2121

22-
Build and initialize with a dotfiles repo:
22+
Build and initialize with a public dotfiles repo:
2323
```bash
2424
docker build --build-arg CHEZMOI_REPO=https://github.com/username/dotfiles.git -t my-chezmoi ./chezmoi
2525
```
2626

27+
Build and initialize with a private dotfiles repo using SSH keys:
28+
```bash
29+
docker buildx build --ssh default --build-arg [email protected]:username/dotfiles.git -t my-chezmoi ./chezmoi
30+
```
31+
32+
Build and initialize with a private dotfiles repo using token:
33+
```bash
34+
docker build --build-arg CHEZMOI_REPO=https://[email protected]/username/dotfiles.git -t my-chezmoi ./chezmoi
35+
```
36+
2737
> [!WARNING]
2838
> Using `CHEZMOI_REPO` will execute code from the dotfiles repository during the build process. Only use with trusted repositories.
2939

chezmoi/Dockerfile

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,19 @@ RUN mise use --global aqua:twpayne/chezmoi@${CHEZMOI_VERSION}
1818
# Optional: Initialize chezmoi with a dotfiles repo
1919
# WARNING: This will execute code from the dotfiles repository during build.
2020
# Only use with trusted repositories.
21+
#
22+
# For public repos or repos with token:
23+
# docker build --build-arg CHEZMOI_REPO=https://github.com/user/dotfiles.git
24+
# docker build --build-arg CHEZMOI_REPO=https://[email protected]/user/dotfiles.git
25+
#
26+
# For private repos with SSH:
27+
# docker buildx build --ssh default --build-arg [email protected]:user/dotfiles.git
2128
ARG CHEZMOI_REPO=""
22-
RUN if [ -n "$CHEZMOI_REPO" ]; then \
23-
chezmoi init --apply "$CHEZMOI_REPO"; \
29+
RUN --mount=type=ssh \
30+
mkdir -p ~/.ssh && \
31+
ssh-keyscan github.com >> ~/.ssh/known_hosts 2>/dev/null || true && \
32+
if [ -n "$CHEZMOI_REPO" ]; then \
33+
chezmoi init --apply "$CHEZMOI_REPO"; \
2434
fi
2535

2636
CMD ["/bin/zsh"]

0 commit comments

Comments
 (0)