Skip to content

Commit 009d3fc

Browse files
authored
Update git config
1 parent 59b97e4 commit 009d3fc

1 file changed

Lines changed: 67 additions & 1 deletion

File tree

Dockerfile

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# - Optimization (China): Timezone set to Asia/Shanghai, PyPI mirror configured.
1515
# - Pre-installed Libraries: A minimal set (numpy, pandas, matplotlib).
1616
# - Extensions Persistence: Extensions installed to system-wide location.
17+
# - Auto Git Config: Automatically configures git user from environment variables.
1718
# ==============================================================================
1819

1920
# --- Build Stage ---
@@ -30,6 +31,9 @@ ENV PATH=${CONDA_DIR}/bin:${PATH}
3031
ENV TZ=Asia/Shanghai
3132
# 设置扩展安装到系统目录
3233
ENV VSCODE_EXTENSIONS_DIR=/opt/code-server/extensions
34+
# Git 配置默认值
35+
ENV GIT_USER_EMAIL="code-server@fjy8018.top"
36+
ENV GIT_USER_NAME="fjy8018"
3337

3438
# --- Installation Phase (as root) ---
3539
USER root
@@ -98,6 +102,36 @@ RUN \
98102
&& chown -R root:root ${VSCODE_EXTENSIONS_DIR} \
99103
&& chmod -R 755 ${VSCODE_EXTENSIONS_DIR}
100104

105+
# --- Git Configuration Script ---
106+
# 创建 Git 配置脚本
107+
RUN cat > /usr/local/bin/setup-git.sh << 'EOF'
108+
#!/bin/bash
109+
110+
# 获取环境变量,如果未设置则使用默认值
111+
GIT_EMAIL="${GIT_USER_EMAIL:-code-server@fjy8018.top}"
112+
GIT_NAME="${GIT_USER_NAME:-fjy8018}"
113+
114+
echo "Configuring Git with:"
115+
echo " Email: $GIT_EMAIL"
116+
echo " Name: $GIT_NAME"
117+
118+
# 配置全局 Git 设置
119+
git config --global user.email "$GIT_EMAIL"
120+
git config --global user.name "$GIT_NAME"
121+
122+
# 设置一些有用的 Git 默认配置
123+
git config --global init.defaultBranch main
124+
git config --global pull.rebase false
125+
git config --global core.autocrlf input
126+
git config --global core.editor "code-server --wait"
127+
128+
echo "Git configuration completed!"
129+
git config --global --list | grep -E "(user\.|init\.|pull\.|core\.)"
130+
EOF
131+
132+
# 使脚本可执行
133+
RUN chmod +x /usr/local/bin/setup-git.sh
134+
101135
# --- User Configuration Phase (as coder) ---
102136
USER coder
103137

@@ -112,7 +146,33 @@ RUN \
112146
\
113147
# 3. Configure the user's shell to auto-activate the system-wide environment.
114148
conda init bash && \
115-
echo "conda activate py${PYTHON_VERSION}" >> ~/.bashrc
149+
echo "conda activate py${PYTHON_VERSION}" >> ~/.bashrc && \
150+
\
151+
# 4. 在用户的 .bashrc 中添加 Git 配置初始化
152+
echo "" >> ~/.bashrc && \
153+
echo "# Auto-configure Git on first login" >> ~/.bashrc && \
154+
echo "if [ ! -f ~/.git-configured ]; then" >> ~/.bashrc && \
155+
echo " /usr/local/bin/setup-git.sh" >> ~/.bashrc && \
156+
echo " touch ~/.git-configured" >> ~/.bashrc && \
157+
echo "fi" >> ~/.bashrc
158+
159+
# --- Startup Script ---
160+
# 创建启动脚本来处理环境变量
161+
USER root
162+
RUN cat > /usr/local/bin/entrypoint.sh << 'EOF'
163+
#!/bin/bash
164+
165+
# 运行 Git 配置脚本
166+
/usr/local/bin/setup-git.sh
167+
168+
# 切换到 coder 用户并启动 code-server
169+
exec gosu coder "$@"
170+
EOF
171+
172+
RUN chmod +x /usr/local/bin/entrypoint.sh
173+
174+
# 安装 gosu(如果基础镜像没有的话)
175+
RUN apt-get update && apt-get install -y gosu && apt-get clean && rm -rf /var/lib/apt/lists/*
116176

117177
# --- Verification and Final Stage ---
118178
FROM builder
@@ -126,6 +186,8 @@ RUN echo "Verifying root environment..." && \
126186
node -v && \
127187
echo "Installed extensions:" && \
128188
ls -la ${VSCODE_EXTENSIONS_DIR} && \
189+
echo "Git setup script:" && \
190+
ls -la /usr/local/bin/setup-git.sh && \
129191
echo "Root environment check PASSED!"
130192

131193
# Final check as CODER.
@@ -140,4 +202,8 @@ RUN echo "Verifying coder environment..." && \
140202
cat ~/.config/code-server/config.yaml && \
141203
echo "Coder environment check PASSED!"
142204

205+
# 设置入口点
206+
USER root
207+
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
208+
143209
# The base image's CMD is inherited automatically.

0 commit comments

Comments
 (0)