Browse Source

Add caching and GitHub Actions optimizations to unit test workflow

Added cache restore and save steps for Xcode DerivedData and .build to improve CI performance.

Limited workflow triggers to relevant file changes using paths-ignore for PRs and pushes.

Narrowed PR events to opened and synchronize only.

Added steps to display and check cache contents before and after the build.

Included a check for uncommitted changes with GitHub warnings for visibility.

Enabled optional parallel test execution using ENABLE_PARALLEL_TESTING flag.
bjornoleh 11 months ago
parent
commit
7afc7300dc
1 changed files with 72 additions and 5 deletions
  1. 72 5
      .github/workflows/unit_tests.yml

+ 72 - 5
.github/workflows/unit_tests.yml

@@ -1,15 +1,26 @@
 name: zzz [DO NOT RUN] Automated unit tests
 
 on:
-  workflow_dispatch: # TODO: remove this after testing
+  workflow_dispatch: # TODO: remove this after testing?
+  
   pull_request:
     branches:
       - dev
-      - main
+    types: [opened, synchronize]
+    paths-ignore:
+      - '**.md'
+      - '**/README'
+      - '**.yml'
+      - '**.txt'
+
   push:
     branches:
       - dev
-      - main
+    paths-ignore:
+      - '**.md'
+      - '**/README'
+      - '**.yml'
+      - '**.txt'
 
 jobs:
   test:
@@ -27,6 +38,25 @@ jobs:
           fetch-depth: 1
           submodules: recursive
 
+      - name: Restore cache
+        id: cache-restore
+        uses: actions/cache/restore@v4
+        with:
+          path: |
+            /Users/runner/Library/Developer/Xcode/DerivedData
+            .build
+          key: ${{ runner.os }}-trio-${{ hashFiles('**/*.swift', '**/*.xcodeproj', '**/*.xcworkspace') }}
+          restore-keys: |
+            ${{ runner.os }}-trio-
+
+      - name: Show cache contents before build
+        run: |
+          echo "📂 Contents of DerivedData:"
+          ls -lah /Users/runner/Library/Developer/Xcode/DerivedData || echo "Directory not found"
+          echo ""
+          echo "📂 Contents of .build:"
+          ls -lah .build || echo ".build directory not found"
+
       - name: Build for testing
         run: |
           set -o pipefail && \
@@ -34,6 +64,42 @@ jobs:
             -workspace Trio.xcworkspace \
             -scheme "Trio Tests" \
             -destination 'platform=iOS Simulator,name=iPhone 16,OS=18.4' \
+
+      - name: Check for uncommitted changes
+        run: |
+          CHANGES=$(git status --porcelain)
+          if [ -n "$CHANGES" ]; then
+            echo "Uncommitted changes detected:"
+            echo "$CHANGES"
+            echo "$CHANGES" | while read -r line; do
+              FILE=$(echo $line | cut -c4-)
+              echo "::warning file=$FILE::Uncommitted change detected"
+            done
+            exit 0
+          else
+            echo "No uncommitted changes detected."
+          fi
+        shell: bash
+
+      - name: Show cache contents after build
+        run: |
+          echo "📂 Updated DerivedData contents:"
+          du -sh /Users/runner/Library/Developer/Xcode/DerivedData || echo "Directory not found"
+          ls -lah /Users/runner/Library/Developer/Xcode/DerivedData || echo "Directory not found"
+          echo ""
+          echo "📂 Updated .build contents:"
+          du -sh .build || echo ".build directory not found"
+          ls -lah .build || echo ".build directory not found"
+          
+      - name: Save cache
+        if: steps.cache-restore.outputs.cache-hit != 'true'
+        uses: actions/cache/save@v4
+        with:
+          path: |
+            /Users/runner/Library/Developer/Xcode/DerivedData
+            .build
+          key: ${{ runner.os }}-trio-${{ hashFiles('**/*.swift', '**/*.xcodeproj', '**/*.xcworkspace') }}  
+
       - name: Run tests
         run: |
           set -o pipefail
@@ -41,7 +107,8 @@ jobs:
             -workspace Trio.xcworkspace \
             -scheme "Trio Tests" \
             -destination 'platform=iOS Simulator,name=iPhone 16,OS=18.4' \
-          2>&1 | tee xcodebuild.log
+            $([ "$ENABLE_PARALLEL_TESTING" = "true" ] && echo "-parallel-testing-enabled YES") \
+            2>&1 | tee xcodebuild.log
 
       - name: Annotate test results
         if: always()
@@ -64,4 +131,4 @@ jobs:
             fi
           else
             echo "::warning::Test log (xcodebuild.log) not found"
-          fi
+          fi