Skip to content

Commit 1d29bdc

Browse files
chengzeyiclaude
andcommitted
Harden image builds: allowlist build context, opt-in push
- Add allowlist .dockerignore so only files the build needs enter the context; anything new in the repo stays out of images by default - build_and_push.sh no longer pushes by default; pass -p to push - Pass the version via WAVESPEED_VERSION build-arg since setuptools_scm can no longer read it from the excluded .git Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent aa2ba7d commit 1d29bdc

3 files changed

Lines changed: 29 additions & 9 deletions

File tree

.dockerignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
*
2+
!src
3+
!pyproject.toml
4+
!setup.cfg
5+
!MANIFEST.in
6+
!README.md
7+
!LICENSE
8+
!images/*/requirements.txt
9+
!images/*/handler.py
10+
**/__pycache__
11+
**/*.pyc
12+
**/*.egg-info

images/build_and_push.sh

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,23 @@ REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
1010
DIRECTORY=
1111
DOCKERFILE=Dockerfile
1212
VARIANT=
13-
LOCAL=0
13+
PUSH=0
1414

1515
# Parse command line args
16-
while getopts ":hd:f:v:l" opt; do
16+
while getopts ":hd:f:v:pl" opt; do
1717
case $opt in
1818
d) DIRECTORY="$OPTARG" ;;
1919
f) DOCKERFILE="$OPTARG" ;;
2020
v) VARIANT="$OPTARG" ;;
21-
l) LOCAL=1 ;;
22-
h) echo "Usage: $0 -d <directory> [-f dockerfile] [-v variant] [-l (local only)]"
21+
p) PUSH=1 ;;
22+
l) PUSH=0 ;; # kept for compatibility; local-only is now the default
23+
h) echo "Usage: $0 -d <directory> [-f dockerfile] [-v variant] [-p (push)]"
2324
echo ""
2425
echo "Options:"
2526
echo " -d Directory name under images/ (required)"
2627
echo " -f Dockerfile name (default: Dockerfile)"
2728
echo " -v Variant suffix for tag"
28-
echo " -l Local build only, skip push"
29+
echo " -p Push to Docker Hub after build (default: local build only)"
2930
exit 0 ;;
3031
:) echo "Option -$OPTARG requires an argument" >&2
3132
exit 1 ;;
@@ -36,7 +37,7 @@ done
3637

3738
if [ -z "$DIRECTORY" ]; then
3839
echo "Error: Missing directory argument (-d)" >&2
39-
echo "Usage: $0 -d <directory> [-f dockerfile] [-v variant] [-l]" >&2
40+
echo "Usage: $0 -d <directory> [-f dockerfile] [-v variant] [-p]" >&2
4041
exit 1
4142
fi
4243

@@ -59,6 +60,8 @@ else
5960
fi
6061

6162
FULL_TAG="${DOCKERHUB_USERNAME}/${IMAGE_NAME}:${TAG}"
63+
WAVESPEED_VERSION="$(git -C "$REPO_ROOT" describe --tags --abbrev=0 2>/dev/null | sed 's/^v//')"
64+
WAVESPEED_VERSION="${WAVESPEED_VERSION:-0.0.0}"
6265
DOCKERFILE_PATH="${SCRIPT_DIR}/${DIRECTORY}/${DOCKERFILE}"
6366
PLATFORM="linux/amd64"
6467

@@ -80,12 +83,14 @@ if [ -z "$VARIANT" ]; then
8083
--platform "$PLATFORM" \
8184
-t "$FULL_TAG" \
8285
-f "$DOCKERFILE_PATH" \
86+
--build-arg WAVESPEED_VERSION="$WAVESPEED_VERSION" \
8387
"$REPO_ROOT"
8488
else
8589
docker buildx build \
8690
--platform "$PLATFORM" \
8791
-t "$FULL_TAG" \
8892
-f "$DOCKERFILE_PATH" \
93+
--build-arg WAVESPEED_VERSION="$WAVESPEED_VERSION" \
8994
--build-arg VARIANT="$VARIANT" \
9095
"$REPO_ROOT"
9196
fi
@@ -94,14 +99,14 @@ echo "" >&2
9499
echo "Docker image built: $FULL_TAG" >&2
95100

96101
# Push the Docker image to Docker Hub
97-
if [ $LOCAL -eq 0 ]; then
102+
if [ $PUSH -eq 1 ]; then
98103
echo "" >&2
99104
echo "=== Pushing to Docker Hub ===" >&2
100105
docker push "$FULL_TAG"
101106
echo "Pushed: $FULL_TAG" >&2
102107
else
103108
echo "" >&2
104-
echo "Skipping push (local mode)" >&2
109+
echo "Skipping push (default; pass -p to push)" >&2
105110
fi
106111

107112
# Output the full tag (useful for scripting)

images/test_worker/Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ COPY ./$THIS_DIR/requirements.txt /opt/work_dir/requirements.txt
1919
RUN $PY -m pip install --no-build-isolation --no-cache-dir -r /opt/work_dir/requirements.txt
2020

2121
# Install wavespeed SDK from local source
22+
# .git is excluded from the build context, so setuptools_scm needs the version passed in
2223
COPY . /opt/wavespeed-python
23-
RUN $PY -m pip install --no-build-isolation --no-cache-dir -e /opt/wavespeed-python
24+
ARG WAVESPEED_VERSION=0.0.0
25+
RUN SETUPTOOLS_SCM_PRETEND_VERSION_FOR_WAVESPEED="$WAVESPEED_VERSION" \
26+
$PY -m pip install --no-build-isolation --no-cache-dir -e /opt/wavespeed-python
2427

2528
COPY ./$THIS_DIR/handler.py /opt/work_dir/handler.py
2629

0 commit comments

Comments
 (0)