|
| 1 | +name: iOS Automation Test Workflow |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: # Trigger this workflow on pull request events |
| 5 | + types: |
| 6 | + - opened |
| 7 | + |
| 8 | +jobs: |
| 9 | + build-and-test: |
| 10 | + name: Build and Test |
| 11 | + runs-on: macos-latest |
| 12 | + timeout-minutes: 45 # Prevents the workflow from hanging indefinitely |
| 13 | + |
| 14 | + steps: |
| 15 | + # Step 1: Checkout the code |
| 16 | + - name: Checkout Repository |
| 17 | + uses: actions/checkout@v3 |
| 18 | + |
| 19 | + # Step 2: Fetch PR Branch Information |
| 20 | + - name: Fetch PR Comments |
| 21 | + id: fetch-branches |
| 22 | + uses: actions/github-script@v6 |
| 23 | + with: |
| 24 | + script: | |
| 25 | + const prBody = context.payload.pull_request?.body; |
| 26 | + if (!prBody) { |
| 27 | + core.setFailed("No pull request body found."); |
| 28 | + return; |
| 29 | + } |
| 30 | +
|
| 31 | + console.log("Pull request body:", prBody); // Debugging: log the PR body |
| 32 | +
|
| 33 | + const kmChatUIBranchMatch = prBody.match(/KM_ChatUI_Branch\s*:\s*`([^`]+)`/); |
| 34 | + const kmCoreBranchMatch = prBody.match(/KM_Core_Branch\s*:\s*`([^`]+)`/); |
| 35 | +
|
| 36 | + if (!kmChatUIBranchMatch || !kmCoreBranchMatch) { |
| 37 | + core.setFailed("No branch information found in the pull request body."); |
| 38 | + return; |
| 39 | + } |
| 40 | +
|
| 41 | + core.setOutput("kmChatUIBranch", kmChatUIBranchMatch[1].trim()); |
| 42 | + core.setOutput("kmCoreBranch", kmCoreBranchMatch[1].trim()); |
| 43 | +
|
| 44 | + # Step 3: Set up Xcode |
| 45 | + - name: Set up Xcode |
| 46 | + uses: maxim-lobanov/setup-xcode@v1 |
| 47 | + with: |
| 48 | + xcode-version: '16.1.0' |
| 49 | + |
| 50 | + # Step 4: Set Environment Variables |
| 51 | + - name: Set Environment Variables |
| 52 | + run: | |
| 53 | + echo "KM_CHATUI_BRANCH=${{ steps.fetch-branches.outputs.kmChatUIBranch }}" >> $GITHUB_ENV |
| 54 | + echo "KM_CORE_BRANCH=${{ steps.fetch-branches.outputs.kmCoreBranch }}" >> $GITHUB_ENV |
| 55 | +
|
| 56 | + # Step 5: Install dependencies |
| 57 | + - name: Install Dependencies |
| 58 | + run: | |
| 59 | + pod install --project-directory=Example |
| 60 | +
|
| 61 | + # Step 4: Install Certificates, Provisioning Profiles, and Set Up Keychain |
| 62 | + - name: Install Apple Certificate, Provisioning Profile, and Set Up Keychain |
| 63 | + env: |
| 64 | + BUILD_CERTIFICATE_BASE64: ${{ secrets.BUILD_CERTIFICATE_BASE64 }} |
| 65 | + P12_PASSWORD: ${{ secrets.P12_PASSWORD }} |
| 66 | + BUILD_PROVISION_PROFILE_BASE64: ${{ secrets.BUILD_PROVISION_PROFILE_BASE64 }} |
| 67 | + KEYCHAIN_PASSWORD: ${{ secrets.KEYCHAIN_PASSWORD }} |
| 68 | + run: | |
| 69 | + # Define file paths |
| 70 | + CERTIFICATE_PATH=$RUNNER_TEMP/build_certificate.p12 |
| 71 | + PP_PATH=$RUNNER_TEMP/build_pp.mobileprovision |
| 72 | + KEYCHAIN_PATH=$RUNNER_TEMP/app-signing.keychain-db |
| 73 | +
|
| 74 | + # Decode the certificate and provisioning profile from secrets |
| 75 | + echo -n "$BUILD_CERTIFICATE_BASE64" | base64 --decode -o $CERTIFICATE_PATH |
| 76 | + echo -n "$BUILD_PROVISION_PROFILE_BASE64" | base64 --decode -o $PP_PATH |
| 77 | +
|
| 78 | + # Create and configure a temporary keychain |
| 79 | + security create-keychain -p "$KEYCHAIN_PASSWORD" $KEYCHAIN_PATH |
| 80 | + security set-keychain-settings -lut 3600 $KEYCHAIN_PATH |
| 81 | + security unlock-keychain -p "$KEYCHAIN_PASSWORD" $KEYCHAIN_PATH |
| 82 | + security list-keychain -d user -s $KEYCHAIN_PATH |
| 83 | +
|
| 84 | + # Import the certificate into the keychain |
| 85 | + security import $CERTIFICATE_PATH -P "$P12_PASSWORD" -A -t cert -f pkcs12 -k $KEYCHAIN_PATH |
| 86 | +
|
| 87 | + # Copy the provisioning profile to the expected location |
| 88 | + mkdir -p ~/Library/MobileDevice/Provisioning\ Profiles |
| 89 | + cp $PP_PATH ~/Library/MobileDevice/Provisioning\ Profiles |
| 90 | +
|
| 91 | + # Step 5: Update Info.plist Files with Secrets |
| 92 | + - name: Append new key in Info.plist for Example |
| 93 | + run: | |
| 94 | + PLIST_PATH="Example/Kommunicate/Info.plist" |
| 95 | + KEY="KOMMUNICATE_APP_ID" |
| 96 | + VALUE="${{ secrets.KOMMUNICATE_APP_ID }}" |
| 97 | + /usr/libexec/PlistBuddy -c "Set :$KEY $VALUE" "$PLIST_PATH" || \ |
| 98 | + /usr/libexec/PlistBuddy -c "Add :$KEY string $VALUE" "$PLIST_PATH" |
| 99 | + cat "$PLIST_PATH" |
| 100 | +
|
| 101 | + - name: Append new key in Info.plist for Tests |
| 102 | + run: | |
| 103 | + PLIST_PATH="Example/Tests/Info.plist" |
| 104 | + KEY="KOMMUNICATE_APP_ID" |
| 105 | + VALUE="${{ secrets.KOMMUNICATE_APP_ID }}" |
| 106 | + /usr/libexec/PlistBuddy -c "Set :$KEY $VALUE" "$PLIST_PATH" || \ |
| 107 | + /usr/libexec/PlistBuddy -c "Add :$KEY string $VALUE" "$PLIST_PATH" |
| 108 | + cat "$PLIST_PATH" |
| 109 | +
|
| 110 | + - name: Append new key in Info.plist for UI Tests |
| 111 | + run: | |
| 112 | + PLIST_PATH="Example/Kommunicate_ExampleUITests/Info.plist" |
| 113 | + KEY="KOMMUNICATE_APP_ID" |
| 114 | + VALUE="${{ secrets.KOMMUNICATE_APP_ID }}" |
| 115 | + /usr/libexec/PlistBuddy -c "Set :$KEY $VALUE" "$PLIST_PATH" || \ |
| 116 | + /usr/libexec/PlistBuddy -c "Add :$KEY string $VALUE" "$PLIST_PATH" |
| 117 | + cat "$PLIST_PATH" |
| 118 | +
|
| 119 | + # Step 6: Run Tests with Keychain Management |
| 120 | + - name: Run XCTest with Keychain |
| 121 | + env: |
| 122 | + KOMMUNICATE_APP_ID: ${{ secrets.KOMMUNICATE_APP_ID }} |
| 123 | + KEYCHAIN_PASSWORD: ${{ secrets.KEYCHAIN_PASSWORD }} # Keychain password |
| 124 | + TEAM_ID: ${{ secrets.TEAM_ID }} # Apple Developer Team ID |
| 125 | + run: | |
| 126 | + # Unlock the keychain for signing during tests |
| 127 | + KEYCHAIN_PATH=$RUNNER_TEMP/app-signing.keychain-db |
| 128 | + security unlock-keychain -p "$KEYCHAIN_PASSWORD" "$KEYCHAIN_PATH" |
| 129 | + |
| 130 | + # Run the tests, ensuring code signing is handled |
| 131 | + xcodebuild test \ |
| 132 | + -workspace Example/Kommunicate.xcworkspace \ |
| 133 | + -scheme Kommunicate_Example \ |
| 134 | + -destination 'platform=iOS Simulator,name=iPhone 16 Pro Max,OS=18.1' \ |
| 135 | + -enableCodeCoverage YES \ |
| 136 | + -derivedDataPath ./DerivedData \ |
| 137 | + CODE_SIGN_IDENTITY="iPhone Developer" \ |
| 138 | + CODE_SIGNING_ALLOWED=YES \ |
| 139 | + CODE_SIGNING_REQUIRED=YES \ |
| 140 | + DEVELOPMENT_TEAM="$TEAM_ID" \ |
| 141 | + KEYCHAIN="$KEYCHAIN_PATH" |
| 142 | +
|
| 143 | + # Step 7: Print Test Results |
| 144 | + - name: Print Test Results |
| 145 | + run: | |
| 146 | + cat ./DerivedData/Logs/Test/*.xcresult/TestSummaries.plist || echo "No test results found." |
| 147 | +
|
| 148 | + # Step 8: Debug Environment Variables (Optional) |
| 149 | + - name: Debug Environment Variables |
| 150 | + run: | |
| 151 | + echo "Environment variables set successfully." |
| 152 | +
|
| 153 | + # Step 9: Post Test Results as a PR Comment |
| 154 | + - name: Post Test Results to PR |
| 155 | + if: github.event_name == 'pull_request' |
| 156 | + uses: actions/github-script@v6 |
| 157 | + with: |
| 158 | + script: | |
| 159 | + const fs = require('fs'); |
| 160 | + const path = './DerivedData/Logs/Test/*.xcresult/TestSummaries.plist'; |
| 161 | + let content = 'Test results not found.'; |
| 162 | +
|
| 163 | + try { |
| 164 | + content = fs.readFileSync(path, 'utf8'); |
| 165 | + } catch (err) { |
| 166 | + console.log('Error reading test results:', err); |
| 167 | + } |
| 168 | +
|
| 169 | + const comment = ` |
| 170 | + ## iOS Automation Test Results |
| 171 | + Congratulations All Tests Passed! 🎉 |
| 172 | + `; |
| 173 | +
|
| 174 | + await github.rest.issues.createComment({ |
| 175 | + issue_number: context.issue.number, |
| 176 | + owner: context.repo.owner, |
| 177 | + repo: context.repo.repo, |
| 178 | + body: comment, |
| 179 | + }); |
0 commit comments