Incrementing Cordova app version

To make your build version management easy, Nevercode exports two environment variables that you can use in your build scripts: NEVERCODE_BUILD and NEVERCODE_BUILD_NUMBER. Read more about their meaning from here.

Since the Cordova build ecosystem supports running arbitrary scripts as a part of your build process using hooks, you can check the existence and the value of these environment variables in either JavaScript or a scripting language of your choosing (such as Bash, Ruby or Python).

Build versioning in Cordova is as simple as defining a key in your configuration file config.xml.
To set Android's versionCode you'd have to define android-versionCode and for iOS' CFBundleVersion you'd have to define ios-CFBundleVersion. Read more about it from the official documentation.

Example

Here is an example of updating your build version using a simple python script.

#!/usr/bin/env python

import os
import sys
from xml.etree import ElementTree

repo_path = os.getenv('NEVERCODE_BUILD_DIR')
build_number = os.getenv('NEVERCODE_BUILD_NUMBER')

if not build_number:
    print "Missing build number, skip updating"
    sys.exit()

config_xml_path = os.path.join(repo_path, 'config.xml')

ElementTree.register_namespace('', 'http://www.w3.org/ns/widgets')
tree = ElementTree.parse(config_xml_path)
root = tree.getroot()

root.attrib['android-versionCode'] = build_number
root.attrib['ios-CFBundleVersion'] = build_number

tree.write(config_xml_path)

print "Updated versionCode and CFBundleVersion to %s" % build_number

Execute permission

In order for Nevercode to run the above script during the build, it must have execute permission. Don't forget to run chmod +x gh_update_cordova_version.py before committing and pushing your code.

To actually run the script you'd have to add the hook to your config.xml:

<widget id="com.example.app" version="1.2.3" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
  ...
  <hook type="before_prepare" src="gh_update_cordova_version.py" />
  ...
</widget>

Alternatively to hooks you can use custom build steps to bump the app version for CI builds.