Commit bc710a02 by Ivan

feat: update deploy

parent 3555e39e
# 移除 syntax 指令以避免网络问题 # syntax=docker/dockerfile:1
# 原指令: # syntax=docker/dockerfile:1
# check=error=true # check=error=true
# This Dockerfile is designed for Git-based deployment with Kamal # This Dockerfile is designed for Git-based deployment with Kamal
# It pulls code from Git during deployment and uses external volumes for code, build artifacts, and data # It pulls code from Git during deployment and uses external volumes for code, build artifacts, and data
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version # Make sure RUBY_VERSION matches the Ruby version in .ruby-version
ARG RUBY_VERSION=3.2.2 FROM ruby:3.2.2
FROM docker.io/library/ruby:$RUBY_VERSION-slim
# Rails app lives here # Rails app lives here
WORKDIR /rails WORKDIR /rails
...@@ -48,11 +46,11 @@ RUN mkdir -p \ ...@@ -48,11 +46,11 @@ RUN mkdir -p \
/rails/tmp/sockets /rails/tmp/sockets
# Declare volumes for persistent storage # Declare volumes for persistent storage
VOLUME ["/rails/code", "/rails/storage", "/rails/public/uploads", "/rails/public/vite", "/rails/node_modules", "/rails/log", "/rails/tmp"] VOLUME ["/root/.ssh", "/rails/code", "/rails/storage", "/rails/public/uploads", "/rails/public/vite", "/rails/node_modules", "/rails/log", "/rails/tmp"]
# Copy entrypoint script # Copy entrypoint script
COPY bin/docker-entrypoint /rails/bin/ COPY bin/docker-entrypoint /rails/bin/
# RUN chmod +x /rails/bin/docker-entrypoint RUN chmod +x /rails/bin/docker-entrypoint
# Run and own only the runtime files as a non-root user for security # Run and own only the runtime files as a non-root user for security
RUN groupadd --system --gid 1000 rails && \ RUN groupadd --system --gid 1000 rails && \
......
...@@ -9,7 +9,9 @@ fi ...@@ -9,7 +9,9 @@ fi
# Ensure all required directories exist with proper permissions # Ensure all required directories exist with proper permissions
echo "Setting up directories..." echo "Setting up directories..."
echo "Setting up directories......"
mkdir -p /rails/code /rails/storage /rails/public/uploads /rails/public/assets /rails/public/vite /rails/node_modules /rails/log /rails/tmp/pids /rails/tmp/cache /rails/tmp/sockets mkdir -p /rails/code /rails/storage /rails/public/uploads /rails/public/assets /rails/public/vite /rails/node_modules /rails/log /rails/tmp/pids /rails/tmp/cache /rails/tmp/sockets
echo "after Setting up directories..."
# Set very permissive permissions for all directories # Set very permissive permissions for all directories
# chmod -R 777 /rails/storage /rails/public/uploads /rails/public/assets /rails/public/vite /rails/node_modules /rails/log /rails/tmp # chmod -R 777 /rails/storage /rails/public/uploads /rails/public/assets /rails/public/vite /rails/node_modules /rails/log /rails/tmp
...@@ -36,34 +38,55 @@ sleep 5 ...@@ -36,34 +38,55 @@ sleep 5
if [ -z "${GIT_REPOSITORY}" ]; then if [ -z "${GIT_REPOSITORY}" ]; then
echo "⚠️ GIT_REPOSITORY environment variable not set. Using mounted code directory." echo "⚠️ GIT_REPOSITORY environment variable not set. Using mounted code directory."
else else
echo "🔄 Setting up code from Git repository: ${GIT_REPOSITORY}" # 检查代码目录是否已经存在文件
if [ "$(ls -A /rails/code 2>/dev/null)" ]; then
echo "✅ Code directory is not empty. Using mounted code directory."
echo "Skipping Git operations to avoid permission issues."
else
echo "🔄 Setting up code from Git repository: ${GIT_REPOSITORY}"
# 添加 Git 安全目录配置,解决仓库所有权问题
echo "Configuring Git safe directory..."
git config --global --add safe.directory /rails/code
# 检查是否安装了 git 命令
if ! command -v git &> /dev/null; then
echo "⚠️ Git command not found. Skipping Git operations."
else
# 检查是否能访问 Git 仓库
if git ls-remote --quiet ${GIT_REPOSITORY} &> /dev/null; then
# 尝试克隆仓库,但可能会失败
echo "Attempting to clone repository (may fail if directory is mounted)..."
if [ -n "${GIT_BRANCH}" ]; then
git clone --branch ${GIT_BRANCH} ${GIT_REPOSITORY} /rails/code || echo "Git clone failed, but continuing anyway"
else
git clone ${GIT_REPOSITORY} /rails/code || echo "Git clone failed, but continuing anyway"
fi
else
echo "⚠️ Cannot access Git repository. Skipping Git operations."
fi
fi
fi
# Check if we already have the repository # 如果存在 Git 仓库,尝试更新,但不强制
if [ -d "/rails/code/.git" ]; then if [ -d "/rails/code/.git" ]; then
echo "Git repository exists, pulling latest changes..." echo "Git repository exists, attempting to pull latest changes..."
cd /rails/code cd /rails/code
git fetch git config --global --add safe.directory /rails/code
git fetch || echo "Git fetch failed, but continuing anyway"
# Check if we need to checkout a specific branch or tag # Check if we need to checkout a specific branch or tag
if [ -n "${GIT_BRANCH}" ]; then if [ -n "${GIT_BRANCH}" ]; then
echo "Checking out branch/tag: ${GIT_BRANCH}" echo "Checking out branch/tag: ${GIT_BRANCH}"
git checkout ${GIT_BRANCH} git checkout ${GIT_BRANCH} || echo "Git checkout failed, but continuing anyway"
git pull origin ${GIT_BRANCH} git pull origin ${GIT_BRANCH} || echo "Git pull failed, but continuing anyway"
else
echo "No branch specified, pulling latest from current branch"
git pull
fi
else
echo "Cloning repository..."
# Clone the repository
if [ -n "${GIT_BRANCH}" ]; then
git clone --branch ${GIT_BRANCH} ${GIT_REPOSITORY} /rails/code
else else
git clone ${GIT_REPOSITORY} /rails/code echo "No branch specified, attempting to pull latest from current branch"
git pull || echo "Git pull failed, but continuing anyway"
fi fi
fi fi
echo "✅ Code updated from Git repository" echo "✅ Using code from mounted directory"
fi fi
# Create symbolic links from code directory to Rails app directory # Create symbolic links from code directory to Rails app directory
......
...@@ -8,9 +8,9 @@ image: mumumumushu/img_manager ...@@ -8,9 +8,9 @@ image: mumumumushu/img_manager
servers: servers:
web: web:
hosts: hosts:
- 45.78.59.154 - 45.78.59.154
labels: labels:
traefik.http.routers.app.rule: Host(`img.mumumumushu.com`) traefik.http.routers.app.rule: Host(`img-manager.mumumumushu.com`)
# job: # job:
# hosts: # hosts:
# - 192.168.0.1 # - 192.168.0.1
...@@ -44,7 +44,7 @@ env: ...@@ -44,7 +44,7 @@ env:
# - GIT_CREDENTIALS # - GIT_CREDENTIALS
clear: clear:
# Git repository configuration # Git repository configuration
GIT_REPOSITORY: http://git.tallty.com/mumumumushu/img-manager.git GIT_REPOSITORY: git@git.tallty.com:mumumumushu/img-manager.git
GIT_BRANCH: main GIT_BRANCH: main
# Force rebuild flags (set to true when you want to force rebuild) # Force rebuild flags (set to true when you want to force rebuild)
...@@ -101,6 +101,7 @@ volumes: ...@@ -101,6 +101,7 @@ volumes:
- "/root/img_manager/logs:/rails/log" - "/root/img_manager/logs:/rails/log"
# Tmp directory for pids and other temporary files # Tmp directory for pids and other temporary files
- "/root/img_manager/tmp:/rails/tmp" - "/root/img_manager/tmp:/rails/tmp"
- "/root/.ssh:/root/.ssh"
# Bridge fingerprinted assets, like JS and CSS, between versions to avoid # Bridge fingerprinted assets, like JS and CSS, between versions to avoid
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment