From bf6f737b230a5cddc6b5f89c759d588b5ac8f576 Mon Sep 17 00:00:00 2001 From: phonk Date: Sat, 14 Nov 2026 16:16:10 +0000 Subject: [PATCH 1/2] =?UTF-8?q?feat(reusable-build):=20guard=20nh=C3=A1nh?= =?UTF-8?q?=20production,=20fail=20n=E1=BA=BFu=20bundle=20ch=E1=BB=A9a=20h?= =?UTF-8?q?ost=20backend=20staging=20tr=C6=B0=E1=BB=9Bc=20khi=20push?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/reusable-build.yml | 30 ++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/.github/workflows/reusable-build.yml b/.github/workflows/reusable-build.yml index f24f24c..ff7960e 100644 --- a/.github/workflows/reusable-build.yml +++ b/.github/workflows/reusable-build.yml @@ -95,7 +95,7 @@ jobs: run: | echo "${{ secrets.DOCKER_HUB_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_HUB_USERNAME || 'techainer1t' }}" --password-stdin \ || echo "login skipped/failed -> relying on ambient docker auth" - - name: Build & push + - name: Build run: | set -euo pipefail ARGS=() @@ -105,7 +105,33 @@ jobs: # Dockerfile phải có `ARG ENV_HASH` trước RUN build thì mới ăn. [ -f .env ] && ARGS+=(--build-arg "ENV_HASH=$(sha256sum .env | cut -d' ' -f1)") docker build "${ARGS[@]}" -t "${{ inputs.image }}:${{ steps.meta.outputs.tag }}" -f "${{ inputs.dockerfile }}" "${{ inputs.context }}" - docker push "${{ inputs.image }}:${{ steps.meta.outputs.tag }}" + # GUARD (chỉ nhánh production): quét output build trong image TRƯỚC khi push; FAIL nếu bundle chứa host backend của staging + # (api-staging., go-staging., hasura/auth-staging.) => bundle prod trỏ nhầm môi trường (env/cache staging lọt vào build prod). + # Host staging khác (lms-staging, smart-class-staging, storage-staging...) nằm trong bảng ánh xạ host->môi trường của nhiều app + # nên chỉ cảnh báo (::warning::), không fail. + - name: Guard - bundle production không chứa host backend staging + if: ${{ github.ref_name == 'production' }} + run: | + set -euo pipefail + IMG="${{ inputs.image }}:${{ steps.meta.outputs.tag }}" + OUT="$(mktemp -d)" + CID="$(docker create "$IMG" /bin/true 2>/dev/null || docker create --entrypoint /bin/true "$IMG")" + trap 'docker rm -f "$CID" >/dev/null 2>&1 || true; rm -rf "$OUT"' EXIT + # Chỉ giải nén chỗ chứa output build (Next standalone/static, Vite dist, nginx html); bỏ node_modules. + docker export "$CID" | tar -x -C "$OUT" --wildcards --exclude='*/node_modules/*' \ + 'app/.next/*' 'app/dist/*' 'app/build/*' 'app/public/*' 'app/out/*' 'usr/share/nginx/html/*' 2>/dev/null || true + N=$(find "$OUT" -type f | wc -l) + if [ "$N" -eq 0 ]; then echo "::warning::guard: không tìm thấy output build trong image (đường dẫn lạ), bỏ qua quét"; exit 0; fi + FAIL_RE='(api|go|hasura|auth)(-[a-z0-9]+)*-staging\.[a-z0-9.-]*(truonghocsoquocgia|thsqg|gkebooks)\.[a-z]+' + WARN_RE='[a-z0-9.-]*(-staging|staging\.)[a-z0-9.-]*(truonghocsoquocgia|thsqg)\.vn' + if grep -rEIohi "$FAIL_RE" "$OUT" | sort | uniq -c | sort -rn | tee "$OUT.hits" | grep -q .; then + echo "::error::Bundle production chứa host backend staging (sai môi trường) -> KHÔNG push image:"; cat "$OUT.hits" + exit 1 + fi + grep -rEIohi "$WARN_RE" "$OUT" | sort | uniq -c | sort -rn | head -10 | sed 's/^/::warning::host staging trong bundle prod (kiểm tay nếu là bảng ánh xạ): /' || true + echo "guard OK: $N tệp đã quét, không có host backend staging" + - name: Push + run: docker push "${{ inputs.image }}:${{ steps.meta.outputs.tag }}" - name: Cleanup if: always() run: | From 93990f20b77a79d3e392815bc275659cb70529fc Mon Sep 17 00:00:00 2001 From: phonk Date: Tue, 17 Nov 2026 07:18:17 +0000 Subject: [PATCH 2/2] =?UTF-8?q?fix(reusable-build):=20guard=20ch=E1=BB=89?= =?UTF-8?q?=20fail=20v=E1=BB=9Bi=20URL=20c=C3=B3=20scheme=20t=E1=BB=9Bi=20?= =?UTF-8?q?backend=20staging,=20s=E1=BB=ADa=20b=E1=BB=8F=20s=C3=B3t=20do?= =?UTF-8?q?=20pipefail?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Host trần trong bảng ánh xạ (posthog.ts của smart-class) làm fail nhầm build production. tee | grep -q dưới pipefail có thể SIGPIPE và bỏ qua hit thật; ghi file rồi test -s. Kiểm trên image thật: prod smart-class/admin/lms-admin OK; image staging lms-admin fail đúng. --- .github/workflows/reusable-build.yml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/workflows/reusable-build.yml b/.github/workflows/reusable-build.yml index ff7960e..6bf542a 100644 --- a/.github/workflows/reusable-build.yml +++ b/.github/workflows/reusable-build.yml @@ -106,7 +106,7 @@ jobs: [ -f .env ] && ARGS+=(--build-arg "ENV_HASH=$(sha256sum .env | cut -d' ' -f1)") docker build "${ARGS[@]}" -t "${{ inputs.image }}:${{ steps.meta.outputs.tag }}" -f "${{ inputs.dockerfile }}" "${{ inputs.context }}" # GUARD (chỉ nhánh production): quét output build trong image TRƯỚC khi push; FAIL nếu bundle chứa host backend của staging - # (api-staging., go-staging., hasura/auth-staging.) => bundle prod trỏ nhầm môi trường (env/cache staging lọt vào build prod). + # (URL có scheme tới api-staging., go-staging., hasura/auth-staging.) => bundle prod trỏ nhầm môi trường (env/cache staging lọt vào build prod). # Host staging khác (lms-staging, smart-class-staging, storage-staging...) nằm trong bảng ánh xạ host->môi trường của nhiều app # nên chỉ cảnh báo (::warning::), không fail. - name: Guard - bundle production không chứa host backend staging @@ -116,15 +116,17 @@ jobs: IMG="${{ inputs.image }}:${{ steps.meta.outputs.tag }}" OUT="$(mktemp -d)" CID="$(docker create "$IMG" /bin/true 2>/dev/null || docker create --entrypoint /bin/true "$IMG")" - trap 'docker rm -f "$CID" >/dev/null 2>&1 || true; rm -rf "$OUT"' EXIT + trap 'docker rm -f "$CID" >/dev/null 2>&1 || true; rm -rf "$OUT" "$OUT.hits"' EXIT # Chỉ giải nén chỗ chứa output build (Next standalone/static, Vite dist, nginx html); bỏ node_modules. docker export "$CID" | tar -x -C "$OUT" --wildcards --exclude='*/node_modules/*' \ 'app/.next/*' 'app/dist/*' 'app/build/*' 'app/public/*' 'app/out/*' 'usr/share/nginx/html/*' 2>/dev/null || true N=$(find "$OUT" -type f | wc -l) if [ "$N" -eq 0 ]; then echo "::warning::guard: không tìm thấy output build trong image (đường dẫn lạ), bỏ qua quét"; exit 0; fi - FAIL_RE='(api|go|hasura|auth)(-[a-z0-9]+)*-staging\.[a-z0-9.-]*(truonghocsoquocgia|thsqg|gkebooks)\.[a-z]+' + FAIL_RE='(https?|wss?)://(api|go|hasura|auth)(-[a-z0-9]+)*-staging\.[a-z0-9.-]*(truonghocsoquocgia|thsqg|gkebooks)\.[a-z]+' WARN_RE='[a-z0-9.-]*(-staging|staging\.)[a-z0-9.-]*(truonghocsoquocgia|thsqg)\.vn' - if grep -rEIohi "$FAIL_RE" "$OUT" | sort | uniq -c | sort -rn | tee "$OUT.hits" | grep -q .; then + # Ghi ra file rồi mới kiểm: `tee | grep -q` dưới pipefail có thể SIGPIPE và bỏ sót hit thật. + { grep -rEIohi "$FAIL_RE" "$OUT" || true; } | sort | uniq -c | sort -rn > "$OUT.hits" + if [ -s "$OUT.hits" ]; then echo "::error::Bundle production chứa host backend staging (sai môi trường) -> KHÔNG push image:"; cat "$OUT.hits" exit 1 fi