|
1 | | -name: Deploy |
| 1 | +name: Build & Deploy |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | push: |
5 | | - branches: [ main ] |
6 | | - tags: |
7 | | - - 'v*' |
| 5 | + branches: [main] |
| 6 | + tags: ['v*'] |
8 | 7 |
|
9 | 8 | jobs: |
10 | | - deploy: |
| 9 | + # ── 1. Build WAR ───────────────────────────────────────────────────────────── |
| 10 | + build: |
| 11 | + name: Build WAR |
11 | 12 | runs-on: ubuntu-latest |
12 | | - if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v') |
13 | | - |
| 13 | + |
14 | 14 | steps: |
15 | | - - uses: actions/checkout@v3 |
16 | | - |
17 | | - - name: Set up JDK 11 |
18 | | - uses: actions/setup-java@v3 |
19 | | - with: |
20 | | - java-version: '11' |
21 | | - distribution: 'temurin' |
22 | | - |
23 | | - - name: Build WAR file |
24 | | - run: mvn clean package -DskipTests |
25 | | - |
26 | | - - name: Deploy to staging |
27 | | - run: | |
28 | | - echo "Deployment would occur here" |
29 | | - echo "Built WAR: target/*.war" |
30 | | - # Add actual deployment steps (e.g., SCP, Docker push, etc.) |
| 15 | + - uses: actions/checkout@v4 |
| 16 | + |
| 17 | + - name: Set up JDK 11 |
| 18 | + uses: actions/setup-java@v4 |
| 19 | + with: |
| 20 | + java-version: '11' |
| 21 | + distribution: 'temurin' |
| 22 | + |
| 23 | + - name: Cache Maven dependencies |
| 24 | + uses: actions/cache@v4 |
| 25 | + with: |
| 26 | + path: ~/.m2 |
| 27 | + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} |
| 28 | + restore-keys: ${{ runner.os }}-m2 |
| 29 | + |
| 30 | + - name: Build WAR (skip tests — tests run in CI) |
| 31 | + run: mvn -B clean package -DskipTests |
| 32 | + |
| 33 | + - name: Upload WAR artifact |
| 34 | + uses: actions/upload-artifact@v4 |
| 35 | + with: |
| 36 | + name: iot-bay-war |
| 37 | + path: target/iot-bay.war |
| 38 | + retention-days: 1 |
| 39 | + |
| 40 | + # ── 2. Build & Push Docker image to GHCR ──────────────────────────────────── |
| 41 | + docker: |
| 42 | + name: Docker Build & Push |
| 43 | + needs: build |
| 44 | + runs-on: ubuntu-latest |
| 45 | + permissions: |
| 46 | + contents: read |
| 47 | + packages: write |
| 48 | + |
| 49 | + steps: |
| 50 | + - uses: actions/checkout@v4 |
| 51 | + |
| 52 | + - name: Download WAR artifact |
| 53 | + uses: actions/download-artifact@v4 |
| 54 | + with: |
| 55 | + name: iot-bay-war |
| 56 | + path: target/ |
| 57 | + |
| 58 | + - name: Log in to GitHub Container Registry |
| 59 | + uses: docker/login-action@v3 |
| 60 | + with: |
| 61 | + registry: ghcr.io |
| 62 | + username: ${{ github.actor }} |
| 63 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 64 | + |
| 65 | + - name: Extract Docker metadata |
| 66 | + id: meta |
| 67 | + uses: docker/metadata-action@v5 |
| 68 | + with: |
| 69 | + images: ghcr.io/${{ github.repository }} |
| 70 | + tags: | |
| 71 | + type=raw,value=latest,enable={{is_default_branch}} |
| 72 | + type=sha,prefix=sha-,format=short |
| 73 | + type=semver,pattern={{version}} |
| 74 | +
|
| 75 | + - name: Build and push Docker image |
| 76 | + uses: docker/build-push-action@v5 |
| 77 | + with: |
| 78 | + context: . |
| 79 | + push: true |
| 80 | + tags: ${{ steps.meta.outputs.tags }} |
| 81 | + labels: ${{ steps.meta.outputs.labels }} |
| 82 | + cache-from: type=gha |
| 83 | + cache-to: type=gha,mode=max |
0 commit comments