From 7b11914d1a962ca6860eea32cce4d3be2a7101c2 Mon Sep 17 00:00:00 2001 From: Satyajit Sahoo Date: Tue, 4 Jul 2023 18:26:20 +0200 Subject: [PATCH] chore: check for turborepo cache before build --- .github/workflows/build-templates.yml | 44 +++++++++++++------ .../common/$.github/workflows/ci.yml | 42 +++++++++++++----- 2 files changed, 61 insertions(+), 25 deletions(-) diff --git a/.github/workflows/build-templates.yml b/.github/workflows/build-templates.yml index 04a567a31..aebb76d56 100644 --- a/.github/workflows/build-templates.yml +++ b/.github/workflows/build-templates.yml @@ -132,15 +132,6 @@ jobs: yarn install --cwd example yarn install - - name: Cache turborepo - uses: actions/cache@v3 - with: - path: | - ${{ env.work_dir }}/.turbo - key: ${{ runner.os }}-library-turborepo-${{ matrix.type }}-${{ matrix.language }}-${{ hashFiles(format('{0}/**/yarn.lock', env.work_dir)) }} - restore-keys: | - ${{ runner.os }}-library-turborepo-${{ matrix.type }}-${{ matrix.language }}- - - name: Get build target working-directory: ${{ env.work_dir }} run: | @@ -158,6 +149,31 @@ jobs: fi fi + - name: Cache turborepo + if: env.android_build == 1 || env.ios_build == 1 + uses: actions/cache@v3 + with: + path: | + ${{ env.work_dir }}/.turbo + key: ${{ runner.os }}-library-turborepo-${{ matrix.type }}-${{ matrix.language }}-${{ hashFiles(format('{0}/**/yarn.lock', env.work_dir)) }} + restore-keys: | + ${{ runner.os }}-library-turborepo-${{ matrix.type }}-${{ matrix.language }}- + + - name: Check turborepo cache + if: env.android_build == 1 || env.ios_build == 1 + working-directory: ${{ env.work_dir }} + run: | + TURBO_CACHE_STATUS_ANDROID=$(node -p "($(yarn --silent turbo run build:android --cache-dir=".turbo/cache" --dry=json)).tasks.find(t => t.task === 'build:android').cache.status") + TURBO_CACHE_STATUS_IOS=$(node -p "($(yarn --silent turbo run build:ios --cache-dir=".turbo/cache" --dry=json)).tasks.find(t => t.task === 'build:ios').cache.status") + + if [[ $TURBO_CACHE_STATUS_ANDROID == "HIT" ]]; then + echo "turbo_cache_hit_android=1" >> $GITHUB_ENV + fi + + if [[ $TURBO_CACHE_STATUS_IOS == "HIT" ]]; then + echo "turbo_cache_hit_ios=1" >> $GITHUB_ENV + fi + - name: Lint library working-directory: ${{ env.work_dir }} run: | @@ -185,19 +201,19 @@ jobs: yarn example expo export:web - name: Install JDK - if: env.android_build == 1 + if: env.android_build == 1 && env.turbo_cache_hit_android != 1 uses: actions/setup-java@v3 with: distribution: 'zulu' java-version: '11' - name: Finalize Android SDK - if: env.android_build == 1 + if: env.android_build == 1 && env.turbo_cache_hit_android != 1 run: | /bin/bash -c "yes | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager --licenses > /dev/null" - name: Cache Gradle - if: env.android_build == 1 + if: env.android_build == 1 && env.turbo_cache_hit_android != 1 uses: actions/cache@v3 with: path: | @@ -212,7 +228,7 @@ jobs: yarn turbo run build:android --cache-dir=".turbo/cache" - name: Cache cocoapods - if: env.ios_build == 1 + if: env.ios_build == 1 && env.turbo_cache_hit_ios != 1 id: library-cocoapods-cache uses: actions/cache@v3 with: @@ -225,7 +241,7 @@ jobs: ${{ runner.os }}-library-cocoapods- - name: Install cocoapods - if: steps.library-cocoapods-cache.outputs.cache-hit != 'true' && env.ios_build == 1 + if: env.ios_build == 1 && env.turbo_cache_hit_ios != 1 && steps.library-cocoapods-cache.outputs.cache-hit != 'true' working-directory: ${{ env.work_dir }} run: | yarn example pods diff --git a/packages/create-react-native-library/templates/common/$.github/workflows/ci.yml b/packages/create-react-native-library/templates/common/$.github/workflows/ci.yml index a5ae5ad85..808b44113 100644 --- a/packages/create-react-native-library/templates/common/$.github/workflows/ci.yml +++ b/packages/create-react-native-library/templates/common/$.github/workflows/ci.yml @@ -59,25 +59,36 @@ jobs: - name: Setup uses: ./.github/actions/setup + - name: Cache turborepo for Android + uses: actions/cache@v3 + with: + path: ${{ env.TURBO_CACHE_DIR }} + key: ${{ runner.os }}-turborepo-android-${{ hashFiles('**/yarn.lock') }} + restore-keys: | + ${{ runner.os }}-turborepo-android- + + - name: Check turborepo cache for Android + run: | + TURBO_CACHE_STATUS=$(node -p "($(yarn --silent turbo run build:android --cache-dir="${{ env.TURBO_CACHE_DIR }}" --dry=json)).tasks.find(t => t.task === 'build:android').cache.status") + + if [[ $TURBO_CACHE_STATUS == "HIT" ]]; then + echo "turbo_cache_hit=1" >> $GITHUB_ENV + fi + - name: Install JDK + if: env.turbo_cache_hit != 1 uses: actions/setup-java@v3 with: distribution: 'zulu' java-version: '11' - name: Finalize Android SDK + if: env.turbo_cache_hit != 1 run: | /bin/bash -c "yes | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager --licenses > /dev/null" - - name: Cache turborepo for Android - uses: actions/cache@v3 - with: - path: ${{ env.TURBO_CACHE_DIR }} - key: ${{ runner.os }}-turborepo-android-${{ hashFiles('**/yarn.lock') }} - restore-keys: | - ${{ runner.os }}-library-turborepo-android- - - name: Cache Gradle + if: env.turbo_cache_hit != 1 uses: actions/cache@v3 with: path: | @@ -106,9 +117,18 @@ jobs: path: ${{ env.TURBO_CACHE_DIR }} key: ${{ runner.os }}-turborepo-ios-${{ hashFiles('**/yarn.lock') }} restore-keys: | - ${{ runner.os }}-library-turborepo-ios- + ${{ runner.os }}-turborepo-ios- + + - name: Check turborepo cache for iOS + run: | + TURBO_CACHE_STATUS=$(node -p "($(yarn --silent turbo run build:ios --cache-dir="${{ env.TURBO_CACHE_DIR }}" --dry=json)).tasks.find(t => t.task === 'build:ios').cache.status") + + if [[ $TURBO_CACHE_STATUS == "HIT" ]]; then + echo "turbo_cache_hit=1" >> $GITHUB_ENV + fi - name: Cache cocoapods + if: env.turbo_cache_hit != 1 id: cocoapods-cache uses: actions/cache@v3 with: @@ -121,13 +141,13 @@ jobs: ${{ runner.os }}-cocoapods- - name: Install cocoapods - if: steps.cocoapods-cache.outputs.cache-hit != 'true' + if: env.turbo_cache_hit != 1 && steps.cocoapods-cache.outputs.cache-hit != 'true' run: | yarn example pods env: NO_FLIPPER: 1 - - name: Build example (iOS) + - name: Build example for iOS run: | yarn turbo run build:ios --cache-dir="${{ env.TURBO_CACHE_DIR }}" <% } else { -%>