Skip to content

Instantly share code, notes, and snippets.

@cristiangu
Last active March 29, 2025 13:33
Show Gist options
  • Save cristiangu/d8e24f88f580519238d681cb09def782 to your computer and use it in GitHub Desktop.
Save cristiangu/d8e24f88f580519238d681cb09def782 to your computer and use it in GitHub Desktop.
A set of CircleCI commands to use when recycling IPA/APK build for React Native
version: 2.1
commands:
restore-ios-ipa-cache:
parameters:
env:
type: enum
enum: ['staging', 'prod']
steps:
- restore_cache:
name: Restore cached iOS .ipa if exists
# Not including the commit id, this will fetch the latest cache for the given env
key: ios-ipa-<< parameters.env >>-v1
save-ios-ipa-cache:
parameters:
env:
type: enum
enum: ['staging', 'prod']
steps:
- save_cache:
name: Save iOS .ipa cache
# Including the commit hash in the cache key to ensure that the cache is invalidated on every build
key: ios-ipa-<< parameters.env >>-v1-<< pipeline.git.revision >>
paths:
- ./mobile_app.ipa
restore-android-apk-cache:
parameters:
env:
type: enum
enum: ['staging', 'prod']
steps:
- restore_cache:
name: Restore cached Android .apk if exists
# Not including the commit id, this will fetch the latest cache for the given env
key: android-apk-<< parameters.env >>-v1
save-android-apk-cache:
parameters:
env:
type: enum
enum: ['staging', 'prod']
steps:
- save_cache:
name: Save Android .apk cache
# Including the commit hash in the cache key to ensure that the cache is invalidated on every build
key: android-apk-<< parameters.env >>-v1-<< pipeline.git.revision >>
paths:
- ./android/app/build/outputs/apk/release/mobile_app.apk
read-app-version:
steps:
- run:
name: Read app version from package.json
command: echo "export APP_VERSION=$(jq -r '.version' package.json)" >> $BASH_ENV
is-native-build-required:
parameters:
platform:
type: enum
enum: ['android', 'ios']
steps:
- run:
name: Check if native builds are required
command: |
if [ << parameters.platform >> == "ios" ]; then
if [ -f ./${PRODUCT_NAME}.ipa ]; then
echo "Cached iOS .ipa found."
else
echo "No cached iOS .ipa found. Creating new a native iOS build."
echo "export NATIVE_IOS_BUILD_REQUIRED=true" >> "$BASH_ENV"
exit 0
fi
fi
if [ << parameters.platform >> == "android" ]; then
if [ -f ./android/app/build/outputs/apk/release/mobile_app.apk ]; then
echo "Cached Android .apk found."
else
echo "No cached Android .apk found. Creating new a native Android build."
echo "export NATIVE_ANDROID_BUILD_REQUIRED=true" >> "$BASH_ENV"
exit 0
fi
fi
commit_id_1="<< pipeline.git.base_revision >>"
commit_id_2="<<pipeline.git.revision>>"
echo "Base commit: $commit_id_1"
echo "Head commit: $commit_id_2"
if [ "$commit_id_1" == "$commit_id_2" ]; then
commit_id_1="$commit_id_2~1"
fi
if [ -z "$commit_id_1" ] || [ -z "$commit_id_2" ]; then
echo "No commit range detected. Creating new native builds for iOS and Android."
echo "export NATIVE_IOS_BUILD_REQUIRED=true" >> "$BASH_ENV"
echo "export NATIVE_ANDROID_BUILD_REQUIRED=true" >> "$BASH_ENV"
exit 0
fi
changed_files=$(git diff --name-only "$commit_id_1" "$commit_id_2")
echo "Changed files: $changed_files"
if echo "$changed_files" | grep -qE '^ios/'; then
echo "Changes detected in the /ios directory. New native iOS build will be created."
echo "export NATIVE_IOS_BUILD_REQUIRED=true" >> "$BASH_ENV"
else
echo "No changes detected in the /ios directory."
echo "export NATIVE_IOS_BUILD_REQUIRED=false" >> "$BASH_ENV"
fi
if echo "$changed_files" | grep -qE '^android/'; then
echo "Changes detected in the /android directory. New native Android build will be created."
echo "export NATIVE_ANDROID_BUILD_REQUIRED=true" >> "$BASH_ENV"
else
echo "No changes detected in the /android directory."
echo "export NATIVE_ANDROID_BUILD_REQUIRED=false" >> "$BASH_ENV"
fi
if echo "$changed_files" | grep -qE '^src/assets'; then
echo "Changes detected in the /src/assets directory. New native iOS and Android builds will be created."
echo "export NATIVE_IOS_BUILD_REQUIRED=true" >> "$BASH_ENV"
echo "export NATIVE_ANDROID_BUILD_REQUIRED=true" >> "$BASH_ENV"
fi
if echo "$changed_files" | grep -qE '^.env'; then
echo "Changes detected in one of the .env* files. New native iOS and Android builds will be created."
echo "export NATIVE_IOS_BUILD_REQUIRED=true" >> "$BASH_ENV"
echo "export NATIVE_ANDROID_BUILD_REQUIRED=true" >> "$BASH_ENV"
fi
if echo "$changed_files" | grep -qE '^patches'; then
echo "Changes detected in the /patches directory. New native iOS and Android builds will be created."
echo "export NATIVE_IOS_BUILD_REQUIRED=true" >> "$BASH_ENV"
echo "export NATIVE_ANDROID_BUILD_REQUIRED=true" >> "$BASH_ENV"
fi
# A new JS lib is added/changed and that lib might contain Android native code.
if echo "$changed_files" | grep -qE '^yarn.lock'; then
echo "Changes detected in the yarn.lock file. New native Android build will be created."
echo "export NATIVE_ANDROID_BUILD_REQUIRED=true" >> "$BASH_ENV"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment