
اعمل build مرة واحدة وانشره في كل مكان: ليه الـ config مالوش مكان في الـ build
- كاتب موثّق
محتويات المقال
الفكرة الغلط الشائعة
--configuration بتـ "provides a mechanism for configuring environment-specific data in your application." وعشان تضيف staging، نفس الصفحة بتقولك انسخ environment.ts لـ environment.staging.ts وضيف staging build configuration. ولو الطريقة الرسمية إنك تحط API URL هي build flag، يبقى build لكل environment شكله هو التصميم الصح.
اللي بيحصل فعلًا
الـ build والـ release والـ run تلات مراحل منفصلة
الـ build التاني artifact تاني
RUN جاي من الـ cache ممكن يبقى شايل packages مختلفة على أجهزة مختلفة. وحتى لو كل الـ inputs مثبّتة، الـ build اللي لكل environment مختلف عن قصد، لأن الـ settings جزء منه. وفي التجربة بتاعتنا، الـ images بتاعة الـ staging والـ production خدوا image IDs مختلفة، يعني الـ image اللي اختبرتها مش هي اللي بتنزّلها. والـ بتاخد الثقة بتاعتها من إنها بتعدّي نفس الـ artifact على كل stage.
الـ config هو اللي بيختلف بين الـ deploys
القيم المتخبزة في الـ image بتمشي معاها
docker history طبع الـ URL بتاع الـ production مرتين، مرة من الـ ENV ومرة من الـ ARG نفسه.
الحل: image واحدة، والـ config وقت التشغيل
server.ts (تحت) على Docker 29.4.3 بـ node:24.22.2-alpine3.24. والغلط بيخبز الـ URL جوه الـ image بـ build argument:
# Dockerfile: ARG PAYMENTS_URL, then ENV PAYMENTS_URL=$PAYMENTS_URLdocker build --build-arg PAYMENTS_URL=https://sandbox.payments.example \ -t shop-api:1.4.0-staging .docker build --build-arg PAYMENTS_URL=https://api.payments.example \ -t shop-api:1.4.0-prod .docker image ls --format '{{.Tag}} {{.ID}}' shop-api# 1.4.0-prod e6cf6bf4ae98# 1.4.0-staging 5911098ae226docker history --format '{{.CreatedBy}}' shop-api:1.4.0-prod | grep PAYMENTS# ENV PAYMENTS_URL=https://api.payments.example# ARG PAYMENTS_URL=https://api.payments.exampledocker build -t shop-api:1.4.0 .docker run -d --name shop-staging \ -e PAYMENTS_URL=https://sandbox.payments.example shop-api:1.4.0docker run -d --name shop-prod \ -e PAYMENTS_URL=https://api.payments.example shop-api:1.4.0docker logs shop-prod# shop-api 1.4.0, payments at https://api.payments.exampledocker inspect --format '{{.Name}} {{.Image}}' shop-staging shop-prod# /shop-staging sha256:c6339bd7a8c12e3a83c260e9b2357033791d8896232c273dab406b7eebc1c7f6# /shop-prod sha256:c6339bd7a8c12e3a83c260e9b2357033791d8896232c273dab406b7eebc1c7f6import { createServer } from "node:http";// Per-deploy config comes from the environment at startupconst paymentsUrl = process.env.PAYMENTS_URL;if (!paymentsUrl) { throw new Error("PAYMENTS_URL is not set");}createServer((_req, res) => { res.end("ok");}).listen(8080, () => { console.log(`shop-api 1.4.0, payments at ${paymentsUrl}`);});PAYMENTS_URL بتقفل على طول بـ Error: PAYMENTS_URL is not set، وده أحسن بكتير من إنها تفشل مع أول عملية دفع.
تطبقها إزاي في مشروعك
اكتب الحاجات اللي بتختلف بين الـ deploys لف على الـ build scripts والـ Dockerfiles والـ pipeline ودوّر على القيم اللي بتختلف حسب الـ environment: الـ URLs والـ hostnames والـ credentials والـ feature switches. كل واحدة فيهم config. طلّعها بره الـ build بدّل الـ build arguments والـ compile-time defines والـ file replacements بقيم بتتقري وقت ما الـ app يقوم، وخلّيه يفشل على طول لو قيمة ناقصة. عدّي artifact واحد اعمل build مرة واحدة لكل commit، وادّيله tag بالـ version أو الـ commit SHA، وسجّل الـ digest بتاعه، ونزّل الـ digest ده على الـ staging وبعدين الـ production. ومقال الـ latest tag ورّانا ليه الـ digest مهم. ادّي الـ secrets للـ container مش للـ image مرّر الـ credentials وقت التشغيل من الـ secret store بتاع الـ platform بتاعتك، وعمرك ما تحطها build arguments، واعمل docker historyعلى الـ images اللي عندك دلوقتي وشوف لو فيه حاجة سرّبت.
الخلاصة
اعمل build مرة واحدة، والـ config بيدخل في الـ release، فـ artifact واحد يقدر يشتغل في كل environment. الـ build اللي لكل environment معناه إن الـ production بيشغّل image عمرها ما اتختبرت. الـ config هو أي حاجة بتختلف بين الـ deploys، اقراه وقت ما الـ app يقوم وخلّيه يفشل على طول لو ناقص. الـ build arguments وقيم الـ ENVبتفضل جوه الـ image، فعمرك ما تستخدمها للـ secrets.عدّي نفس الـ digest من الـ staging للـ production، واعمل rollback بإنك تنزّل اللي قبله تاني.
التعليقات (0)