diff --git a/.github/workflows/build_image.yml b/.github/workflows/build_image.yml index 2d03943b..d8df8e8c 100644 --- a/.github/workflows/build_image.yml +++ b/.github/workflows/build_image.yml @@ -115,7 +115,7 @@ jobs: type=ref,event=tag type=sha,format=short,prefix= - # build and push frontend docker image + # build and push frontend docker image with version argument - name: Build Frontend Docker image uses: docker/build-push-action@v5 with: @@ -124,6 +124,8 @@ jobs: tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} platforms: linux/amd64 + build-args: | + VERSION=${{ github.ref_type == 'tag' && github.ref_name || '' }} update_helm_values: runs-on: ubuntu-24.04 @@ -150,12 +152,25 @@ jobs: sudo wget -qO /usr/local/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 sudo chmod +x /usr/local/bin/yq - # update image tags in values/credit/credit.yaml - - name: Update image tags in values/credit/credit.yaml + # determine helm values file based on tag type (production or test) + - name: Determine Helm values file + id: helm_config + run: | + TAG="${{ github.ref_name }}" + if [[ $TAG =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + echo "values_file=values/credit/credit.yaml" >> "$GITHUB_OUTPUT" + echo "branch_prefix=update/credit" >> "$GITHUB_OUTPUT" + else + echo "values_file=values/credit_test/credit.yaml" >> "$GITHUB_OUTPUT" + echo "branch_prefix=update/credit_test" >> "$GITHUB_OUTPUT" + fi + + # update image tags in helm values file + - name: Update image tags in helm values file run: | cd helm-charts - yq -i '.image.tag = "${{ github.ref_name }}"' values/credit/credit.yaml - yq -i '.image.webTag = "${{ github.ref_name }}"' values/credit/credit.yaml + yq -i '.image.tag = "${{ github.ref_name }}"' ${{ steps.helm_config.outputs.values_file }} + yq -i '.image.webTag = "${{ github.ref_name }}"' ${{ steps.helm_config.outputs.values_file }} # commit and push to a new branch, then open a PR for review - name: Commit, push and create PR @@ -165,14 +180,15 @@ jobs: cd helm-charts git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" - git checkout -b update/credit-${{ github.ref_name }} - git add values/credit/credit.yaml + BRANCH_NAME="${{ steps.helm_config.outputs.branch_prefix }}-${{ github.ref_name }}" + git checkout -b "$BRANCH_NAME" + git add ${{ steps.helm_config.outputs.values_file }} git commit -m "chore: update credit image tag to ${{ github.ref_name }}" - git push origin update/credit-${{ github.ref_name }} + git push origin "$BRANCH_NAME" HELM_REPO=$(echo "${{ vars.HELM_CHARTS_REPO }}" | sed 's/git@github.com://;s/\.git$//') gh pr create \ --repo "${HELM_REPO}" \ --title "chore: update credit image tag to ${{ github.ref_name }}" \ - --body "Automated PR: update \`values/credit/credit.yaml\` image tags to \`${{ github.ref_name }}\` after Docker image build." \ + --body "Automated PR: update \`${{ steps.helm_config.outputs.values_file }}\` image tags to \`${{ github.ref_name }}\` after Docker image build." \ --base main \ - --head update/credit-${{ github.ref_name }} + --head "$BRANCH_NAME" diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 6fca43ff..5975dab8 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -21,9 +21,17 @@ WORKDIR /app ENV NODE_ENV=production +# accepts version argument to override package.json version +ARG VERSION="" + COPY --from=deps /app/node_modules ./node_modules COPY . . +# update package.json version if VERSION arg is provided +RUN if [ -n "$VERSION" ]; then \ + node -e "const pkg = require('./package.json'); pkg.version = '$VERSION'; require('fs').writeFileSync('./package.json', JSON.stringify(pkg, null, 2))"; \ +fi + EXPOSE 3000 CMD ["sh", "-c", "pnpm build && pnpm start"] diff --git a/internal/router/middlewares.go b/internal/router/middlewares.go index d8fb8a3c..92c46eb8 100644 --- a/internal/router/middlewares.go +++ b/internal/router/middlewares.go @@ -21,6 +21,7 @@ import ( "time" "github.com/gin-gonic/gin" + "github.com/linux-do/credit/internal/config" "github.com/linux-do/credit/internal/logger" "github.com/linux-do/credit/internal/otel_trace" "go.opentelemetry.io/otel/codes" @@ -51,18 +52,22 @@ func loggerMiddleware() gin.HandlerFunc { latency := end.Sub(start) // 打印日志 - logger.InfoF( - ctx, - "[LoggerMiddleware] %s %s\nStartTime: %s\nEndTime: %s\nLatency: %d\nClientIP: %s\nResponse: %d %d", - c.Request.Method, - path, - start.Format(time.RFC3339), - end.Format(time.RFC3339), - latency.Milliseconds(), - c.ClientIP(), - c.Writer.Status(), - c.Writer.Size(), - ) + // 排除健康检查接口 + healthPath := config.Config.App.APIPrefix + "/v1/health" + if c.Request.URL.Path != healthPath { + logger.InfoF( + ctx, + "[LoggerMiddleware] %s %s\nStartTime: %s\nEndTime: %s\nLatency: %d\nClientIP: %s\nResponse: %d %d", + c.Request.Method, + path, + start.Format(time.RFC3339), + end.Format(time.RFC3339), + latency.Milliseconds(), + c.ClientIP(), + c.Writer.Status(), + c.Writer.Size(), + ) + } // 设置 Span 状态 if c.Writer.Status() >= 400 {