name: Build Docker Image
on:
push:
branches: [ main, master, develop ]
tags: [ 'v*' ]
pull_request:
branches: [ main, master ]
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Build TypeScript
run: npm run build
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Generate image metadata
id: meta
run: |
# 生成镜像标签
if [[ $GITHUB_REF == refs/tags/* ]]; then
# 如果是tag,使用tag名称
TAG=${GITHUB_REF#refs/tags/}
echo "tags=mongodb-mcp-server:$TAG,mongodb-mcp-server:latest" >> $GITHUB_OUTPUT
echo "version=$TAG" >> $GITHUB_OUTPUT
elif [[ $GITHUB_REF == refs/heads/main ]] || [[ $GITHUB_REF == refs/heads/master ]]; then
# 如果是main/master分支
echo "tags=mongodb-mcp-server:latest,mongodb-mcp-server:main" >> $GITHUB_OUTPUT
echo "version=main-$(echo $GITHUB_SHA | cut -c1-8)" >> $GITHUB_OUTPUT
else
# 其他分支或PR
BRANCH_NAME=${GITHUB_REF#refs/heads/}
BRANCH_NAME=${BRANCH_NAME//\//-} # 替换斜杠为横线
echo "tags=mongodb-mcp-server:$BRANCH_NAME" >> $GITHUB_OUTPUT
echo "version=$BRANCH_NAME-$(echo $GITHUB_SHA | cut -c1-8)" >> $GITHUB_OUTPUT
fi
- name: Build Docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
platforms: linux/amd64
push: false
tags: ${{ steps.meta.outputs.tags }}
outputs: type=docker,dest=/tmp/mongodb-mcp-server.tar
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Compress Docker image
run: |
# 压缩镜像文件以减小体积
gzip /tmp/mongodb-mcp-server.tar
- name: Get image info
id: image_info
run: |
# 获取压缩后的文件大小
SIZE=$(du -h /tmp/mongodb-mcp-server.tar.gz | cut -f1)
echo "size=$SIZE" >> $GITHUB_OUTPUT
# 生成文件名
FILENAME="mongodb-mcp-server-${{ steps.meta.outputs.version }}-amd64.tar.gz"
echo "filename=$FILENAME" >> $GITHUB_OUTPUT
# 重命名文件
mv /tmp/mongodb-mcp-server.tar.gz "/tmp/$FILENAME"
- name: Upload Docker image as artifact
uses: actions/upload-artifact@v4
with:
name: ${{ steps.image_info.outputs.filename }}
path: /tmp/${{ steps.image_info.outputs.filename }}
retention-days: 30
compression-level: 0 # 已经压缩过了,不需要再压缩
- name: Create download instructions
run: |
cat > download-instructions.md << 'EOF'
# MongoDB MCP Server Docker 镜像下载说明
## 镜像信息
- **版本**: ${{ steps.meta.outputs.version }}
- **架构**: linux/amd64
- **文件大小**: ${{ steps.image_info.outputs.size }}
- **文件名**: ${{ steps.image_info.outputs.filename }}
## 下载和使用方法
### 1. 下载镜像文件
请从GitHub Actions的Artifacts中下载镜像文件:${{ steps.image_info.outputs.filename }}
### 2. 加载Docker镜像
```bash
# 解压镜像文件
gunzip ${{ steps.image_info.outputs.filename }}
# 加载到Docker中
docker load -i mongodb-mcp-server-${{ steps.meta.outputs.version }}-amd64.tar
```
### 3. 运行容器
```bash
# 使用默认配置运行
docker run -d -p 8000:8000 \
-e MDB_MCP_CONNECTION_STRING="your_mongodb_connection_string" \
-e MDB_DB="your_database_name" \
mongodb-mcp-server:${{ steps.meta.outputs.version }}
# 或者使用latest标签
docker run -d -p 8000:8000 \
-e MDB_MCP_CONNECTION_STRING="your_mongodb_connection_string" \
-e MDB_DB="your_database_name" \
mongodb-mcp-server:latest
```
### 4. 环境变量说明
- `PORT`: 服务端口 (默认: 8000)
- `MDB_MCP_CONNECTION_STRING`: MongoDB连接字符串
- `MDB_DB`: 默认数据库名称 (默认: ChatBI)
### 5. 验证运行状态
```bash
# 检查容器状态
docker ps
# 查看容器日志
docker logs <container_id>
```
EOF
- name: Upload download instructions
uses: actions/upload-artifact@v4
with:
name: download-instructions-${{ steps.meta.outputs.version }}
path: download-instructions.md
retention-days: 30
- name: Comment download link on PR
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const { data: runId } = await github.rest.actions.getWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.runId
});
const artifactUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
const comment = `## 🐳 Docker 镜像构建完成
**镜像信息:**
- 版本: \`${{ steps.meta.outputs.version }}\`
- 架构: \`linux/amd64\`
- 文件大小: \`${{ steps.image_info.outputs.size }}\`
**下载链接:**
[📦 下载 Docker 镜像](${artifactUrl})
**使用方法:**
1. 从上面的链接下载 \`${{ steps.image_info.outputs.filename }}\`
2. 解压并加载镜像:
\`\`\`bash
gunzip ${{ steps.image_info.outputs.filename }}
docker load -i mongodb-mcp-server-${{ steps.meta.outputs.version }}-amd64.tar
\`\`\`
3. 运行容器:
\`\`\`bash
docker run -d -p 8000:8000 \\
-e MDB_MCP_CONNECTION_STRING="your_mongodb_connection_string" \\
mongodb-mcp-server:${{ steps.meta.outputs.version }}
\`\`\``;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});
- name: Create release notes
if: startsWith(github.ref, 'refs/tags/')
run: |
cat > release-notes.md << 'EOF'
# MongoDB MCP Server ${{ steps.meta.outputs.version }}
## Docker 镜像下载
**镜像信息:**
- 架构: linux/amd64
- 文件大小: ${{ steps.image_info.outputs.size }}
**下载文件:**
- [${{ steps.image_info.outputs.filename }}](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})
**快速开始:**
```bash
# 下载并解压镜像
gunzip ${{ steps.image_info.outputs.filename }}
# 加载到Docker
docker load -i mongodb-mcp-server-${{ steps.meta.outputs.version }}-amd64.tar
# 运行服务
docker run -d -p 8000:8000 \
-e MDB_MCP_CONNECTION_STRING="mongodb://user:pass@host:port/database" \
mongodb-mcp-server:${{ steps.meta.outputs.version }}
```
## 更新内容
请查看提交历史了解本版本的具体更新内容。
EOF
- name: Upload release notes
if: startsWith(github.ref, 'refs/tags/')
uses: actions/upload-artifact@v4
with:
name: release-notes-${{ steps.meta.outputs.version }}
path: release-notes.md
retention-days: 90