|
| 1 | +name: CI Pipeline - Build, Test, Analyze and Deploy |
| 2 | +on: |
| 3 | + push: |
| 4 | + branches: |
| 5 | + - main |
| 6 | + pull_request: |
| 7 | + types: [opened, synchronize, reopened] |
| 8 | + |
| 9 | +jobs: |
| 10 | + build: |
| 11 | + name: Build, Test, and Analyze |
| 12 | + runs-on: ubuntu-latest |
| 13 | + strategy: |
| 14 | + fail-fast: false |
| 15 | + matrix: |
| 16 | + microservice: ['discovery-service', 'hospital-service', 'emergency-service'] |
| 17 | + |
| 18 | + steps: |
| 19 | + - uses: actions/checkout@v4 |
| 20 | + with: |
| 21 | + fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis |
| 22 | + |
| 23 | + - name: Set up JDK 21 |
| 24 | + uses: actions/setup-java@v4 |
| 25 | + with: |
| 26 | + java-version: 21 |
| 27 | + distribution: 'zulu' |
| 28 | + |
| 29 | + - name: Cache SonarQube packages |
| 30 | + uses: actions/cache@v4 |
| 31 | + with: |
| 32 | + path: ~/.sonar/cache |
| 33 | + key: ${{ runner.os }}-sonar |
| 34 | + restore-keys: ${{ runner.os }}-sonar |
| 35 | + |
| 36 | + - name: Cache Maven packages |
| 37 | + uses: actions/cache@v4 |
| 38 | + with: |
| 39 | + path: ~/.m2 |
| 40 | + key: ${{ runner.os }}-m2-${{ hashFiles(format('backend/{0}/pom.xml', matrix.microservice)) }} |
| 41 | + restore-keys: ${{ runner.os }}-m2 |
| 42 | + |
| 43 | + - name: Build and analyze |
| 44 | + # Run from repo root to properly reference the module path |
| 45 | + env: |
| 46 | + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
| 47 | + run: | |
| 48 | + mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar \ |
| 49 | + -Dsonar.projectKey=swyth-dev_realtime-emergency-response-system-${{ matrix.microservice }} \ |
| 50 | + -Dsonar.projectName=${{ matrix.service }} \ |
| 51 | + -f backend/${{ matrix.microservice }} |
| 52 | + |
| 53 | + - name: Save build artifacts |
| 54 | + uses: actions/upload-artifact@v4 |
| 55 | + with: |
| 56 | + name: ${{ matrix.microservice }}-build |
| 57 | + path: backend/${{ matrix.microservice }}/target/*.jar |
| 58 | + retention-days: 1 |
| 59 | + |
| 60 | + docker-build-push: |
| 61 | + name: Build and Push Docker Images |
| 62 | + needs: build |
| 63 | + runs-on: ubuntu-latest |
| 64 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 65 | + strategy: |
| 66 | + fail-fast: false |
| 67 | + matrix: |
| 68 | + microservice: ['discovery-service', 'hospital-service', 'emergency-service'] |
| 69 | + |
| 70 | + steps: |
| 71 | + - uses: actions/checkout@v4 |
| 72 | + |
| 73 | + - name: Download build artifacts |
| 74 | + uses: actions/download-artifact@v4 |
| 75 | + with: |
| 76 | + name: ${{ matrix.microservice }}-build |
| 77 | + path: backend/${{ matrix.microservice }}/target/ |
| 78 | + |
| 79 | + - name: Set up Docker Buildx |
| 80 | + uses: docker/setup-buildx-action@v3 |
| 81 | + |
| 82 | + - name: Login to Docker Hub |
| 83 | + uses: docker/login-action@v3 |
| 84 | + with: |
| 85 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 86 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 87 | + |
| 88 | + - name: Extract metadata for Docker |
| 89 | + id: meta |
| 90 | + uses: docker/metadata-action@v5 |
| 91 | + with: |
| 92 | + images: ${{ secrets.DOCKERHUB_USERNAME }}/${{ matrix.microservice }} |
| 93 | + tags: | |
| 94 | + type=ref,event=branch |
| 95 | + type=sha,format=short |
| 96 | + |
| 97 | + - name: Build and push Docker image |
| 98 | + uses: docker/build-push-action@v5 |
| 99 | + with: |
| 100 | + context: backend/${{ matrix.microservice }} |
| 101 | + push: true |
| 102 | + tags: ${{ steps.meta.outputs.tags }} |
| 103 | + labels: ${{ steps.meta.outputs.labels }} |
0 commit comments