How to increment build number of Flutter IOS app when deploying with CodeMagic
Asked Answered
D

7

11

I am unable to increment the build number of my Flutter app automatically when I deploy it using CodeMagic (https://codemagic.io/) which is owned by Nevercode.

I followed the steps described on this page: https://developer.nevercode.io/docs/incrementing-ios-app-version.

The script they suggest is this:

DSYM_INFO_PLIST="${DWARF_DSYM_FOLDER_PATH}/${DWARF_DSYM_FILE_NAME}/Contents/Info.plist"
buildNumber=$NEVERCODE_BUILD_NUMBER
stringLength=${#buildNumber}

if [ $stringLength -ne 0 ]; then
    echo "Updating build number to $buildNumber"
    /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
    if [ -f "$DSYM_INFO_PLIST" ]; then
        /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $BUILD_NUMBER" "$DSYM_INFO_PLIST"
    fi
else
    echo "Missing build number, skip updating"
fi

After I add this script in Xcode, I get this error:

Running pod install...                                             34.3s
Running Xcode build...
 ├─Assembling Flutter resources...                           6.1s
 └─Compiling, linking and signing...                         6.9s
Xcode build done.                                           30.3s
Failed to build iOS app
Error output from Xcode build:
↳
    ** BUILD FAILED **


Xcode's output:
↳
    === BUILD TARGET Runner OF PROJECT Runner WITH CONFIGURATION Debug ===
    /Users/macbook/Library/Developer/Xcode/DerivedData/Runner-hdgyskbygbvchfagqudvhwidlraa/Build/Intermediates.noindex/Runner.build/Debug-iphoneos/Runner.build/Script-3590602C2
    2484D000061C91A.sh: line 15: syntax error: unexpected end of file
    Command /bin/sh failed with exit code 2

Could not build the precompiled application for the device.

Could someone shed some light on how to properly increment the build number of an IOS app when deploying with Codemagic? Should it be a script run in Xcode's build phases or a command added into the build steps of Codemagic workflow?

Ideally, it should not increment whenever I run the app with flutter run but when I deploy it to App Store Connect.

Dolora answered 25/3, 2019 at 1:4 Comment(0)
P
8

From Codemagic documentation they show you a few options:

Here are some examples of the build arguments you can use to increment the app version. You can enter the build arguments in App settings > Build > Build arguments.

--build-name=2.0.$BUILD_NUMBER --build-number=$(($BUILD_NUMBER + 100))

--build-name=1.0.0 --build-number=$BUILD_NUMBER

--build-number=$(git rev-list HEAD --count)

Add it here:

enter image description here

Please note that the number of builds in BUILD_NUMBER is counted separately for each workflow.

Pazice answered 11/9, 2019 at 0:40 Comment(0)
E
4

This works now, i wrote to Codemagic.io. This is the response:

We provide $BUILD_NUMBER variable to use it for versioning. For instance you can use build arguments like --build-name="1.0.$(($BUILD_NUMBER + 100))" --build-number=$(($BUILD_NUMBER + 100))

Exemplary answered 27/3, 2019 at 17:9 Comment(0)
T
3

There are more options to use the $BUILD_NUMBER variable for build versioning. You can find some examples in Codemagic documentation: https://docs.codemagic.io/building/build-versioning/

Traweek answered 20/6, 2019 at 15:7 Comment(0)
T
2

I've faced the next issue with Codemagic: at the beginning in Info.plist were ${CURRENT_PROJECT_VERSION} for Bundle Version and ${MARKETING_VERSION} for Bundle Version string (short) and these lines above did not work

The place to put these lines in Codemagic

--build-name=2.0.$BUILD_NUMBER --build-number=$BUILD_NUMBER
--build-name=2.0.$PROJECT_BUILD_NUMBER --build-number=$PROJECT_BUILD_NUMBER

But after I changed to ${FLUTTER_BUILD_NUMBER} and ${FLUTTER_BUILD_NAME} for Bundle Version and Bundle Version string (short) respectively lines above in Codemagic started to do magic, they start to work.

Tetherball answered 1/9, 2021 at 4:26 Comment(2)
"changed to ${FLUTTER_BUILD_NUMBER} and ${FLUTTER_BUILD_NAME} for Bundle Version and Bundle Version string" - changed this where? info.plist?Sharmainesharman
Yes, info.plist. I however was getting undefined for my FLUTTER_BUILD_NUMBER/NAME. The fix was to check the configuration files in project > info > configurations for all build types had #include "Generated.xcconfig" at the top of the file. Mine were autogenerated podsrunner.release-flavor ones that I couldn't find and inspect so I created new files for each flavor, then added ``#include "Generated.xcconfig"` then add the autogenerated one (#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.release-qa.xcconfig") then add my flavor specific environment variables.Sharmainesharman
A
1

In my case, adding:

--build-name=1.0.0 --build-number=$(($BUILD_NUMBER + 100))

didn't affect the build number for my iOS builds, despite it working for Android.

For iOS builds, it was still using the build number from my xCode project.

What worked was to add a pre-build script which would bump the build version on Codemagic's VM:

#!/bin/sh
cd ios
agvtool new-version -all $(($BUILD_NUMBER + 100))

Basically, you'd be running xCode's agvtool, in the ios folder, to update the build number with the one supplied by Codemagic (+100, in my case!).

Adios answered 6/7, 2020 at 4:56 Comment(0)
O
1

Shareing this for those looking to grab the tag name from git and inspect it to get the major/minor/patch numbers.

Pre-build script this goes into the pre-build step before the build section.

Pre-build script
Run script before the build phase has started
#! /bin/bash
# https://jon.sprig.gs/blog/post/1175
# https://blog.codemagic.io/build-versioning-with-codemagic/

RE='[^0-9]*\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)\([0-9A-Za-z-]*\)'

base="$2"
if [ -z "$2" ]
then
  base=$(git tag 2>/dev/null| tail -n 1)
  if [ -z "$base" ]
  then
    base=0.0.0
  fi
fi

MAJOR=`echo $base | sed -e "s#$RE#\1#"`
MINOR=`echo $base | sed -e "s#$RE#\2#"`
PATCH=`echo $base | sed -e "s#$RE#\3#"`
BUILD_TOTAL=$(git rev-list HEAD --count)

GIT_BUILD_NUMBER=$BUILD_TOTAL
GIT_BUILD_TAG_VER="$MAJOR.$MINOR.$PATCH"
GIT_BUILD_VER_BUILD="$MAJOR.$MINOR.$PATCH+$BUILD_TOTAL"

# First Log the variables for the console. Then output them to the CM_ENV property so code magic can use
# them in the subsequent build steps.
echo "GIT_BUILD_TAG_VER=$GIT_BUILD_TAG_VER"
echo "GIT_BUILD_NUMBER=$GIT_BUILD_NUMBER"
echo "GIT_BUILD_VER_BUILD=$GIT_BUILD_VER_BUILD"

echo "input tag = $base"
echo "GIT_BUILD_TAG_VER=$GIT_BUILD_TAG_VER" >> $CM_ENV
echo "GIT_BUILD_NUMBER=$GIT_BUILD_NUMBER" >> $CM_ENV
echo "GIT_BUILD_VER_BUILD=$GIT_BUILD_VER_BUILD" >> $CM_ENV

echo "standard config is usual --build-name=1.0.\$BUILD_NUMBER --build-number=\$BUILD_NUMBER"
echo "config option A)  --build-name=\$GIT_BUILD_TAG_VER --build-number=\$GIT_BUILD_NUMBER"
echo "config option B)  --build-name=\$GIT_BUILD_VER_BUILD --build-number=\$GIT_BUILD_NUMBER"
echo "Option B may not be supported by Apple."
echo "Output in subsequent build step should look like  --build-name=$GIT_BUILD_TAG_VER --build-number=$GIT_BUILD_NUMBER"

# GIT_BUILD_TAG_VER=2.3.4
# GIT_BUILD_NUMBER=953
# GIT_BUILD_VER_BUILD=2.3.4+953
# input tag = v2.3.4-pre-release
# standard config is usual --build-name=1.0.$BUILD_NUMBER --build-number=$BUILD_NUMBER
# config option A)  --build-name=$GIT_BUILD_TAG_VER --build-number=$GIT_BUILD_NUMBER
# config option B)  --build-name=$GIT_BUILD_VER_BUILD --build-number=$GIT_BUILD_NUMBER
# Option B may not be supported by Apple.
# Output in subsequent build step should look like  --build-name=2.3.4 --build-number=953

Build Step Then in your Build section for android/ios build arguments you can add.

--flavor dev -t lib/main_dev.dart --build-name=$GIT_BUILD_TAG_VER --build-number=$GIT_BUILD_NUMBER
Octavia answered 13/4, 2022 at 15:53 Comment(0)
C
0

My solution was to set the MARKETING_VERSION (manually) and BUILD_NUMBER_VERSION (dynamically) in pre-build script.

Basically, the MARKETING_VERSION represents a specific version number of your iOS app, like 1.0.0 and BUILD_NUMBER_VERSION represents the number of a build of MARKETING_VERSION, like 1.0.0.20

I didn't find in any place how can i catch dynamically the actual MARKETING_VERSION in App Store Connect, so I infer that I need to manually set the version in accordance with the version that is in the process for to be publish in App Store.

I put this code in my pre-build script:

cd $FCI_BUILD_DIR/ios
agvtool new-marketing-version 1.0.0
agvtool new-version -all $(($BUILD_NUMBER))

Remember, the MARKETING_VERSION needs to be the same as the version that is in process in App Store Connect.

Caelum answered 23/12, 2020 at 18:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.