Skip to content

test(util): 添加密码、JSON和货币工具类的单元测试 #19

test(util): 添加密码、JSON和货币工具类的单元测试

test(util): 添加密码、JSON和货币工具类的单元测试 #19

Workflow file for this run

name: CI
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main, develop ]
jobs:
test:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.0
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: gotribe
MYSQL_USER: gotribe
MYSQL_PASSWORD: gotribe123
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
redis:
image: redis:7-alpine
ports:
- 6379:6379
options: --health-cmd="redis-cli ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.23'
- name: Cache Go modules
uses: actions/cache@v4
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Install dependencies
run: go mod download
- name: Verify dependencies
run: go mod verify
- name: Format check
run: |
if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then
echo "The following files are not formatted:"
gofmt -s -l .
exit 1
fi
- name: Vet
run: go vet ./...
# 暂时跳过 Lint 验证以避免缓存问题
# - name: Lint
# uses: golangci/golangci-lint-action@v3
# with:
# version: v1.61.0
# args: --timeout=5m
# skip-cache: true
# skip-pkg-cache: true
# skip-build-cache: true
- name: Test
run: go test -v -race -coverprofile=coverage.out ./...
env:
DB_HOST: localhost
DB_PORT: 3306
DB_USERNAME: gotribe
DB_PASSWORD: gotribe123
DB_DATABASE: gotribe
DB_TYPE: mysql
REDIS_HOST: localhost
REDIS_PORT: 6379
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
file: ./coverage.out
flags: unittests
name: codecov-umbrella
build:
runs-on: ubuntu-latest
needs: test
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.23'
- name: Build
run: |
go build -v -ldflags "-X gotribe-admin/internal/pkg/common.GitVersion=$(git describe --tags --always --dirty) -X gotribe-admin/internal/pkg/common.GitCommit=$(git rev-parse HEAD) -X gotribe-admin/internal/pkg/common.GitTreeState=$(if [ -z "$(git status --porcelain)" ]; then echo "clean"; else echo "dirty"; fi) -X gotribe-admin/internal/pkg/common.BuildDate=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" -o gotribe-admin .
- name: Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: gotribe-admin-linux-amd64
path: gotribe-admin
docker:
runs-on: ubuntu-latest
needs: test
steps:
- uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# 尝试登录 Docker Hub(如果没有配置 secrets 会失败但不影响构建)
- name: Login to Docker Hub
continue-on-error: true
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: gotribe-admin
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
continue-on-error: true
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
# 只在 main 分支推送,如果没有登录会失败但不影响构建
push: ${{ github.ref == 'refs/heads/main' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
security:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
scan-type: 'fs'
scan-ref: '.'
format: 'sarif'
output: 'trivy-results.sarif'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v3
if: always()
with:
sarif_file: 'trivy-results.sarif'
release:
runs-on: ubuntu-latest
needs: [test, build]
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.23'
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v4
with:
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}