|
| 1 | +name: ShramikLink CI/CD |
| 2 | + |
| 3 | +# Runs on every push to main or pull request |
| 4 | +# This ensures code is always tested before merging |
| 5 | +on: |
| 6 | + push: |
| 7 | + branches: [main, develop] |
| 8 | + pull_request: |
| 9 | + branches: [main] |
| 10 | + |
| 11 | +jobs: |
| 12 | + # ============================================ |
| 13 | + # JOB 1: Test and build the Spring Boot backend |
| 14 | + # ============================================ |
| 15 | + backend-test: |
| 16 | + name: Backend Tests |
| 17 | + runs-on: ubuntu-latest |
| 18 | + |
| 19 | + services: |
| 20 | + # Start a PostgreSQL container for integration tests |
| 21 | + postgres: |
| 22 | + image: postgres:15 |
| 23 | + env: |
| 24 | + POSTGRES_DB: shramiklink_test |
| 25 | + POSTGRES_USER: shramiklink |
| 26 | + POSTGRES_PASSWORD: shramiklink123 |
| 27 | + ports: |
| 28 | + - 5432:5432 |
| 29 | + options: >- |
| 30 | + --health-cmd pg_isready |
| 31 | + --health-interval 10s |
| 32 | + --health-timeout 5s |
| 33 | + --health-retries 5 |
| 34 | +
|
| 35 | + # Start Redis for cache tests |
| 36 | + redis: |
| 37 | + image: redis:7 |
| 38 | + ports: |
| 39 | + - 6379:6379 |
| 40 | + options: >- |
| 41 | + --health-cmd "redis-cli ping" |
| 42 | + --health-interval 10s |
| 43 | + --health-timeout 5s |
| 44 | + --health-retries 5 |
| 45 | +
|
| 46 | + steps: |
| 47 | + # Checkout the code |
| 48 | + - uses: actions/checkout@v4 |
| 49 | + |
| 50 | + # Set up Java 17 (matches our pom.xml) |
| 51 | + - name: Set up Java 17 |
| 52 | + uses: actions/setup-java@v4 |
| 53 | + with: |
| 54 | + java-version: '17' |
| 55 | + distribution: 'temurin' |
| 56 | + |
| 57 | + # Cache Maven dependencies to speed up builds |
| 58 | + - name: Cache Maven packages |
| 59 | + uses: actions/cache@v3 |
| 60 | + with: |
| 61 | + path: ~/.m2 |
| 62 | + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} |
| 63 | + |
| 64 | + # Run tests |
| 65 | + - name: Run backend tests |
| 66 | + working-directory: ./backend |
| 67 | + env: |
| 68 | + DATABASE_URL: jdbc:postgresql://localhost:5432/shramiklink_test |
| 69 | + DB_USERNAME: shramiklink |
| 70 | + DB_PASSWORD: shramiklink123 |
| 71 | + REDIS_HOST: localhost |
| 72 | + JWT_SECRET: test-secret-key-for-ci-environment |
| 73 | + run: ./mvnw test |
| 74 | + |
| 75 | + # Build the JAR (skip tests since we just ran them) |
| 76 | + - name: Build backend JAR |
| 77 | + working-directory: ./backend |
| 78 | + run: ./mvnw package -DskipTests |
| 79 | + |
| 80 | + # ============================================ |
| 81 | + # JOB 2: Build the Flutter app |
| 82 | + # ============================================ |
| 83 | + flutter-build: |
| 84 | + name: Flutter Build |
| 85 | + runs-on: ubuntu-latest |
| 86 | + |
| 87 | + steps: |
| 88 | + - uses: actions/checkout@v4 |
| 89 | + |
| 90 | + # Set up Flutter SDK |
| 91 | + - name: Set up Flutter |
| 92 | + uses: subosito/flutter-action@v2 |
| 93 | + with: |
| 94 | + flutter-version: '3.16.0' |
| 95 | + |
| 96 | + # Get Dart/Flutter packages |
| 97 | + - name: Get Flutter packages |
| 98 | + working-directory: ./flutter_app |
| 99 | + run: flutter pub get |
| 100 | + |
| 101 | + # Analyze code for warnings/errors |
| 102 | + - name: Flutter analyze |
| 103 | + working-directory: ./flutter_app |
| 104 | + run: flutter analyze |
| 105 | + |
| 106 | + # Build APK (release mode — optimized, smaller file) |
| 107 | + - name: Build Android APK |
| 108 | + working-directory: ./flutter_app |
| 109 | + run: flutter build apk --release |
| 110 | + |
| 111 | + # Upload the APK so it can be downloaded from GitHub |
| 112 | + - name: Upload APK |
| 113 | + uses: actions/upload-artifact@v3 |
| 114 | + with: |
| 115 | + name: shramiklink-release-apk |
| 116 | + path: flutter_app/build/app/outputs/flutter-apk/app-release.apk |
| 117 | + |
| 118 | + # ============================================ |
| 119 | + # JOB 3: Deploy to server (only on main branch) |
| 120 | + # ============================================ |
| 121 | + deploy: |
| 122 | + name: Deploy Backend |
| 123 | + runs-on: ubuntu-latest |
| 124 | + needs: [backend-test, flutter-build] # Only runs if both tests pass |
| 125 | + if: github.ref == 'refs/heads/main' # Only deploy from main branch |
| 126 | + |
| 127 | + steps: |
| 128 | + - uses: actions/checkout@v4 |
| 129 | + |
| 130 | + # Build and push Docker image to GitHub Container Registry |
| 131 | + - name: Log in to GitHub Container Registry |
| 132 | + uses: docker/login-action@v3 |
| 133 | + with: |
| 134 | + registry: ghcr.io |
| 135 | + username: ${{ github.actor }} |
| 136 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 137 | + |
| 138 | + - name: Build and push Docker image |
| 139 | + uses: docker/build-push-action@v5 |
| 140 | + with: |
| 141 | + context: ./backend |
| 142 | + push: true |
| 143 | + tags: ghcr.io/${{ github.repository }}/backend:latest |
| 144 | + |
| 145 | + # SSH into server and pull the new image |
| 146 | + # Add SSH_PRIVATE_KEY and SERVER_HOST in GitHub repo Secrets |
| 147 | + - name: Deploy to server via SSH |
| 148 | + uses: appleboy/ssh-action@v1.0.0 |
| 149 | + with: |
| 150 | + host: ${{ secrets.SERVER_HOST }} |
| 151 | + username: ubuntu |
| 152 | + key: ${{ secrets.SSH_PRIVATE_KEY }} |
| 153 | + script: | |
| 154 | + cd /opt/shramiklink |
| 155 | + docker-compose pull backend |
| 156 | + docker-compose up -d backend |
| 157 | + echo "✅ Deployment complete!" |
0 commit comments