Skip to main content

Auto-incrementing Build Numbers in Capacitor

Appflow provides an auto-incrementing environment variable (CI_BUILD_NUMBER) that can be used with Trapeze to increment build numbers for the native builds of Capacitor apps.

note

An app demonstrating this feature can be found here.

To use this feature, first install @trapezedev/configure project into your app:

npm install @trapezedev/configure
note

Platform flags (--ios and --android) are only available in Trapeze v7.0.7+.

Next, create a simple appflow.yml file with the following contents:

vars:
CI_BUILD_NUMBER:
default: 1

platforms:
ios:
buildNumber: $CI_BUILD_NUMBER
android:
versionCode: $CI_BUILD_NUMBER

Finally, add the appflow:build script to your npm scripts in package.json to run this configuration during an Appflow build:

  "scripts": {
"appflow:build": "if [ \"$CI_PLATFORM\" != \"web\" ]; then npx trapeze run appflow.yml -y --$CI_PLATFORM; fi && npm run build",
},

The Appflow-provided $CI_PLATFORM environment variable dynamically resolves to the platform name of the currently running build (either ios or android). The --ios and --android flags tell Trapeze to run the specified platform only. Since Appflow only builds one platform at a time, combining these features effectively allows us to use one Trapeze script containing both iOS and Android commands in Appflow.

Make sure to keep your build command as the second part of the appflow:build script to ensure your app web assets build properly.

Setting Initial Build Number

If you'd like to start incrementing at a certain number, use the following appflow:build command instead, substituting your initial build number for the number 1234. All subsequent build numbers will be added to this number:

  "scripts": {
"appflow:build": "CI_BUILD_NUMBER=$(($CI_BUILD_NUMBER + 1234)); if [ \"$CI_PLATFORM\" != \"web\" ]; then npx trapeze run appflow.yml -y --$CI_PLATFORM; fi && npm run build"
},

Learn more

Trapeze has a number of additional features that may be useful in your build pipeline. Explore the ionic-team/trapeze repo to learn more.