|
| 1 | +# |
| 2 | +# .github/workflows/android-kotlin-browserstack.yml |
| 3 | +# Workflow for building and testing android-kotlin on BrowserStack physical devices |
| 4 | +# |
| 5 | +--- |
| 6 | +name: android-kotlin-browserstack |
| 7 | + |
| 8 | +on: |
| 9 | + pull_request: |
| 10 | + branches: [main] |
| 11 | + paths: |
| 12 | + - 'android-kotlin/**' |
| 13 | + - '.github/workflows/android-kotlin-browserstack.yml' |
| 14 | + push: |
| 15 | + branches: [main] |
| 16 | + paths: |
| 17 | + - 'android-kotlin/**' |
| 18 | + - '.github/workflows/android-kotlin-browserstack.yml' |
| 19 | + workflow_dispatch: # Allow manual trigger |
| 20 | + |
| 21 | +concurrency: |
| 22 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 23 | + cancel-in-progress: true |
| 24 | + |
| 25 | +jobs: |
| 26 | + build-and-test: |
| 27 | + name: Build and Test on BrowserStack |
| 28 | + runs-on: ubuntu-latest |
| 29 | + |
| 30 | + steps: |
| 31 | + - name: Checkout code |
| 32 | + uses: actions/checkout@v4 |
| 33 | + |
| 34 | + - name: Set up JDK 17 |
| 35 | + uses: actions/setup-java@v4 |
| 36 | + with: |
| 37 | + java-version: '17' |
| 38 | + distribution: 'temurin' |
| 39 | + |
| 40 | + - name: Setup Android SDK |
| 41 | + uses: android-actions/setup-android@v3 |
| 42 | + |
| 43 | + - name: Setup Gradle |
| 44 | + uses: gradle/actions/setup-gradle@v3 |
| 45 | + |
| 46 | + - name: Create .env file |
| 47 | + run: | |
| 48 | + echo "DITTO_APP_ID=${{ secrets.DITTO_APP_ID }}" > .env |
| 49 | + echo "DITTO_PLAYGROUND_TOKEN=${{ secrets.DITTO_PLAYGROUND_TOKEN }}" >> .env |
| 50 | + echo "DITTO_AUTH_URL=${{ secrets.DITTO_AUTH_URL }}" >> .env |
| 51 | + echo "DITTO_WEBSOCKET_URL=${{ secrets.DITTO_WEBSOCKET_URL }}" >> .env |
| 52 | +
|
| 53 | + - name: Cache Gradle dependencies |
| 54 | + uses: actions/cache@v4 |
| 55 | + with: |
| 56 | + path: | |
| 57 | + ~/.gradle/caches |
| 58 | + ~/.gradle/wrapper |
| 59 | + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} |
| 60 | + restore-keys: | |
| 61 | + ${{ runner.os }}-gradle- |
| 62 | +
|
| 63 | + - name: Build APK |
| 64 | + working-directory: android-kotlin/QuickStartTasks |
| 65 | + run: | |
| 66 | + ./gradlew assembleDebug assembleDebugAndroidTest |
| 67 | + echo "APK built successfully" |
| 68 | +
|
| 69 | + - name: Run Unit Tests |
| 70 | + working-directory: android-kotlin/QuickStartTasks |
| 71 | + run: ./gradlew test |
| 72 | + |
| 73 | + - name: Upload APKs to BrowserStack |
| 74 | + id: upload |
| 75 | + run: | |
| 76 | + # Upload app APK |
| 77 | + echo "Uploading app APK..." |
| 78 | + APP_UPLOAD_RESPONSE=$(curl -u "${{ secrets.BROWSERSTACK_USERNAME }}:${{ secrets.BROWSERSTACK_ACCESS_KEY }}" \ |
| 79 | + -X POST "https://api-cloud.browserstack.com/app-automate/upload" \ |
| 80 | + -F "file=@android-kotlin/QuickStartTasks/app/build/outputs/apk/debug/app-debug.apk" \ |
| 81 | + -F "custom_id=ditto-android-kotlin-app") |
| 82 | + |
| 83 | + echo "App upload response: $APP_UPLOAD_RESPONSE" |
| 84 | + APP_URL=$(echo $APP_UPLOAD_RESPONSE | jq -r .app_url) |
| 85 | + |
| 86 | + if [ "$APP_URL" = "null" ] || [ -z "$APP_URL" ]; then |
| 87 | + echo "Error: Failed to upload app APK" |
| 88 | + echo "Response: $APP_UPLOAD_RESPONSE" |
| 89 | + exit 1 |
| 90 | + fi |
| 91 | + |
| 92 | + echo "app_url=$APP_URL" >> $GITHUB_OUTPUT |
| 93 | + echo "App uploaded successfully: $APP_URL" |
| 94 | + |
| 95 | + # Upload test APK |
| 96 | + echo "Uploading test APK..." |
| 97 | + TEST_UPLOAD_RESPONSE=$(curl -u "${{ secrets.BROWSERSTACK_USERNAME }}:${{ secrets.BROWSERSTACK_ACCESS_KEY }}" \ |
| 98 | + -X POST "https://api-cloud.browserstack.com/app-automate/espresso/test-suite" \ |
| 99 | + -F "file=@android-kotlin/QuickStartTasks/app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk" \ |
| 100 | + -F "custom_id=ditto-android-kotlin-test") |
| 101 | + |
| 102 | + echo "Test upload response: $TEST_UPLOAD_RESPONSE" |
| 103 | + TEST_URL=$(echo $TEST_UPLOAD_RESPONSE | jq -r .test_suite_url) |
| 104 | + |
| 105 | + if [ "$TEST_URL" = "null" ] || [ -z "$TEST_URL" ]; then |
| 106 | + echo "Error: Failed to upload test APK" |
| 107 | + echo "Response: $TEST_UPLOAD_RESPONSE" |
| 108 | + exit 1 |
| 109 | + fi |
| 110 | + |
| 111 | + echo "test_url=$TEST_URL" >> $GITHUB_OUTPUT |
| 112 | + echo "Test APK uploaded successfully: $TEST_URL" |
| 113 | +
|
| 114 | + - name: Execute tests on BrowserStack |
| 115 | + id: test |
| 116 | + run: | |
| 117 | + # Validate inputs before creating test execution request |
| 118 | + APP_URL="${{ steps.upload.outputs.app_url }}" |
| 119 | + TEST_URL="${{ steps.upload.outputs.test_url }}" |
| 120 | + |
| 121 | + echo "App URL: $APP_URL" |
| 122 | + echo "Test URL: $TEST_URL" |
| 123 | + |
| 124 | + if [ -z "$APP_URL" ] || [ "$APP_URL" = "null" ]; then |
| 125 | + echo "Error: No valid app URL available" |
| 126 | + exit 1 |
| 127 | + fi |
| 128 | + |
| 129 | + if [ -z "$TEST_URL" ] || [ "$TEST_URL" = "null" ]; then |
| 130 | + echo "Error: No valid test URL available" |
| 131 | + exit 1 |
| 132 | + fi |
| 133 | + |
| 134 | + # Create test execution request |
| 135 | + BUILD_RESPONSE=$(curl -u "${{ secrets.BROWSERSTACK_USERNAME }}:${{ secrets.BROWSERSTACK_ACCESS_KEY }}" \ |
| 136 | + -X POST "https://api-cloud.browserstack.com/app-automate/espresso/v2/build" \ |
| 137 | + -H "Content-Type: application/json" \ |
| 138 | + -d "{ |
| 139 | + \"app\": \"$APP_URL\", |
| 140 | + \"testSuite\": \"$TEST_URL\", |
| 141 | + \"devices\": [ |
| 142 | + \"Google Pixel 8-14.0\", |
| 143 | + \"Samsung Galaxy S23-13.0\", |
| 144 | + \"Google Pixel 6-12.0\", |
| 145 | + \"OnePlus 9-11.0\" |
| 146 | + ], |
| 147 | + \"project\": \"Ditto Android Kotlin\", |
| 148 | + \"buildName\": \"Build #${{ github.run_number }}\", |
| 149 | + \"buildTag\": \"${{ github.ref_name }}\", |
| 150 | + \"deviceLogs\": true, |
| 151 | + \"video\": true, |
| 152 | + \"networkLogs\": true, |
| 153 | + \"autoGrantPermissions\": true |
| 154 | + }") |
| 155 | + |
| 156 | + echo "BrowserStack API Response:" |
| 157 | + echo "$BUILD_RESPONSE" |
| 158 | + |
| 159 | + BUILD_ID=$(echo "$BUILD_RESPONSE" | jq -r .build_id) |
| 160 | + |
| 161 | + # Check if BUILD_ID is null or empty |
| 162 | + if [ "$BUILD_ID" = "null" ] || [ -z "$BUILD_ID" ]; then |
| 163 | + echo "Error: Failed to create BrowserStack build" |
| 164 | + echo "Response: $BUILD_RESPONSE" |
| 165 | + exit 1 |
| 166 | + fi |
| 167 | + |
| 168 | + echo "build_id=$BUILD_ID" >> $GITHUB_OUTPUT |
| 169 | + echo "Build started with ID: $BUILD_ID" |
| 170 | +
|
| 171 | + - name: Wait for BrowserStack tests to complete |
| 172 | + run: | |
| 173 | + BUILD_ID="${{ steps.test.outputs.build_id }}" |
| 174 | + |
| 175 | + # Validate BUILD_ID before proceeding |
| 176 | + if [ "$BUILD_ID" = "null" ] || [ -z "$BUILD_ID" ]; then |
| 177 | + echo "Error: No valid BUILD_ID available. Skipping test monitoring." |
| 178 | + exit 1 |
| 179 | + fi |
| 180 | + |
| 181 | + MAX_WAIT_TIME=1800 # 30 minutes |
| 182 | + CHECK_INTERVAL=30 # Check every 30 seconds |
| 183 | + ELAPSED=0 |
| 184 | + |
| 185 | + while [ $ELAPSED -lt $MAX_WAIT_TIME ]; do |
| 186 | + BUILD_STATUS_RESPONSE=$(curl -s -u "${{ secrets.BROWSERSTACK_USERNAME }}:${{ secrets.BROWSERSTACK_ACCESS_KEY }}" \ |
| 187 | + "https://api-cloud.browserstack.com/app-automate/espresso/v2/builds/$BUILD_ID") |
| 188 | + |
| 189 | + BUILD_STATUS=$(echo "$BUILD_STATUS_RESPONSE" | jq -r .status) |
| 190 | + |
| 191 | + # Check for API errors |
| 192 | + if [ "$BUILD_STATUS" = "null" ] || [ -z "$BUILD_STATUS" ]; then |
| 193 | + echo "Error getting build status. Response: $BUILD_STATUS_RESPONSE" |
| 194 | + sleep $CHECK_INTERVAL |
| 195 | + ELAPSED=$((ELAPSED + CHECK_INTERVAL)) |
| 196 | + continue |
| 197 | + fi |
| 198 | + |
| 199 | + echo "Build status: $BUILD_STATUS (elapsed: ${ELAPSED}s)" |
| 200 | + echo "Full response: $BUILD_STATUS_RESPONSE" |
| 201 | + |
| 202 | + # Check for completion states - BrowserStack uses different status values |
| 203 | + if [ "$BUILD_STATUS" = "done" ] || [ "$BUILD_STATUS" = "failed" ] || [ "$BUILD_STATUS" = "error" ] || [ "$BUILD_STATUS" = "passed" ] || [ "$BUILD_STATUS" = "completed" ]; then |
| 204 | + echo "Build completed with status: $BUILD_STATUS" |
| 205 | + break |
| 206 | + fi |
| 207 | + |
| 208 | + sleep $CHECK_INTERVAL |
| 209 | + ELAPSED=$((ELAPSED + CHECK_INTERVAL)) |
| 210 | + done |
| 211 | + |
| 212 | + # Get final results |
| 213 | + FINAL_RESULT=$(curl -s -u "${{ secrets.BROWSERSTACK_USERNAME }}:${{ secrets.BROWSERSTACK_ACCESS_KEY }}" \ |
| 214 | + "https://api-cloud.browserstack.com/app-automate/espresso/v2/builds/$BUILD_ID") |
| 215 | + |
| 216 | + echo "Final build result:" |
| 217 | + echo "$FINAL_RESULT" | jq . |
| 218 | + |
| 219 | + # Check if we got valid results |
| 220 | + if echo "$FINAL_RESULT" | jq -e .devices > /dev/null 2>&1; then |
| 221 | + # Check if the overall build passed |
| 222 | + BUILD_STATUS=$(echo "$FINAL_RESULT" | jq -r .status) |
| 223 | + if [ "$BUILD_STATUS" != "passed" ]; then |
| 224 | + echo "Build failed with status: $BUILD_STATUS" |
| 225 | + |
| 226 | + # Check each device for failures |
| 227 | + FAILED_TESTS=$(echo "$FINAL_RESULT" | jq -r '.devices[] | select(.sessions[].status != "passed") | .device') |
| 228 | + |
| 229 | + if [ -n "$FAILED_TESTS" ]; then |
| 230 | + echo "Tests failed on devices: $FAILED_TESTS" |
| 231 | + fi |
| 232 | + |
| 233 | + exit 1 |
| 234 | + else |
| 235 | + echo "All tests passed successfully!" |
| 236 | + fi |
| 237 | + else |
| 238 | + echo "Warning: Could not parse final results" |
| 239 | + echo "Raw response: $FINAL_RESULT" |
| 240 | + fi |
| 241 | +
|
| 242 | + - name: Generate test report |
| 243 | + if: always() |
| 244 | + run: | |
| 245 | + BUILD_ID="${{ steps.test.outputs.build_id }}" |
| 246 | + |
| 247 | + # Create test report |
| 248 | + echo "# BrowserStack Test Report" > test-report.md |
| 249 | + echo "" >> test-report.md |
| 250 | + |
| 251 | + if [ "$BUILD_ID" = "null" ] || [ -z "$BUILD_ID" ]; then |
| 252 | + echo "Build ID: N/A (Build creation failed)" >> test-report.md |
| 253 | + echo "" >> test-report.md |
| 254 | + echo "## Error" >> test-report.md |
| 255 | + echo "Failed to create BrowserStack build. Check the 'Execute tests on BrowserStack' step for details." >> test-report.md |
| 256 | + else |
| 257 | + echo "Build ID: $BUILD_ID" >> test-report.md |
| 258 | + echo "View full report: https://app-automate.browserstack.com/dashboard/v2/builds/$BUILD_ID" >> test-report.md |
| 259 | + echo "" >> test-report.md |
| 260 | + |
| 261 | + # Get detailed results |
| 262 | + RESULTS=$(curl -s -u "${{ secrets.BROWSERSTACK_USERNAME }}:${{ secrets.BROWSERSTACK_ACCESS_KEY }}" \ |
| 263 | + "https://api-cloud.browserstack.com/app-automate/espresso/v2/builds/$BUILD_ID") |
| 264 | + |
| 265 | + echo "## Device Results" >> test-report.md |
| 266 | + if echo "$RESULTS" | jq -e .devices > /dev/null 2>&1; then |
| 267 | + echo "$RESULTS" | jq -r '.devices[] | "- \(.device): \(.status)"' >> test-report.md |
| 268 | + else |
| 269 | + echo "Unable to retrieve device results" >> test-report.md |
| 270 | + fi |
| 271 | + fi |
| 272 | +
|
| 273 | + - name: Upload test artifacts |
| 274 | + if: always() |
| 275 | + uses: actions/upload-artifact@v4 |
| 276 | + with: |
| 277 | + name: test-results |
| 278 | + path: | |
| 279 | + android-kotlin/QuickStartTasks/app/build/outputs/apk/ |
| 280 | + android-kotlin/QuickStartTasks/app/build/reports/ |
| 281 | + test-report.md |
| 282 | +
|
| 283 | + - name: Comment PR with results |
| 284 | + if: github.event_name == 'pull_request' && always() |
| 285 | + uses: actions/github-script@v7 |
| 286 | + with: |
| 287 | + script: | |
| 288 | + const buildId = '${{ steps.test.outputs.build_id }}'; |
| 289 | + const status = '${{ job.status }}'; |
| 290 | + const runUrl = '${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}'; |
| 291 | + |
| 292 | + let body; |
| 293 | + if (buildId === 'null' || buildId === '' || !buildId) { |
| 294 | + body = `## 📱 BrowserStack Test Results |
| 295 | + |
| 296 | + **Status:** ❌ Failed (Build creation failed) |
| 297 | + **Build:** [#${{ github.run_number }}](${runUrl}) |
| 298 | + **Issue:** Failed to create BrowserStack build. Check the workflow logs for details. |
| 299 | + |
| 300 | + ### Expected Devices: |
| 301 | + - Google Pixel 8 (Android 14) |
| 302 | + - Samsung Galaxy S23 (Android 13) |
| 303 | + - Google Pixel 6 (Android 12) |
| 304 | + - OnePlus 9 (Android 11) |
| 305 | + `; |
| 306 | + } else { |
| 307 | + const bsUrl = `https://app-automate.browserstack.com/dashboard/v2/builds/${buildId}`; |
| 308 | + body = `## 📱 BrowserStack Test Results |
| 309 | + |
| 310 | + **Status:** ${status === 'success' ? '✅ Passed' : '❌ Failed'} |
| 311 | + **Build:** [#${{ github.run_number }}](${runUrl}) |
| 312 | + **BrowserStack:** [View detailed results](${bsUrl}) |
| 313 | + |
| 314 | + ### Tested Devices: |
| 315 | + - Google Pixel 8 (Android 14) |
| 316 | + - Samsung Galaxy S23 (Android 13) |
| 317 | + - Google Pixel 6 (Android 12) |
| 318 | + - OnePlus 9 (Android 11) |
| 319 | + `; |
| 320 | + } |
| 321 | + |
| 322 | + github.rest.issues.createComment({ |
| 323 | + issue_number: context.issue.number, |
| 324 | + owner: context.repo.owner, |
| 325 | + repo: context.repo.repo, |
| 326 | + body: body |
| 327 | + }); |
0 commit comments