Skip to content

Commit ab8f021

Browse files
committed
Add support for securely passing ssh identity to docker build
1 parent 6468b14 commit ab8f021

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,11 @@ version is the image's digest.
260260
* `pull_tag`: *Optional.* **DEPRECATED. Use `get` and `load` instead.** Default
261261
`latest`. The tag of the repository to pull down via `pull_repository`.
262262

263+
* `ssh_identity`: *Optional.* Only applies when `docker_buildkit` is set to `1`.
264+
Set to an openssh private SSH key (i.e. -----BEGIN OPENSSH PRIVATE KEY----- ...),
265+
this identity will be passed to `docker build` via the `--ssh default` argument
266+
through a temporary `ssh-agent` instance.
267+
263268
* `tag`: **DEPRECATED - Use `tag_file` instead**
264269
* `tag_file`: *Optional.* The value should be a path to a file containing the name
265270
of the tag. When not set, the Docker build will be pushed with tag value set by

assets/out

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ import_file=$(jq -r '.params.import_file // ""' < $payload)
113113

114114
pull_repository=$(jq -r '.params.pull_repository // ""' < $payload)
115115
pull_tag=$(jq -r '.params.pull_tag // "latest"' < $payload)
116+
ssh_identity=$(jq -r '.params.ssh_identity // ""' < $payload)
116117
target_name=$(jq -r '.params.target_name // ""' < $payload)
117118

118119
if [ -n "$load" ]; then
@@ -224,6 +225,15 @@ elif [ -n "$build" ]; then
224225
fi
225226
fi
226227

228+
ssh_args=()
229+
if [ -n "$ssh_identity" ] && [ "$DOCKER_BUILDKIT" -eq 1 ]; then
230+
eval "$(ssh-agent)"
231+
trap "ssh-agent -k; $( trap -p EXIT | cut -f2 -d \' )" EXIT
232+
ssh-add <(echo "$ssh_identity")
233+
ssh_args+=("--ssh")
234+
ssh_args+=("default")
235+
fi
236+
227237
target=()
228238
if [ -n "${target_name}" ]; then
229239
target+=("--target")

dockerfiles/alpine/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ RUN apk --no-cache add \
3030
xz \
3131
util-linux \
3232
tar \
33+
openssh-client \
3334
;
3435
COPY --from=builder /assets /opt/resource
3536
RUN ln -s /opt/resource/ecr-login /usr/local/bin/docker-credential-ecr-login

dockerfiles/ubuntu/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ RUN apt-get update; \
3030
ca-certificates \
3131
curl \
3232
gnupg-agent \
33-
software-properties-common; \
33+
software-properties-common \
34+
openssh-client; \
3435
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - ; \
3536
add-apt-repository \
3637
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \

0 commit comments

Comments
 (0)