Skip to content

Commit

Permalink
feat(deploy): dockerfile add front build (#1166)
Browse files Browse the repository at this point in the history
  • Loading branch information
nannan00 authored Aug 16, 2023
1 parent d37eec0 commit df94a53
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 19 deletions.
19 changes: 15 additions & 4 deletions src/bk-user/Dockerfile
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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"]
3 changes: 2 additions & 1 deletion src/bk-user/bkuser/common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/bk-user/bkuser/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/pages/.bk.development.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
BK_SITE_URL = ''

# 静态资源路径
STATIC_URL = ''
BK_STATIC_URL = ''

# 当前应用的环境,预发布环境为 stag,正式环境为 prod
BK_PAAS_ENVIRONMENT = ''
Expand Down
2 changes: 1 addition & 1 deletion src/pages/.bk.production.env
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}'
Expand Down
2 changes: 1 addition & 1 deletion src/pages/bk.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/pages/index.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<link rel="icon" href="{{ STATIC_URL }}/images/favicon.png" type="image/x-icon" />
<link rel="shortcut icon" href="{{ STATIC_URL }}/images/favicon.png" type="image/x-icon" />
<link rel="icon" href="{{ BK_STATIC_URL }}/images/favicon.png" type="image/x-icon" />
<link rel="shortcut icon" href="{{ BK_STATIC_URL }}/images/favicon.png" type="image/x-icon" />
<meta charset="utf-8">
<title> index </title>
</head>
Expand All @@ -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 }}'
</script>
</body>
Expand Down
8 changes: 3 additions & 5 deletions src/pages/paas-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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 = '';
}
// 注入全局变量
Expand All @@ -94,5 +94,3 @@ app.set('view engine', 'html');
app.listen(PORT, () => {
console.log(`App is running in port ${PORT}`);
});


4 changes: 2 additions & 2 deletions src/pages/src/http/fetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -120,5 +120,5 @@ methods.forEach((method) => {
},
});
});

export default http as Http;

0 comments on commit df94a53

Please sign in to comment.