Skip to content

Commit

Permalink
fix: 🐛 优化本地部署打镜像流程
Browse files Browse the repository at this point in the history
1. 修复python:alpine3.17,使用的是python3.12无法使用pip安装依赖,改为指定python3.12
  • Loading branch information
moyueheng committed Dec 20, 2023
1 parent cdeb8d7 commit 012a0a1
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 3 deletions.
Empty file added .dockerignore
Empty file.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
IMAGE_NAME=Qexo
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ tmp
venv
configs.py

*.db
*.db

.env
14 changes: 12 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
FROM python:alpine3.17
FROM python:3.11
WORKDIR /root
COPY . .
RUN pip install -r requirements_withoutmsyql.txt && \

ARG CN=false
RUN if [ "$CN" = "true" ]; then \
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/ && \
pip config set global.trusted-host pypi.tuna.tsinghua.edu.cn; \
fi

RUN python -m pip install --upgrade pip && \
pip install -r requirements_withoutmsyql.txt && \
python3 manage.py makemigrations && \
python3 manage.py migrate

EXPOSE 3000 8000

CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000", "--noreload" ]
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Target section and Global definitions
# -----------------------------------------------------------------------------
.PHONY: deploy

all: deploy


deploy: generate_dot_env
bash ./update_tag.sh
docker-compose build
docker-compose up -d

down:
docker-compose down

generate_dot_env:
@if [[ ! -e .env ]]; then \
cp .env.example .env; \
fi
13 changes: 13 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: "3"

services:
app:
build:
context: .
args:
CN: "true"
ports:
- "8000:8000"
env_file:
- .env
image: qexo:${IMAGE_TAG:-latest}
38 changes: 38 additions & 0 deletions update_tag.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

# 尝试获取当前的 Git 提交 ID 的简短哈希值
COMMIT_ID=$(git rev-parse --short HEAD 2>/dev/null)

# 获取当前的日期,格式为 YYYYMMDD
BUILD_DATE=$(date +%Y%m%d)

# 根据是否能够获取到 COMMIT_ID 来设置 IMAGE_TAG
if [ -n "$COMMIT_ID" ]; then
NEW_IMAGE_TAG="${BUILD_DATE}_${COMMIT_ID}"
else
NEW_IMAGE_TAG="${BUILD_DATE}"
fi

# 想要更新或添加的环境变量键名
ENV_VAR="IMAGE_TAG"

# 检查.env文件是否存在
if [ -f ".env" ]; then
# 检查IMAGE_TAG变量是否存在
if grep -q "${ENV_VAR}=" .env; then
# 使用Sed来安全地替换.env文件中的IMAGE_TAG值
sed -i.bak -e "s/^${ENV_VAR}=.*/${ENV_VAR}=${NEW_IMAGE_TAG}/" .env
echo "Updated ${ENV_VAR} in .env file."
else
# 如果IMAGE_TAG不存在,那么添加它
# 首先确保.env文件以换行符结尾
tail -c1 .env | read -r _ || echo "" >> .env
# 然后添加新的IMAGE_TAG
echo "${ENV_VAR}=${NEW_IMAGE_TAG}" >> .env
echo "Added ${ENV_VAR} to .env file."
fi
else
# 如果.env文件不存在,那么创建一个并添加IMAGE_TAG
echo "${ENV_VAR}=${NEW_IMAGE_TAG}" > .env
echo "Created .env file and added ${ENV_VAR}."
fi

0 comments on commit 012a0a1

Please sign in to comment.