From d37eec07f9cb1c1c067fea90159123b1aa2f8daa Mon Sep 17 00:00:00 2001 From: yuri0528 <72436066+yuri0528@users.noreply.github.com> Date: Tue, 15 Aug 2023 21:40:15 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E5=89=8D=E7=AB=AF?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=20(#1161)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/.bk.development.env | 2 +- src/pages/.bk.production.env | 2 +- src/pages/bk.config.js | 3 ++- src/pages/index.html | 14 +------------- src/pages/paas-server/index.js | 6 +++--- src/pages/src/http/fetch/index.ts | 2 +- 6 files changed, 9 insertions(+), 20 deletions(-) diff --git a/src/pages/.bk.development.env b/src/pages/.bk.development.env index 80e7c420f..beced60ca 100644 --- a/src/pages/.bk.development.env +++ b/src/pages/.bk.development.env @@ -4,7 +4,7 @@ BK_SITE_URL = '' # 静态资源路径 -BK_STATIC_URL = '' +STATIC_URL = '' # 当前应用的环境,预发布环境为 stag,正式环境为 prod BK_PAAS_ENVIRONMENT = '' diff --git a/src/pages/.bk.production.env b/src/pages/.bk.production.env index 79eee9a65..ce2b1821a 100644 --- a/src/pages/.bk.production.env +++ b/src/pages/.bk.production.env @@ -4,7 +4,7 @@ BK_SITE_URL = '{{ SITE_URL }}' # 静态资源路径 -BK_STATIC_URL = '{{ BK_STATIC_URL }}' +STATIC_URL = '{{ STATIC_URL }}' # 当前应用的环境,预发布环境为 stag,正式环境为 prod BK_PAAS_ENVIRONMENT = '{{ BKPAAS_ENVIRONMENT }}' diff --git a/src/pages/bk.config.js b/src/pages/bk.config.js index 8d700efba..d268678f5 100644 --- a/src/pages/bk.config.js +++ b/src/pages/bk.config.js @@ -4,10 +4,11 @@ const mockServer = require('./mock-server'); module.exports = { host: process.env.BK_APP_HOST, port: process.env.BK_APP_PORT, - publicPath: process.env.BK_STATIC_URL, + publicPath: process.env.STATIC_URL, cache: true, open: true, replaceStatic: true, + outputAssetsDirName: '', // webpack config 配置 configureWebpack() { diff --git a/src/pages/index.html b/src/pages/index.html index 73b2975f6..fe0a0bab3 100644 --- a/src/pages/index.html +++ b/src/pages/index.html @@ -12,22 +12,10 @@ // 登录页面 window.BK_LOGIN_URL = '{{ BK_LOGIN_URL }}' // 开发环境域名 - window.AJAX_BASE_URL = '{{ AJAX_URL_PREFIX }}' + window.AJAX_BASE_URL = '{{ AJAX_BASE_URL }}' // 静态资源路径 window.STATIC_URL = '{{ STATIC_URL }}' window.CSRF_COOKIE_NAME = '{{ CSRF_COOKIE_NAME }}' - // var SITE_URL = '<%= process.env.BK_SITE_URL %>' - // var BK_STATIC_URL = '<%= process.env.BK_STATIC_URL %>' - // // 当前应用的环境,预发布环境为 stag,正式环境为 prod - // var BKPAAS_ENVIRONMENT = '<%= process.env.BK_PAAS_ENVIRONMENT %>' - // // EngineApp名称,拼接规则:bkapp-{appcode}-{BKPAAS_ENVIRONMENT} - // var BKPAAS_ENGINE_APP_NAME = '<%= process.env.BK_PAAS_ENGINE_APP_NAME %>' - // // 内部版对应ieod,外部版对应tencent,混合云版对应clouds - // var BKPAAS_ENGINE_REGION = '<%= process.env.BK_PAAS_ENGINE_REGION %>' - // // APP ID - // var BKPAAS_APP_ID = '<%= process.env.BK_PAAS_APP_ID %>' - // var BKPAAS_APP_SECRET = '<%= process.env.BK_PAAS_APP_SECRET %>' - // var BK_LOGIN_URL = '<%= process.env.BK_LOGIN_URL %>' diff --git a/src/pages/paas-server/index.js b/src/pages/paas-server/index.js index ba7cdc1f4..76bfa8b0e 100644 --- a/src/pages/paas-server/index.js +++ b/src/pages/paas-server/index.js @@ -27,7 +27,7 @@ app.use(user); // 注入全局变量 const GLOBAL_VAR = { SITE_URL: process.env.SITE_URL || '', - BK_STATIC_URL: process.env.BK_STATIC_URL || '', + STATIC_URL: process.env.STATIC_URL || '', // 当前应用的环境,预发布环境为 stag,正式环境为 prod BKPAAS_ENVIRONMENT: process.env.BKPAAS_ENVIRONMENT || '', // EngineApp名称,拼接规则:bkapp-{appcode}-{BKPAAS_ENVIRONMENT} @@ -69,11 +69,11 @@ app.get('/', (req, res) => { const scriptName = (req.headers['x-script-name'] || '').replace(/\//g, ''); // 使用子路径 if (scriptName) { - GLOBAL_VAR.BK_STATIC_URL = `/${scriptName}`; + GLOBAL_VAR.STATIC_URL = `/${scriptName}`; GLOBAL_VAR.SITE_URL = `/${scriptName}`; } else { // 使用系统分配域名 - GLOBAL_VAR.BK_STATIC_URL = ''; + GLOBAL_VAR.STATIC_URL = ''; GLOBAL_VAR.SITE_URL = ''; } // 注入全局变量 diff --git a/src/pages/src/http/fetch/index.ts b/src/pages/src/http/fetch/index.ts index fb3153bdf..e67506a74 100644 --- a/src/pages/src/http/fetch/index.ts +++ b/src/pages/src/http/fetch/index.ts @@ -48,7 +48,7 @@ const axiosInstance = axios.create({ xsrfCookieName: window.CSRF_COOKIE_NAME, xsrfHeaderName: 'X-CSRFToken', headers: { - 'X-CSRFToken': window.CSRF_COOKIE_NAME, + 'X-CSRFToken': Cookies.get(window.CSRF_COOKIE_NAME), 'x-requested-with': 'XMLHttpRequest', }, }); From df94a53636eca374aa95dd9d4980a1dad1ccf80b Mon Sep 17 00:00:00 2001 From: nannan00 <17491932+nannan00@users.noreply.github.com> Date: Wed, 16 Aug 2023 12:11:54 +0800 Subject: [PATCH 2/2] feat(deploy): dockerfile add front build (#1166) --- src/bk-user/Dockerfile | 19 +++++++++++++++---- src/bk-user/bkuser/common/views.py | 3 ++- src/bk-user/bkuser/settings.py | 2 +- src/pages/.bk.development.env | 2 +- src/pages/.bk.production.env | 2 +- src/pages/bk.config.js | 2 +- src/pages/index.html | 6 +++--- src/pages/paas-server/index.js | 8 +++----- src/pages/src/http/fetch/index.ts | 4 ++-- 9 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/bk-user/Dockerfile b/src/bk-user/Dockerfile index 8988403bd..28bb9c0bb 100644 --- a/src/bk-user/Dockerfile +++ b/src/bk-user/Dockerfile @@ -1,10 +1,18 @@ -FROM python:3.10.12-slim-buster +FROM node:18.17.1-bullseye-slim AS StaticBuilding +ENV NPM_VERSION 9.6.7 + +COPY src/pages / +WORKDIR / +RUN npm install +RUN npm run build + +FROM python:3.10.12-slim-bullseye USER root RUN rm /etc/apt/sources.list && \ - echo "deb https://mirrors.tencent.com/debian buster main" >> /etc/apt/sources.list && \ - echo "deb https://mirrors.tencent.com/debian-security buster/updates main" >> /etc/apt/sources.list && \ - echo "deb https://mirrors.tencent.com/debian buster-updates main" >> /etc/apt/sources.list + echo "deb https://mirrors.tencent.com/debian bullseye main" >> /etc/apt/sources.list && \ + echo "deb https://mirrors.tencent.com/debian-security bullseye-security main" >> /etc/apt/sources.list && \ + echo "deb https://mirrors.tencent.com/debian bullseye-updates main" >> /etc/apt/sources.list RUN mkdir ~/.pip && printf '[global]\nindex-url = https://mirrors.tencent.com/pypi/simple/' > ~/.pip/pip.conf @@ -25,4 +33,7 @@ COPY src/bk-user/bkuser /app/bkuser COPY src/bk-user/bin /app/bin COPY src/bk-user/manage.py /app +COPY --from=StaticBuilding /dist /app/staticfiles +COPY --from=StaticBuilding /dist/index.html /app/templates/index.html + CMD ["bash", "/app/bin/start.sh"] diff --git a/src/bk-user/bkuser/common/views.py b/src/bk-user/bkuser/common/views.py index 5395678a2..fb16b1ff2 100644 --- a/src/bk-user/bkuser/common/views.py +++ b/src/bk-user/bkuser/common/views.py @@ -157,10 +157,11 @@ def get(self, request, *args, **kwargs): context = { # BK_DOMAIN "BK_DOMAIN": settings.BK_DOMAIN, + "BK_LOGIN_URL": settings.BK_LOGIN_URL.rstrip("/"), "BK_USER_URL": settings.BK_USER_URL.rstrip("/"), "AJAX_BASE_URL": settings.AJAX_BASE_URL.rstrip("/"), # 去除末尾的 /, 前端约定 - "STATIC_URL": settings.STATIC_URL.rstrip("/"), + "BK_STATIC_URL": settings.STATIC_URL.rstrip("/"), # 去除开头的 . document.domain需要 "SESSION_COOKIE_DOMAIN": settings.SESSION_COOKIE_DOMAIN.lstrip("."), # csrftoken name diff --git a/src/bk-user/bkuser/settings.py b/src/bk-user/bkuser/settings.py index 8d8a45e26..6d0701335 100644 --- a/src/bk-user/bkuser/settings.py +++ b/src/bk-user/bkuser/settings.py @@ -156,7 +156,7 @@ BK_DOMAIN = env.str("BK_DOMAIN", default="") # BK USER URL BK_USER_URL = env.str("BK_USER_URL") -AJAX_BASE_URL = SITE_URL + "api/v1" +AJAX_BASE_URL = "" # csrf _BK_USER_URL_PARSE_URL = urlparse(BK_USER_URL) diff --git a/src/pages/.bk.development.env b/src/pages/.bk.development.env index beced60ca..80e7c420f 100644 --- a/src/pages/.bk.development.env +++ b/src/pages/.bk.development.env @@ -4,7 +4,7 @@ BK_SITE_URL = '' # 静态资源路径 -STATIC_URL = '' +BK_STATIC_URL = '' # 当前应用的环境,预发布环境为 stag,正式环境为 prod BK_PAAS_ENVIRONMENT = '' diff --git a/src/pages/.bk.production.env b/src/pages/.bk.production.env index ce2b1821a..79eee9a65 100644 --- a/src/pages/.bk.production.env +++ b/src/pages/.bk.production.env @@ -4,7 +4,7 @@ BK_SITE_URL = '{{ SITE_URL }}' # 静态资源路径 -STATIC_URL = '{{ STATIC_URL }}' +BK_STATIC_URL = '{{ BK_STATIC_URL }}' # 当前应用的环境,预发布环境为 stag,正式环境为 prod BK_PAAS_ENVIRONMENT = '{{ BKPAAS_ENVIRONMENT }}' diff --git a/src/pages/bk.config.js b/src/pages/bk.config.js index d268678f5..0847129cd 100644 --- a/src/pages/bk.config.js +++ b/src/pages/bk.config.js @@ -4,7 +4,7 @@ const mockServer = require('./mock-server'); module.exports = { host: process.env.BK_APP_HOST, port: process.env.BK_APP_PORT, - publicPath: process.env.STATIC_URL, + publicPath: process.env.BK_STATIC_URL, cache: true, open: true, replaceStatic: true, diff --git a/src/pages/index.html b/src/pages/index.html index fe0a0bab3..8679fc84a 100644 --- a/src/pages/index.html +++ b/src/pages/index.html @@ -1,8 +1,8 @@ - - + + index @@ -14,7 +14,7 @@ // 开发环境域名 window.AJAX_BASE_URL = '{{ AJAX_BASE_URL }}' // 静态资源路径 - window.STATIC_URL = '{{ STATIC_URL }}' + window.BK_STATIC_URL = '{{ BK_STATIC_URL }}' window.CSRF_COOKIE_NAME = '{{ CSRF_COOKIE_NAME }}' diff --git a/src/pages/paas-server/index.js b/src/pages/paas-server/index.js index 76bfa8b0e..8d4e72e7b 100644 --- a/src/pages/paas-server/index.js +++ b/src/pages/paas-server/index.js @@ -27,7 +27,7 @@ app.use(user); // 注入全局变量 const GLOBAL_VAR = { SITE_URL: process.env.SITE_URL || '', - STATIC_URL: process.env.STATIC_URL || '', + BK_STATIC_URL: process.env.BK_STATIC_URL || '', // 当前应用的环境,预发布环境为 stag,正式环境为 prod BKPAAS_ENVIRONMENT: process.env.BKPAAS_ENVIRONMENT || '', // EngineApp名称,拼接规则:bkapp-{appcode}-{BKPAAS_ENVIRONMENT} @@ -69,11 +69,11 @@ app.get('/', (req, res) => { const scriptName = (req.headers['x-script-name'] || '').replace(/\//g, ''); // 使用子路径 if (scriptName) { - GLOBAL_VAR.STATIC_URL = `/${scriptName}`; + GLOBAL_VAR.BK_STATIC_URL = `/${scriptName}`; GLOBAL_VAR.SITE_URL = `/${scriptName}`; } else { // 使用系统分配域名 - GLOBAL_VAR.STATIC_URL = ''; + GLOBAL_VAR.BK_STATIC_URL = ''; GLOBAL_VAR.SITE_URL = ''; } // 注入全局变量 @@ -94,5 +94,3 @@ app.set('view engine', 'html'); app.listen(PORT, () => { console.log(`App is running in port ${PORT}`); }); - - diff --git a/src/pages/src/http/fetch/index.ts b/src/pages/src/http/fetch/index.ts index e67506a74..abbfcba94 100644 --- a/src/pages/src/http/fetch/index.ts +++ b/src/pages/src/http/fetch/index.ts @@ -40,7 +40,7 @@ interface Http { const baseURL = /http(s)?:\/\//.test(window.AJAX_BASE_URL) ? window.AJAX_BASE_URL - : location.origin + window.STATIC_URL + window.AJAX_BASE_URL; + : location.origin + window.AJAX_BASE_URL; const axiosInstance = axios.create({ baseURL, @@ -120,5 +120,5 @@ methods.forEach((method) => { }, }); }); - + export default http as Http;