Skip to content

Added Github Actions for Automation Testing #78

Added Github Actions for Automation Testing

Added Github Actions for Automation Testing #78

Workflow file for this run

name: iOS Automation Test Workflow
on:
push:
branches:
- dev # Run the workflow on pushes to the dev branch
pull_request:
jobs:
build-and-test:
name: Build and Test
runs-on: macos-latest
timeout-minutes: 30 # Prevents the workflow from hanging indefinitely
steps:
# Step 1: Checkout the code
- name: Checkout Repository
uses: actions/checkout@v3
# Step 2: Set up Xcode
- name: Set up Xcode
uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: '16.1.0' # Replace with the Xcode version you need
# Step 3: Install dependencies (if using CocoaPods)
- name: Install Dependencies
run: |
pod install --project-directory=Example
# Step 4: Install Certificates, Provisioning Profiles, and Set Up Keychain
- name: Install Apple Certificate, Provisioning Profile, and Set Up Keychain
env:
BUILD_CERTIFICATE_BASE64: ${{ secrets.BUILD_CERTIFICATE_BASE64 }}
P12_PASSWORD: ${{ secrets.P12_PASSWORD }}
BUILD_PROVISION_PROFILE_BASE64: ${{ secrets.BUILD_PROVISION_PROFILE_BASE64 }}
KEYCHAIN_PASSWORD: ${{ secrets.KEYCHAIN_PASSWORD }}
run: |
# Define file paths
CERTIFICATE_PATH=$RUNNER_TEMP/build_certificate.p12
PP_PATH=$RUNNER_TEMP/build_pp.mobileprovision
KEYCHAIN_PATH=$RUNNER_TEMP/app-signing.keychain-db
echo "Keychain Path: $KEYCHAIN_PATH"
# Decode the certificate and provisioning profile from secrets
echo -n "$BUILD_CERTIFICATE_BASE64" | base64 --decode -o $CERTIFICATE_PATH
echo -n "$BUILD_PROVISION_PROFILE_BASE64" | base64 --decode -o $PP_PATH
# Create and configure a temporary keychain
security create-keychain -p "$KEYCHAIN_PASSWORD" $KEYCHAIN_PATH
security set-keychain-settings -lut 3600 $KEYCHAIN_PATH
security unlock-keychain -p "$KEYCHAIN_PASSWORD" $KEYCHAIN_PATH
security list-keychain -d user -s $KEYCHAIN_PATH
# Import the certificate into the keychain
security import $CERTIFICATE_PATH -P "$P12_PASSWORD" -A -t cert -f pkcs12 -k $KEYCHAIN_PATH
# Copy the provisioning profile to the expected location
mkdir -p ~/Library/MobileDevice/Provisioning\ Profiles
cp $PP_PATH ~/Library/MobileDevice/Provisioning\ Profiles
# Step 5: Update Info.plist Files with Secrets
- name: Append new key in Info.plist for Example
run: |
PLIST_PATH="Example/Kommunicate/Info.plist"
KEY="KOMMUNICATE_APP_ID"
VALUE="${{ secrets.KOMMUNICATE_APP_ID }}"
/usr/libexec/PlistBuddy -c "Set :$KEY $VALUE" "$PLIST_PATH" || \
/usr/libexec/PlistBuddy -c "Add :$KEY string $VALUE" "$PLIST_PATH"
cat "$PLIST_PATH"
- name: Append new key in Info.plist for Tests
run: |
PLIST_PATH="Example/Tests/Info.plist"
KEY="KOMMUNICATE_APP_ID"
VALUE="${{ secrets.KOMMUNICATE_APP_ID }}"
/usr/libexec/PlistBuddy -c "Set :$KEY $VALUE" "$PLIST_PATH" || \
/usr/libexec/PlistBuddy -c "Add :$KEY string $VALUE" "$PLIST_PATH"
cat "$PLIST_PATH"
- name: Append new key in Info.plist for UI Tests
run: |
PLIST_PATH="Example/Kommunicate_ExampleUITests/Info.plist"
KEY="KOMMUNICATE_APP_ID"
VALUE="${{ secrets.KOMMUNICATE_APP_ID }}"
/usr/libexec/PlistBuddy -c "Set :$KEY $VALUE" "$PLIST_PATH" || \
/usr/libexec/PlistBuddy -c "Add :$KEY string $VALUE" "$PLIST_PATH"
cat "$PLIST_PATH"
# Step 6: Run Tests with Keychain Management
- name: Run XCTest with Keychain
env:
KOMMUNICATE_APP_ID: ${{ secrets.KOMMUNICATE_APP_ID }}
KEYCHAIN_PATH: $RUNNER_TEMP/app-signing.keychain-db # Path to the keychain
KEYCHAIN_PASSWORD: ${{ secrets.KEYCHAIN_PASSWORD }} # Keychain password
TEAM_ID: ${{ secrets.TEAM_ID }} # Apple Developer Team ID
run: |
# Unlock the keychain for signing during tests
echo "Unlocking Keychain at $KEYCHAIN_PATH"
security unlock-keychain -p "$KEYCHAIN_PASSWORD" "$KEYCHAIN_PATH"
# Run the tests, ensuring code signing is handled
xcodebuild test \
-workspace Example/Kommunicate.xcworkspace \
-scheme Kommunicate_Example \
-destination 'platform=iOS Simulator,name=iPhone 16,OS=18.1' \
-enableCodeCoverage YES \
-derivedDataPath ./DerivedData \
CODE_SIGN_IDENTITY="iPhone Developer" \
CODE_SIGNING_ALLOWED=YES \
CODE_SIGNING_REQUIRED=YES \
DEVELOPMENT_TEAM="$TEAM_ID" \
KEYCHAIN="$KEYCHAIN_PATH"
# Step 7: Print Test Results
- name: Print Test Results
run: |
cat ./DerivedData/Logs/Test/*.xcresult/TestSummaries.plist || echo "No test results found."
# Step 8: Debug Environment Variables (Optional)
- name: Debug Environment Variables
run: |
echo "Environment variables set successfully."
# Step 9: Post Test Results as a PR Comment
- name: Post Test Results to PR
if: github.event_name == 'pull_request'
uses: actions/github-script@v6
with:
script: |
const fs = require('fs');
const path = './DerivedData/Logs/Test/*.xcresult/TestSummaries.plist';
let content = 'Test results not found.';
try {
content = fs.readFileSync(path, 'utf8');
} catch (err) {
console.log('Error reading test results:', err);
}
const comment = `
## iOS Automation Test Results
\`\`\`
${content.substring(0, 65000)} <!-- GitHub has a 65k char limit -->
\`\`\`
`;
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment,
});