Skip to content

Commit b66d624

Browse files
committed
hide token in a secret during docker build
1 parent 5d0a83f commit b66d624

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

.github/workflows/docker.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,13 @@ jobs:
8585
cache-from: type=gha
8686
cache-to: type=gha,mode=max
8787
build-args: |
88-
GH_TOKEN=${{ secrets.GIT_ACCESS_TOKEN }}
8988
PRIVATE_REPO_HOST=github.com/scality
9089
BUILD_DATE=${{ fromJson(steps.meta.outputs.json)['org.opencontainers.image.created'] }}
9190
GIT_COMMIT=${{ github.sha }}
9291
SOURCE_DATE_EPOCH=${{ env.SOURCE_DATE_EPOCH }}
9392
VERSION=${{ inputs.version || github.sha }}
93+
secrets: |
94+
gh_token=${{ secrets.GIT_ACCESS_TOKEN }}
9495
9596
- name: Generate GitHub SLSA provenance
9697
uses: actions/attest-build-provenance@v1

Dockerfile

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Build the manager binary
22
FROM golang:1.24 AS builder
33

4-
ARG GH_TOKEN
5-
64
ARG PRIVATE_REPO_HOST=github.com/scality
75

86
ARG TARGETOS
@@ -12,16 +10,19 @@ WORKDIR /workspace
1210

1311
RUN go env -w GOPRIVATE=${PRIVATE_REPO_HOST}
1412

15-
RUN if [ -z "$GH_TOKEN" ]; then echo "GH_TOKEN is missing"; exit 1; fi && \
16-
git config --global url."https://oauth2:${GH_TOKEN}@${PRIVATE_REPO_HOST}".insteadOf "https://${PRIVATE_REPO_HOST}"
17-
1813
# Copy the Go Modules manifests
1914
COPY go.mod go.mod
2015
COPY go.sum go.sum
2116

22-
# cache deps before building and copying source so that we don't need to re-download as much
23-
# and so that source changes don't invalidate our downloaded layer
24-
RUN go mod download
17+
# Cache deps before building and copying source. Use BuildKit secret for GH_TOKEN so it
18+
# never appears in build args or image history. Pass with: --secret id=gh_token,env=GH_TOKEN
19+
RUN --mount=type=secret,id=gh_token \
20+
if [ -f /run/secrets/gh_token ] && [ -s /run/secrets/gh_token ]; then \
21+
token=$(cat /run/secrets/gh_token); \
22+
git config --global url."https://oauth2:${token}@${PRIVATE_REPO_HOST}".insteadOf "https://${PRIVATE_REPO_HOST}"; \
23+
fi && \
24+
go mod download && \
25+
rm -f /root/.gitconfig
2526

2627
# Copy the go source
2728
COPY cmd/main.go cmd/main.go

0 commit comments

Comments
 (0)