diff --git a/Jenkinsfile b/Jenkinsfile index 14cddda..3c694a9 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -14,6 +14,7 @@ pipeline { APP_NAME = 'no-whatever' CI_IMAGE = 'mcr.microsoft.com/playwright:v1.51.1-jammy' NPM_REGISTRY = 'https://registry.npmmirror.com' + BUILDX_CACHE_DIR = '.buildx-cache' } triggers { @@ -77,7 +78,7 @@ pipeline { } } - stage('CI Gate (Lint + TypeCheck + Unit + Smoke E2E)') { + stage('CI Gate (Lint + Unit + Smoke E2E)') { options { timeout(time: 30, unit: 'MINUTES') } @@ -89,7 +90,7 @@ pipeline { -e HOME=/tmp \ -e RUN_COVERAGE=${RUN_COVERAGE} \ ${CI_DEPS_IMAGE} \ - sh -lc 'set -e; cd /workspace; npx prisma generate; npm run lint; npx tsc --noEmit; if [ "$RUN_COVERAGE" = "true" ]; then npm run test:coverage; else npm run test; fi; npm run test:e2e:smoke') + sh -lc 'set -e; cd /workspace; npx prisma generate; npm run lint; if [ "$RUN_COVERAGE" = "true" ]; then npm run test:coverage; else npm run test; fi; npm run test:e2e:smoke') cleanup() { docker rm -f "$cid" >/dev/null 2>&1 || true } @@ -116,7 +117,50 @@ pipeline { withCredentials([ string(credentialsId: 'amap-api-key', variable: 'AMAP_KEY') ]) { - sh "docker build --build-arg NEXT_PUBLIC_AMAP_API_KEY=${AMAP_KEY} -t ${APP_NAME}:${BUILD_NUMBER} -t ${APP_NAME}:latest ." + sh ''' + set -e + CACHE_DIR="${BUILDX_CACHE_DIR}" + CACHE_NEW_DIR="${BUILDX_CACHE_DIR}-new" + rm -rf "$CACHE_NEW_DIR" + + if docker buildx version >/dev/null 2>&1; then + echo "using docker buildx with local cache" + docker buildx create --name "${APP_NAME}-builder" --use >/dev/null 2>&1 || true + docker buildx use "${APP_NAME}-builder" >/dev/null 2>&1 || true + mkdir -p "$CACHE_DIR" + + set +e + docker buildx build \ + --load \ + --build-arg NEXT_PUBLIC_AMAP_API_KEY="$AMAP_KEY" \ + --cache-from type=local,src="$CACHE_DIR" \ + --cache-to type=local,dest="$CACHE_NEW_DIR",mode=max \ + -t "${APP_NAME}:${BUILD_NUMBER}" \ + -t "${APP_NAME}:latest" \ + . + bx_status=$? + set -e + + if [ $bx_status -eq 0 ]; then + rm -rf "$CACHE_DIR" + mv "$CACHE_NEW_DIR" "$CACHE_DIR" + else + echo "buildx build failed, fallback to DOCKER_BUILDKIT=1 docker build" + DOCKER_BUILDKIT=1 docker build \ + --build-arg NEXT_PUBLIC_AMAP_API_KEY="$AMAP_KEY" \ + -t "${APP_NAME}:${BUILD_NUMBER}" \ + -t "${APP_NAME}:latest" \ + . + fi + else + echo "docker buildx unavailable, fallback to DOCKER_BUILDKIT=1 docker build" + DOCKER_BUILDKIT=1 docker build \ + --build-arg NEXT_PUBLIC_AMAP_API_KEY="$AMAP_KEY" \ + -t "${APP_NAME}:${BUILD_NUMBER}" \ + -t "${APP_NAME}:latest" \ + . + fi + ''' } } }