1

I'm trying out Swift for backend development (no use of Xcode or Apple platforms).

In my CI/CD workflow, which uses GitHub Actions, I have implemented a step to cache the build and use it accordingly.

      - name: Set up Swift ${{ env.SWIFT_VERSION }}
        uses: fwal/setup-swift@v1
        with:
          swift-version: ${{ env.SWIFT_VERSION }}
      
      - name: Load Swift Build cache
        uses: actions/cache@v3
        id: cached-build
        with:
          path: .build
          key: swift-${{ runner.os }}-${{ env.SWIFT_VERSION }}-${{ hashFiles('**/Package.resolved') }}
          restore-keys: |
            swift-${{ runner.os }}-${{ env.SWIFT_VERSION }}-
            swift-${{ runner.os }}-
            swift-

      - name: Build Application
        if: steps.cached-build.outputs.cache-hit != 'true'
        id: build
        run: swift build

      - name: Run tests
        id: tests
        run: swift test

While the workflow runs, GitHub retrieves the cached build without any problem. The problem is that the test step now starts by building the entire application and all of its dependencies and only then starts to run the TestCases (written with XCTest).

Before I added the caching logic, the workflow would build the application as usual, and then the test step would start right away with the TestCases - the test step took ~10sec, and now it takes ~2:30min.

Help is very much appreciated why this happens.

colin
  • 761
  • 1
  • 11
  • 24
  • Maybe, the `swift test` command builds the source code again before running tests. This (https://stackoverflow.com/questions/72534364/fastlane-run-ios-unit-test-without-build) seems relevant with `test_without_building: true`. If there's some command line argument, you could use that if the cache is hit, let it build otherwise. – Azeem Feb 11 '23 at 06:37
  • I have the same issue when using docker. I make my build in two steps in my Dockerfile: first compile the dependencies only w/ a dummy app, then compile the rest. Sometimes, but not always, Swift does not reuse the already built objects in the first step and starts from scratch. Sometimes it does not… It’s possibly due to the file date checks being done to the nanoseconds: https://stackoverflow.com/q/76138567 – Frizlab Aug 06 '23 at 17:51

0 Answers0