|
| 1 | +name: Frontend CI + Sonar |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: ["**"] |
| 6 | + paths: |
| 7 | + - "src/**" |
| 8 | + - "package.json" |
| 9 | + - "package-lock.json" |
| 10 | + - "angular.json" |
| 11 | + - ".github/workflows/ci-sonar-angular.yml" |
| 12 | + - "sonar-project.properties" |
| 13 | + pull_request: |
| 14 | + |
| 15 | +permissions: |
| 16 | + contents: read |
| 17 | + pull-requests: write |
| 18 | + |
| 19 | +jobs: |
| 20 | + build-test-and-analyze: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + env: |
| 23 | + NODE_VERSION: 20 |
| 24 | + SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} |
| 25 | + CI: true |
| 26 | + |
| 27 | + steps: |
| 28 | + - name: Checkout |
| 29 | + uses: actions/checkout@v4 |
| 30 | + with: |
| 31 | + fetch-depth: 0 # Sonar cần full history để tính blame |
| 32 | + |
| 33 | + - name: Set up JDK (for Sonar scanner) |
| 34 | + uses: actions/setup-java@v4 |
| 35 | + with: |
| 36 | + distribution: temurin |
| 37 | + java-version: "21" |
| 38 | + |
| 39 | + - name: Set up Node |
| 40 | + uses: actions/setup-node@v4 |
| 41 | + with: |
| 42 | + node-version: ${{ env.NODE_VERSION }} |
| 43 | + cache: "npm" |
| 44 | + |
| 45 | + - name: Install Chrome (for Karma Headless) |
| 46 | + id: chrome |
| 47 | + uses: browser-actions/setup-chrome@v1 |
| 48 | + |
| 49 | + - name: Install dependencies |
| 50 | + run: npm ci |
| 51 | + |
| 52 | + - name: Build (production) |
| 53 | + run: npm run build |
| 54 | + |
| 55 | + - name: Unit Test + Coverage (Karma headless) |
| 56 | + env: |
| 57 | + CHROME_BIN: ${{ steps.chrome.outputs.chrome-path }} |
| 58 | + run: | |
| 59 | + # nếu bạn đã có cấu hình Karma CI riêng thì thay lệnh dưới bằng script đó |
| 60 | + npx ng test --watch=false --browsers=ChromeHeadless --code-coverage |
| 61 | +
|
| 62 | + - name: Upload coverage artifact |
| 63 | + if: always() |
| 64 | + uses: actions/upload-artifact@v4 |
| 65 | + with: |
| 66 | + name: coverage-frontend |
| 67 | + path: coverage/** |
| 68 | + if-no-files-found: warn |
| 69 | + |
| 70 | + # --- SonarCloud (mặc định) --- |
| 71 | + - name: SonarCloud Scan |
| 72 | + if: ${{ env.SONAR_HOST_URL == '' }} |
| 73 | + uses: SonarSource/sonarcloud-github-action@v2 |
| 74 | + env: |
| 75 | + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
| 76 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 77 | + with: |
| 78 | + args: > |
| 79 | + -Dsonar.qualitygate.wait=true |
| 80 | +
|
| 81 | + # --- SonarQube self-hosted (nếu bạn set SONAR_HOST_URL) --- |
| 82 | + - name: SonarQube Scan (self-hosted) |
| 83 | + if: ${{ env.SONAR_HOST_URL != '' }} |
| 84 | + uses: SonarSource/sonarqube-scan-action@v4 |
| 85 | + env: |
| 86 | + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
| 87 | + SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} |
| 88 | + with: |
| 89 | + args: > |
| 90 | + -Dsonar.qualitygate.wait=true |
0 commit comments