Skip to content

Let's use AI to Earn!

feat: 更新 dockerfile

柯南f9f7fc57af2a
diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml
new file mode 100644
index 00000000..9dfee8fb
--- /dev/null
+++ b/.github/workflows/deploy.yml
@@ -0,0 +1,180 @@
+name: Deploy Service
+
+on:
+ workflow_dispatch:
+ inputs:
+ environment:
+ description: 选择目标环境
+ required: true
+ type: choice
+ options:
+ - dev
+ - prod
+ version:
+ description: 镜像版本 (留空使用默认生成规则)
+ required: false
+ type: string
+ architectures:
+ description: 目标平台 (逗号分隔,如 linux/amd64,linux/arm64)
+ required: false
+ type: string
+ default: linux/amd64
+
+permissions:
+ id-token: write
+ contents: read
+
+env:
+ AWS_REGION: ap-southeast-1
+ ECR_REGISTRY: 339388639667.dkr.ecr.ap-southeast-1.amazonaws.com
+ ROLE_TO_ASSUME: arn:aws:iam::339388639667:role/GithubActions
+ IMAGE_REPO: aitoearn
+ APP_NAME: aitoearn-web
+ HELM_REPO: yikart/k8s-apps
+ HELM_REPO_URL: https://github.com/yikart/k8s-apps.git
+
+jobs:
+ build-and-deploy:
+ runs-on: ubuntu-latest
+ environment: ${{ github.event.inputs.environment }}
+
+ steps:
+ - name: Checkout source code
+ uses: actions/checkout@v4
+
+ - name: Set up Docker Buildx
+ uses: docker/setup-buildx-action@v3
+
+ - name: Configure AWS credentials
+ uses: aws-actions/configure-aws-credentials@v4
+ with:
+ role-to-assume: ${{ env.ROLE_TO_ASSUME }}
+ aws-region: ${{ env.AWS_REGION }}
+
+ - name: Login to Amazon ECR
+ id: login-ecr
+ uses: aws-actions/amazon-ecr-login@v2
+
+ - name: Generate version
+ id: version
+ run: |
+ if [ -n "${{ github.event.inputs.version }}" ]; then
+ VERSION="${{ github.event.inputs.version }}"
+ else
+ VERSION=$(date +%Y%m%d)-$(git rev-parse --short HEAD)
+ fi
+ echo "version=$VERSION" >> $GITHUB_OUTPUT
+
+ - name: Build and push application
+ uses: docker/build-push-action@v5
+ with:
+ context: .
+ file: ./Dockerfile
+ platforms: ${{ github.event.inputs.architectures }}
+ push: true
+ tags: |
+ ${{ env.ECR_REGISTRY }}/${{ env.IMAGE_REPO }}/${{ env.APP_NAME }}:${{ steps.version.outputs.version }}
+ cache-from: type=gha
+ cache-to: type=gha,mode=max
+
+ - uses: actions/create-github-app-token@v2
+ id: generate-token
+ with:
+ app-id: ${{ secrets.APP_ID }}
+ private-key: ${{ secrets.APP_PRIVATE_KEY }}
+ owner: yikart
+ repositories: k8s-apps
+
+ - name: Clone Helm repository
+ uses: actions/checkout@v4
+ with:
+ repository: ${{ env.HELM_REPO }}
+ token: ${{ steps.generate-token.outputs.token }}
+ path: k8s-apps
+
+ - name: Update Helm values
+ run: |
+ cd k8s-apps
+ ENV="${{ github.event.inputs.environment }}"
+ VERSION="${{ steps.version.outputs.version }}"
+ APP="${{ env.APP_NAME }}"
+ VALUES_FILE="${ENV}/values-${APP}.yaml"
+
+ if [ -f "$VALUES_FILE" ]; then
+ sed -i "s|tag: .*|tag: ${VERSION}|g" "$VALUES_FILE"
+ echo "Updated ${APP} tag to ${VERSION}"
+ else
+ echo "Warning: Values file not found: $VALUES_FILE"
+ fi
+
+ - name: Get source repository changes
+ id: source-changes
+ run: |
+ COMMIT_HISTORY=$(git log --oneline -10 --pretty=format:"- [%h](${{ github.server_url }}/${{ github.repository }}/commit/%H) %s")
+ echo 'commit_history<<EOF' >> $GITHUB_OUTPUT
+ echo "$COMMIT_HISTORY" >> $GITHUB_OUTPUT
+ echo 'EOF' >> $GITHUB_OUTPUT
+
+ - name: Create Pull Request in Helm Repo
+ id: create-pr
+ uses: peter-evans/create-pull-request@v7
+ with:
+ token: ${{ steps.generate-token.outputs.token }}
+ path: ./k8s-apps
+ branch: deploy/${{ env.APP_NAME }}-${{ github.event.inputs.environment }}-${{ steps.version.outputs.version }}
+ title: deploy(${{ github.event.inputs.environment }}) update ${{ env.APP_NAME }} to ${{ steps.version.outputs.version }}
+ body: |
+ ## 🚀 部署请求
+
+ **应用**: ${{ env.APP_NAME }}
+ **环境**: ${{ github.event.inputs.environment }}
+ **版本**: ${{ steps.version.outputs.version }}
+
+ ### 📋 变更详情
+
+ - 更新镜像标签: `${{ env.APP_NAME }}:${{ steps.version.outputs.version }}`
+ - 目标环境: `${{ github.event.inputs.environment }}`
+
+ ### 📝 源代码变更历史
+
+ 最近的提交记录:
+ ${{ steps.source-changes.outputs.commit_history }}
+
+ ### 🔗 相关信息
+
+ - **源仓库**: ${{ github.repository }}
+ - **源提交**: [`${{ github.sha }}`](${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }})
+ - **镜像地址**: `${{ env.ECR_REGISTRY }}/${{ env.IMAGE_REPO }}:${{ steps.version.outputs.version }}`
+ - **构建日志**: [GitHub Actions Run ${{ github.run_id }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
+ # 提交信息
+ commit-message: |
+ deploy(${{ github.event.inputs.environment }}): update ${{ env.APP_NAME }} to ${{ steps.version.outputs.version }}
+
+ Update image tag for ${{ env.APP_NAME }} in ${{ github.event.inputs.environment }} environment
+ Source-Commit: ${{ github.sha }}
+ Triggered-By: GitHub Actions
+ # PR 的目标分支
+ base: main
+
+ - name: Summary
+ if: always()
+ run: |
+ echo "## 📦部署摘要" >> $GITHUB_STEP_SUMMARY
+ echo "- **应用**: ${{ env.APP_NAME }}" >> $GITHUB_STEP_SUMMARY
+ echo "- **环境**: ${{ github.event.inputs.environment }}" >> $GITHUB_STEP_SUMMARY
+ echo "- **版本**: ${{ steps.version.outputs.version }}" >> $GITHUB_STEP_SUMMARY
+ echo "- **镜像**: \`${{ env.ECR_REGISTRY }}/${{ env.IMAGE_REPO }}:${{ steps.version.outputs.version }}\`" >> $GITHUB_STEP_SUMMARY
+
+ # 检查 create-pr 步骤的 'created' 输出
+ if [ "${{ steps.create-pr.outputs.pull-request-number }}" != "" ]; then
+ echo "- **状态**: ✅ Pull Request 已创建到 Helm 仓库" >> $GITHUB_STEP_SUMMARY
+ # 使用新的输出 'pull-request-url'
+ echo "- **PR链接**: ${{ steps.create-pr.outputs.pull-request-url }}" >> $GITHUB_STEP_SUMMARY
+ else
+ echo "- **状态**: ⚠️ 无变更,未创建 Pull Request" >> $GITHUB_STEP_SUMMARY
+ fi
+
+ echo "" >> $GITHUB_STEP_SUMMARY
+ echo "### 🔗 快速链接" >> $GITHUB_STEP_SUMMARY
+ echo "- [Helm 仓库](${{ env.HELM_REPO_URL }})" >> $GITHUB_STEP_SUMMARY
+ echo "- [源代码提交](${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }})" >> $GITHUB_STEP_SUMMARY
diff --git a/Dockerfile b/Dockerfile
index c8faa822..e5517b08 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,7 +1,47 @@
-FROM node:18-alpine AS base
+FROM node:lts-alpine AS base
+ENV NEXT_TELEMETRY_DISABLED=1
+ENV NODE_ENV=production
+
+RUN apk add --no-cache libc6-compat
+
+RUN corepack enable pnpm && corepack prepare pnpm@latest --activate
+
+WORKDIR /app
FROM base AS deps
-RUN apk add --no-cache libc6-compat
+COPY package.json pnpm-lock.yaml ./
+
+RUN pnpm install --frozen-lockfile --prod=false
+FROM base AS builder
WORKDIR /app
+
+COPY --from=deps /app/node_modules ./node_modules
+COPY . .
+
+RUN pnpm build
+
+FROM base AS production
+WORKDIR /app
+
+ENV NODE_ENV=production
+ENV NEXT_TELEMETRY_DISABLED=1
+
+RUN addgroup --system --gid 1001 nodejs
+RUN adduser --system --uid 1001 nextjs
+
+COPY --from=builder /app/public ./public
+
+RUN mkdir .next
+RUN chown nextjs:nodejs .next
+
+COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
+COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
+
+USER nextjs
+
+ENV PORT=3000
+ENV HOSTNAME="0.0.0.0"
+
+CMD ["node", "server.js"]
\ No newline at end of file