完善GitHub设置:添加CI/CD、Docker支持、组织README #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Java CI/CD Pipeline | |
| on: | |
| push: | |
| branches: [ main, develop ] | |
| pull_request: | |
| branches: [ main ] | |
| schedule: | |
| - cron: '0 2 * * *' # 每天凌晨2点运行 | |
| jobs: | |
| build-and-test: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: Set up JDK 11 | |
| uses: actions/setup-java@v3 | |
| with: | |
| java-version: '11' | |
| distribution: 'temurin' | |
| cache: maven | |
| - name: Cache Maven dependencies | |
| uses: actions/cache@v3 | |
| with: | |
| path: ~/.m2 | |
| key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} | |
| restore-keys: | | |
| ${{ runner.os }}-maven- | |
| - name: Build with Maven | |
| run: ./mvnw clean compile -DskipTests | |
| - name: Run tests | |
| run: ./mvnw test | |
| env: | |
| OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} | |
| - name: Run integration tests | |
| run: ./mvnw verify -DskipITs=false | |
| if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
| - name: Code coverage | |
| uses: codecov/codecov-action@v3 | |
| with: | |
| file: ./target/site/jacoco/jacoco.xml | |
| fail_ci_if_error: false | |
| - name: Build Docker image | |
| run: | | |
| docker build -t intellidev/java-ai-starter:latest . | |
| if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
| - name: Security scan | |
| uses: snyk/actions/maven@master | |
| env: | |
| SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }} | |
| continue-on-error: true | |
| - name: Upload build artifacts | |
| uses: actions/upload-artifact@v3 | |
| with: | |
| name: java-artifacts | |
| path: target/*.jar | |
| if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
| quality-check: | |
| runs-on: ubuntu-latest | |
| needs: build-and-test | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: SonarCloud Scan | |
| uses: SonarSource/sonarcloud-github-action@master | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | |
| - name: Checkstyle | |
| run: ./mvnw checkstyle:check | |
| - name: PMD analysis | |
| run: ./mvnw pmd:check | |
| - name: SpotBugs analysis | |
| run: ./mvnw spotbugs:check | |
| deploy: | |
| runs-on: ubuntu-latest | |
| needs: [build-and-test, quality-check] | |
| if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: Set up JDK 11 | |
| uses: actions/setup-java@v3 | |
| with: | |
| java-version: '11' | |
| distribution: 'temurin' | |
| - name: Build package | |
| run: ./mvnw clean package -DskipTests | |
| - name: Login to DockerHub | |
| uses: docker/login-action@v2 | |
| with: | |
| username: ${{ secrets.DOCKER_USERNAME }} | |
| password: ${{ secrets.DOCKER_PASSWORD }} | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v4 | |
| with: | |
| context: . | |
| push: true | |
| tags: | | |
| intellidev/java-ai-starter:latest | |
| intellidev/java-ai-starter:${{ github.sha }} | |
| - name: Deploy to staging | |
| run: | | |
| echo "Deploying to staging environment..." | |
| # 这里添加你的部署脚本 | |
| # 例如:kubectl apply -f k8s/ | |
| env: | |
| KUBECONFIG: ${{ secrets.KUBECONFIG_STAGING }} | |
| - name: Run smoke tests | |
| run: | | |
| echo "Running smoke tests..." | |
| # 这里添加冒烟测试脚本 | |
| - name: Deploy to production | |
| if: success() | |
| run: | | |
| echo "Deploying to production..." | |
| # 这里添加生产环境部署脚本 | |
| env: | |
| KUBECONFIG: ${{ secrets.KUBECONFIG_PRODUCTION }} | |
| documentation: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: Generate API documentation | |
| run: ./mvnw javadoc:javadoc | |
| - name: Deploy to GitHub Pages | |
| uses: peaceiris/actions-gh-pages@v3 | |
| with: | |
| github_token: ${{ secrets.GITHUB_TOKEN }} | |
| publish_dir: ./target/site/apidocs | |
| destination_dir: ./docs/api | |
| if: github.event_name == 'push' && github.ref == 'refs/heads/main' |