Skip to content

Commit

Permalink
Added workaround for not being able to use build args in entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tlbdk committed Nov 21, 2019
1 parent c2f14f8 commit 3d3a976
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode
auth-wrapper
Dockerfile
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ARG WRAP_IMAGE
ARG WRAP_ENTRYPOINT
ARG WRAP_COMMAND
ARG SSH_KEY_PATH

# Build image
Expand All @@ -23,12 +23,12 @@ RUN CGO_ENABLED=0 GOOS=linux go build -o auth-wrapper -ldflags "-X 'main.version
# Production image
FROM ${WRAP_IMAGE} as production

ARG WRAP_ENTRYPOINT
ENV WRAP_ENTRYPOINT=${WRAP_ENTRYPOINT}
ARG WRAP_COMMAND
ENV WRAP_COMMAND=${WRAP_COMMAND}

ARG SSH_KEY_PATH
ENV SSH_KEY_PATH=${SSH_KEY_PATH}

COPY --from=builder /app/auth-wrapper /opt/auth-wrapper

ENTRYPOINT [ "/opt/auth-wrapper", "${WRAP_ENTRYPOINT}" ]
ENTRYPOINT ["/opt/auth-wrapper"]
4 changes: 2 additions & 2 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ steps:
args: [
'build',
'--build-arg=WRAP_IMAGE=gcr.io/cloud-builders/git',
'--build-arg=WRAP_ENTRYPOINT=git',
'--build-arg=WRAP_COMMAND=git',
'--build-arg=SSH_KEY_PATH=kms://projects/connectedcars-staging/locations/global/keyRings/cloudbuilder/cryptoKeys/ssh-key/cryptoKeyVersions/3',
'--tag=gcr.io/$PROJECT_ID/$REPO_NAME-git.$BRANCH_NAME:$COMMIT_SHA',
'--tag=gcr.io/$PROJECT_ID/$REPO_NAME-git.$BRANCH_NAME:latest',
Expand All @@ -31,7 +31,7 @@ steps:
args: [
'build',
'--build-arg=WRAP_IMAGE=gcr.io/cloud-builders/docker',
'--build-arg=WRAP_ENTRYPOINT=docker',
'--build-arg=WRAP_COMMAND=docker',
'--build-arg=SSH_KEY_PATH=kms://projects/connectedcars-staging/locations/global/keyRings/cloudbuilder/cryptoKeys/ssh-key/cryptoKeyVersions/3',
'--tag=gcr.io/$PROJECT_ID/$REPO_NAME-docker.$BRANCH_NAME:$COMMIT_SHA',
'--tag=gcr.io/$PROJECT_ID/$REPO_NAME-docker.$BRANCH_NAME:latest',
Expand Down
22 changes: 15 additions & 7 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,21 @@ func main() {
os.Unsetenv("SSH_KEY_PATH")
os.Unsetenv("SSH_KEY_PASSWORD")

// Setup exec command
command := os.Args[1]
args := os.Args[2:]
var command string
var args []string
wrapCommand := os.Getenv("WRAP_COMMAND")
if wrapCommand != "" {
command = wrapCommand
args = os.Args[1:]
} else {
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "auth-wrapper cmd args")
os.Exit(1)
}
// Setup exec command
command = os.Args[1]
args = os.Args[2:]
}

exitCode, err := runWithSSHAgent(command, args, sshKeyPath, sshKeyPassword)
if err != nil {
Expand Down Expand Up @@ -77,10 +89,6 @@ func runWithSSHAgent(command string, args []string, sshKeyPath string, sshKeyPas
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

if len(os.Args) < 2 {
return 1, fmt.Errorf("auth-wrapper cmd args")
}

if err := cmd.Start(); err != nil {
return 1, fmt.Errorf("cmd.Start: %v", err)
}
Expand Down

0 comments on commit 3d3a976

Please sign in to comment.