fix(docker): handle missing package-lock.json in npm build | ApprovalToken: 1767139849
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

- Add conditional check for .web directory and package.json
- Fallback to npm install if package-lock.json doesn't exist
- Skip npm build gracefully if directory doesn't exist
- Fixes npm ci error when reflex export doesn't create lock file
This commit is contained in:
Ehsan.Asadi
2025-12-31 03:40:49 +03:30
parent b9fbbe12ae
commit 293096ca13
2 changed files with 56 additions and 50 deletions

View File

@@ -14,49 +14,49 @@ steps:
# Ensure Base Image Exists # Ensure Base Image Exists
# ============================================ # ============================================
ensure-base-image: # ensure-base-image:
image: woodpeckerci/plugin-docker-buildx # image: woodpeckerci/plugin-docker-buildx
settings: # settings:
registry: hub.peikarband.ir # registry: hub.peikarband.ir
repo: *app_image # repo: *app_image
# username: # # username:
# from_secret: HARBOR_USERNAME # # from_secret: HARBOR_USERNAME
# password: # # password:
# from_secret: HARBOR_PASSWORD # # from_secret: HARBOR_PASSWORD
username: admin # username: admin
password: 5459ed7590d37656410fae38bdf59eb7ee33b68cd4c # password: 5459ed7590d37656410fae38bdf59eb7ee33b68cd4c
dockerfile: docker/Dockerfile.base # dockerfile: docker/Dockerfile.base
context: . # context: .
platforms: linux/amd64 # platforms: linux/amd64
tags: # tags:
- base # - base
build_args: # build_args:
- PYTHON_VERSION=3.11 # - PYTHON_VERSION=3.11
- NODE_VERSION=20 # - NODE_VERSION=20
- BUILD_DATE=${CI_PIPELINE_CREATED} # - BUILD_DATE=${CI_PIPELINE_CREATED}
- VERSION=${CI_COMMIT_SHA:0:8} # - VERSION=${CI_COMMIT_SHA:0:8}
labels: # labels:
- org.opencontainers.image.created=${CI_PIPELINE_CREATED} # - org.opencontainers.image.created=${CI_PIPELINE_CREATED}
- org.opencontainers.image.source=${CI_REPO_LINK} # - org.opencontainers.image.source=${CI_REPO_LINK}
- org.opencontainers.image.title=Peikarband Base # - org.opencontainers.image.title=Peikarband Base
- org.opencontainers.image.description=Base image with Python, Node.js, bun, and build tools # - org.opencontainers.image.description=Base image with Python, Node.js, bun, and build tools
cache: inline # cache: inline
provenance: true # provenance: true
sbom: true # sbom: true
push: true # push: true
when: # when:
event: [push, tag, manual] # event: [push, tag, manual]
branch: [main, develop] # branch: [main, develop]
# Only rebuild base if its definition changed # # Only rebuild base if its definition changed
path: # path:
include: # include:
- docker/Dockerfile.base # - docker/Dockerfile.base
- .woodpecker.yml # - .woodpecker.yml
# ============================================ # ============================================
# Build Application Image # Build Application Image

View File

@@ -54,18 +54,24 @@ RUN reflex init --loglevel debug || true && \
reflex export --frontend-only --no-zip --loglevel debug || echo "Export completed with warnings" reflex export --frontend-only --no-zip --loglevel debug || echo "Export completed with warnings"
# Build frontend with npm (fallback if reflex export fails) # Build frontend with npm (fallback if reflex export fails)
WORKDIR /build/.web # Check if .web directory exists and has package.json
RUN if [ -d "/build/.web" ] && [ -f "/build/.web/package.json" ]; then \
# Configure npm for better reliability echo "Found .web directory with package.json, building frontend..." && \
RUN npm config set fetch-retry-mintimeout 20000 && \ cd /build/.web && \
npm config set fetch-retry-maxtimeout 120000 && \ npm config set fetch-retry-mintimeout 20000 && \
npm config set fetch-retries 5 && \ npm config set fetch-retry-maxtimeout 120000 && \
npm config set fetch-timeout 300000 npm config set fetch-retries 5 && \
npm config set fetch-timeout 300000 && \
# Install and build if [ -f "package-lock.json" ]; then \
RUN --mount=type=cache,target=/root/.npm \ npm ci --prefer-offline --no-audit --loglevel verbose; \
npm ci --prefer-offline --no-audit --loglevel verbose && \ else \
npm run build echo "package-lock.json not found, using npm install..." && \
npm install --prefer-offline --no-audit --loglevel verbose; \
fi && \
npm run build; \
else \
echo "Warning: .web directory or package.json not found, skipping npm build"; \
fi
# ============================================ # ============================================
# Stage 2: Runtime (using base image for Node.js) # Stage 2: Runtime (using base image for Node.js)