Skip to content

Commit 4981c1b

Browse files
committed
fix: actions s3cmd -> rclone
1 parent 320d29d commit 4981c1b

File tree

2 files changed

+66
-76
lines changed

2 files changed

+66
-76
lines changed

.github/workflows/deploy.yml

Lines changed: 54 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -38,88 +38,77 @@ jobs:
3838
git commit -m "Deploy $GITHUB_SHA to gh-pages"
3939
git push --force --set-upstream origin gh-pages
4040
41-
- name: Install and Configure s3cmd
41+
- name: Install and Configure rclone
4242
run: |
43-
# Install s3cmd
44-
pip install s3cmd
43+
# Install rclone
44+
curl https://rclone.org/install.sh | sudo bash
4545
46-
# Extract domain from endpoint URL (remove https:// if present)
47-
ENDPOINT=$(echo "${{ secrets.S3_ENDPOINT_URL }}" | sed 's/https:\/\///')
48-
49-
# Create s3cmd config file with MIME type settings
50-
cat > ~/.s3cfg << EOF
51-
[default]
52-
access_key = ${{ secrets.S3_ACCESS_KEY }}
53-
secret_key = ${{ secrets.S3_SECRET_KEY }}
54-
host_base = ${ENDPOINT}
55-
host_bucket = ${ENDPOINT}
56-
use_https = True
57-
check_ssl_certificate = False
58-
check_ssl_hostname = False
59-
# Enable verbose output and progress meter
60-
verbose = True
61-
progress_meter = True
62-
# Enable proper MIME types detection
63-
guess_mime_type = True
46+
# Configure rclone for S3 compatible storage
47+
mkdir -p ~/.config/rclone
48+
cat > ~/.config/rclone/rclone.conf << EOF
49+
[s3]
50+
type = s3
51+
provider = Other
52+
env_auth = false
53+
access_key_id = ${{ secrets.S3_ACCESS_KEY }}
54+
secret_access_key = ${{ secrets.S3_SECRET_KEY }}
55+
endpoint = ${{ secrets.S3_ENDPOINT_URL }}
56+
acl = public-read
57+
bucket_acl = public-read
58+
storage_class = STANDARD
59+
no_check_bucket = true
6460
EOF
6561
66-
- name: Deploy to S3-compatible storage with s3cmd
62+
- name: Deploy to S3-compatible storage with rclone
6763
run: |
6864
# Navigate to where the built files are
6965
cd gh-pages
7066
7167
# Print info about deployment
72-
echo "Using s3cmd to upload to S3-compatible storage"
68+
echo "Using rclone to upload to S3-compatible storage"
7369
echo "Bucket: ${S3_BUCKET_NAME_MASKED}"
7470
75-
# Test bucket access
76-
echo "Testing bucket access..."
77-
if s3cmd ls s3://${{ secrets.S3_BUCKET_NAME }}/; then
71+
# Create a mime types file to ensure proper content types
72+
cat > mime.types << EOF
73+
# MIME types for web content
74+
text/html html htm
75+
text/css css
76+
text/javascript js
77+
application/javascript js
78+
application/json json
79+
text/plain txt md
80+
image/jpeg jpg jpeg
81+
image/png png
82+
image/svg+xml svg
83+
image/webp webp
84+
image/gif gif
85+
application/pdf pdf
86+
application/font-woff woff
87+
application/font-woff2 woff2
88+
EOF
89+
90+
# Test connection
91+
echo "Testing connection to S3-compatible storage..."
92+
if rclone ls s3:${{ secrets.S3_BUCKET_NAME }} --quiet; then
7893
echo "✅ Bucket exists and is accessible"
7994
else
8095
echo "❌ Bucket does not exist or is not accessible"
8196
exit 1
8297
fi
8398
84-
# First, remove all existing files in the bucket
85-
echo "Clearing existing files from bucket..."
86-
s3cmd del --recursive --force s3://${{ secrets.S3_BUCKET_NAME }}/
87-
88-
# Upload all files first - this will use auto mime-type detection
89-
echo "Uploading files to S3-compatible storage..."
90-
s3cmd sync --verbose --progress \
91-
--exclude=".git/*" \
92-
--no-check-md5 \
93-
--delete-removed \
94-
--guess-mime-type \
95-
--add-header="Content-Disposition: inline" \
96-
./ s3://${{ secrets.S3_BUCKET_NAME }}/
97-
98-
# Set content types for specific file types to ensure proper handling
99-
echo "Setting content types for HTML files..."
100-
find . -name "*.html" | while read file; do
101-
rel_path=${file#./}
102-
echo "Setting content-type for: $rel_path"
103-
s3cmd modify --mime-type="text/html" \
104-
--add-header="Content-Disposition: inline" \
105-
s3://${{ secrets.S3_BUCKET_NAME }}/$rel_path
106-
done
107-
108-
echo "Setting content types for CSS files..."
109-
find . -name "*.css" | while read file; do
110-
rel_path=${file#./}
111-
s3cmd modify --mime-type="text/css" \
112-
--add-header="Content-Disposition: inline" \
113-
s3://${{ secrets.S3_BUCKET_NAME }}/$rel_path
114-
done
115-
116-
echo "Setting content types for JavaScript files..."
117-
find . -name "*.js" | while read file; do
118-
rel_path=${file#./}
119-
s3cmd modify --mime-type="application/javascript" \
120-
--add-header="Content-Disposition: inline" \
121-
s3://${{ secrets.S3_BUCKET_NAME }}/$rel_path
122-
done
99+
# Sync files to S3, using proper metadata and content types
100+
echo "Syncing files to S3-compatible storage..."
101+
rclone sync --verbose --progress \
102+
--checksum \
103+
--update \
104+
--delete-after \
105+
--exclude ".git/**" \
106+
--exclude "mime.types" \
107+
--metadata-set "Content-Disposition=inline" \
108+
--mime-file mime.types \
109+
--s3-no-head \
110+
--s3-no-check-bucket \
111+
./ s3:${{ secrets.S3_BUCKET_NAME }}
123112
124113
echo "Successfully deployed to S3-compatible storage"
125114
env:

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,32 @@
1111
1. 安装最新版本的 mdbook
1212
2. 构建静态网站
1313
3. 部署到 GitHub Pages
14-
4. 使用 s3cmd 将文件上传到兼容 S3 的对象存储服务
14+
4. 使用 rclone 将文件上传到兼容 S3 的对象存储服务
1515

1616
### 配置对象存储部署
1717

1818
要启用对象存储部署功能,需要在 GitHub 仓库中设置以下 Secrets:
1919

2020
1. `S3_ACCESS_KEY` - 对象存储服务的访问密钥(AK)
2121
2. `S3_SECRET_KEY` - 对象存储服务的秘密访问密钥(SK)
22-
3. `S3_ENDPOINT_URL` - 对象存储服务的端点(例如 `obs.cn-east-3.myhuaweicloud.com`
22+
3. `S3_ENDPOINT_URL` - 对象存储服务的完整端点URL(包含https://前缀
2323
4. `S3_BUCKET_NAME` - 存储桶名称(必须提前创建)
2424

2525
可以在 GitHub 仓库的 Settings → Secrets and variables → Actions 页面中添加这些 secrets。
2626

2727
**部署流程说明**
2828

29-
1. 部署流程使用轻量级的 s3cmd 工具与兼容 S3 的存储服务通信:
30-
- 配置简单,仅需几个基本参数
31-
- 提供内置的进度显示功能
32-
- 对第三方S3兼容服务有良好支持
29+
1. 部署流程使用 rclone 工具与兼容 S3 的存储服务通信:
30+
- rclone 是一个强大的文件同步工具,支持多种云存储服务
31+
- 正确处理文件元数据和内容类型(Content-Type)
32+
- 设置恰当的 Content-Disposition 头信息
3333

34-
2. 每次部署前会:
35-
- 清空目标存储桶中的所有现有文件
36-
- 排除不需要的文件(如 `.git` 目录)
37-
- 实时显示上传进度
34+
2. 每次部署过程会:
35+
- 自动测试存储桶连接
36+
- 使用 `--delete-after` 参数删除旧版本文件
37+
- 应用自定义 MIME 类型映射确保正确的内容类型
38+
- 设置 `Content-Disposition=inline` 确保文件在浏览器中正确显示
3839

3940
3. 如果对接华为云 OBS 或其他 S3 兼容服务,请确保:
4041
- 存储桶已创建并配置正确的访问权限
41-
- 端点 URL 格式正确(不要包含协议前缀 `https://`
42+
- 如需公开访问,确保存储桶已设置为公共读取权限

0 commit comments

Comments
 (0)