Section 8/151 menit

8. CI Caching untuk SPM

8. CI Caching untuk SPM

SPM menyimpan packages di dua lokasi:

swift
Lokasi 1: ~/Library/Developer/Xcode/DerivedData/<AppName>/SourcePackages/Khusus untuk project ini, dalam DerivedDataDibersihkan saat Clean Build Folder

Lokasi 2: ~/Library/Caches/org.swift.swiftpm/ (atau ~/.cache/org.swift.swiftpm/)
  → Global SPM cache, shared antar project
  → Lebih stabil untuk CI caching

GitHub Actions: SPM Cache

swift
# .github/workflows/ios-ci.yml

jobs:
  build-and-test:
    runs-on: macos-15
    
    steps:
      - uses: actions/checkout@v4
      
      # Cache SPM packages — key based on Package.resolved
      - name: Cache SPM Packages
        uses: actions/cache@v4
        with:
          path: |
            ~/Library/Developer/Xcode/DerivedData/MyApp-*/SourcePackages/checkouts
            ~/Library/Caches/org.swift.swiftpm
          key: spm-${{ runner.os }}-${{ hashFiles('**/Package.resolved') }}
          restore-keys: |
            spm-${{ runner.os }}-

      # Resolve packages (cepat jika cache hit)
      - name: Resolve SPM Packages
        run: |
          xcodebuild -resolvePackageDependencies \
            -project MyApp.xcodeproj \
            -scheme MyApp \
            -derivedDataPath DerivedData

      - name: Build & Test
        run: |
          xcodebuild test \
            -project MyApp.xcodeproj \
            -scheme MyApp \
            -derivedDataPath DerivedData \
            -destination "platform=iOS Simulator,name=iPhone 16 Pro" \
            | xcpretty

SPM Cache dengan DerivedData Pinned Location

swift
# Strategi yang lebih reliable: pin DerivedData ke lokasi tetap
# sehingga cache path konsisten antar run

jobs:
  build:
    runs-on: macos-15
    
    env:
      DERIVED_DATA_PATH: ${{ github.workspace }}/DerivedData
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Cache SPM
        uses: actions/cache@v4
        with:
          # Cache SourcePackages di dalam DerivedData yang dipin
          path: ${{ env.DERIVED_DATA_PATH }}/SourcePackages
          key: spm-${{ runner.os }}-${{ hashFiles('MyApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved') }}
          restore-keys: |
            spm-${{ runner.os }}-
      
      - name: Resolve Packages
        run: |
          xcodebuild -resolvePackageDependencies \
            -project MyApp.xcodeproj \
            -derivedDataPath "$DERIVED_DATA_PATH"
      
      - name: Test
        run: |
          xcodebuild test \
            -project MyApp.xcodeproj \
            -scheme MyApp \
            -derivedDataPath "$DERIVED_DATA_PATH" \
            -destination "platform=iOS Simulator,name=iPhone 16 Pro"

SPM untuk Swift Package (Bukan Xcode Project)

swift
# Jika package standalone (bukan dalam Xcode project)

jobs:
  test:
    runs-on: ubuntu-latest  # Atau macos-15 untuk iOS tests
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Cache SPM
        uses: actions/cache@v4
        with:
          path: .build
          key: spm-${{ runner.os }}-swift${{ matrix.swift }}-${{ hashFiles('Package.resolved') }}
          restore-keys: |
            spm-${{ runner.os }}-swift${{ matrix.swift }}-
            spm-${{ runner.os }}-
      
      - name: Resolve
        run: swift package resolve
      
      - name: Test
        run: swift test --parallel