Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version numbering to dev builds #402

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 88 additions & 72 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,99 +1,121 @@
version: 2
jobs:
build:

# Use a `references` section to DRY the config with YAML reference syntax.
# https://discuss.circleci.com/t/using-defaults-syntax-in-config-yaml-aka-yaml-anchors/16168/3
# For a working example see https://github.com/opendatakit/collect/blob/master/.circleci/config.yml
references:
build_config: &build_config
working_directory: ~/client
docker:
- image: circleci/android:api-23
- image: circleci/android:api-28
environment:
JVM_OPTS: -Xmx3200m
# See https://github.com/opendatakit/collect/blob/master/.circleci/config.yml#L86-L92 for -Xm[sx]
# Disable pre-dexing because we don't need incremental builds here and it just slows the process down.
GRADLE_OPTS: '-Dorg.gradle.jvmargs="-Xms128m -Xmx1024m -XX:+HeapDumpOnOutOfMemoryError" -DpreDexEnable=false'

cache_key: &cache_key
key: buendia-client-v1-{{ checksum "build.gradle" }}-{{ checksum "app/build.gradle" }}

restore_cache: &restore_cache
restore_cache:
<<: *cache_key

save_cache: &save_cache
save_cache:
<<: *cache_key
paths:
- ~/.gradle
- ~/.m2

init_client_libs: &init_client_libs
run:
name: Init client-libs submodule
command: git submodule update --init client-libs

get_package_version: &get_package_version
run:
name: Get package version
command: echo "export PACKAGE_VERSION=$(.circleci/get_package_version)" >> $BASH_ENV

get_dependencies: &get_dependencies
run:
name: Download dependencies
command: ./gradlew androidDependencies

run_unit_tests: &run_unit_tests
run:
name: Run unit tests
# lint is slightly busted for us here
# command: ./gradlew lint test
command: ./gradlew test

make_artifact_store: &make_artifact_store
run:
name: Make artifact store
command: mkdir -p /tmp/artifacts

store_artifacts: &store_artifacts
store_artifacts: # for display in Artifacts: https://circleci.com/docs/2.0/artifacts/
path: /tmp/artifacts
destination: artifacts

jobs:
build:
<<: *build_config
steps:
- checkout

- run:
name: Init client-lib submodule
command: git submodule update --init client-libs
- *init_client_libs

- run:
name: Get package version
command: echo "export PACKAGE_VERSION=$(.circleci/get_package_version)" >> $BASH_ENV

- restore_cache:
keys:
- buendia-client-v1-{{ checksum "build.gradle" }}-{{ checksum "app/build.gradle" }}
- buendia-client-v1-{{ checksum "build.gradle" }}-
- buendia-client-v1-{{ checksum "build.gradle" }}-
- run:
name: Download Dependencies
command: ./gradlew androidDependencies
- *get_package_version

- *restore_cache

- *get_dependencies

- *save_cache

- save_cache:
paths:
- ~/.gradle
key: buendia-client-v1-{{ checksum "build.gradle" }}-{{ checksum "app/build.gradle" }}
- *make_artifact_store

- run:
name: Build APK
name: Build debug APK
command: |
export ANDROID_PATH=/opt/android/sdk
./gradlew assembleDebug
mkdir -p /tmp/artifacts
./gradlew -PversionNumber=${PACKAGE_VERSION} assembleDebug
cp app/build/outputs/apk/debug/app-debug.apk /tmp/artifacts/buendia-client-${PACKAGE_VERSION}-debug.apk

- run:
name: Run Tests
# lint is slightly busted for us here
# command: ./gradlew lint test
command: ./gradlew test
- *run_unit_tests

- store_artifacts: # for display in Artifacts: https://circleci.com/docs/2.0/artifacts/
path: /tmp/artifacts
destination: artifacts
- *store_artifacts

#- store_test_results: # for display in Test Summary: https://circleci.com/docs/2.0/collect-test-data/
#path: app/build/test-results

release:
working_directory: ~/client
docker:
- image: circleci/android:api-23
environment:
JVM_OPTS: -Xmx3200m
<<: *build_config
steps:
- checkout

- run:
name: Init client-lib submodule
command: git submodule update --init client-libs
- *init_client_libs

- run:
name: Get package version
command: echo "export PACKAGE_VERSION=$(.circleci/get_package_version)" >> $BASH_ENV

- restore_cache:
keys:
- buendia-client-v1-{{ checksum "build.gradle" }}-{{ checksum "app/build.gradle" }}
- buendia-client-v1-{{ checksum "build.gradle" }}-
- buendia-client-v1-{{ checksum "build.gradle" }}-
- run:
name: Download Dependencies
command: ./gradlew androidDependencies
- *get_package_version

- *restore_cache

- *get_dependencies

- save_cache:
paths:
- ~/.gradle
key: buendia-client-v1-{{ checksum "build.gradle" }}-{{ checksum "app/build.gradle" }}
- *save_cache

- *make_artifact_store

- run:
name: Save keystore
name: Generate keystore
command: |
echo "$ANDROID_KEYSTORE" | base64 -d > $ANDROID_KEYSTORE_FILE

- run:
name: Build release APK
command: |
export ANDROID_PATH=/opt/android/sdk
./gradlew -PversionNumber=${PACKAGE_VERSION} --no-daemon assembleRelease
mkdir -p /tmp/artifacts
./gradlew -PversionNumber=${PACKAGE_VERSION} assembleRelease
cp app/build/outputs/apk/release/app-release.apk /tmp/artifacts/buendia-client-${PACKAGE_VERSION}.apk

- run:
Expand All @@ -102,15 +124,9 @@ jobs:
keytool -list -printcert -jarfile /tmp/artifacts/buendia-client-${PACKAGE_VERSION}.apk | tee /tmp/cert.txt
grep -q Buendia /tmp/cert.txt

- run:
name: Run Tests
# lint is slightly busted for us here
# command: ./gradlew lint test
command: ./gradlew test

- store_artifacts: # for display in Artifacts: https://circleci.com/docs/2.0/artifacts/
path: /tmp/artifacts
destination: artifacts
- *run_unit_tests

- *store_artifacts

workflows:
version: 2
Expand Down
15 changes: 8 additions & 7 deletions .circleci/get_package_version
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ if echo "$CIRCLE_TAG" | grep -Eq '^v([0-9]+\.?)+$'; then
fi

# Find the most recent release tag on master.
TAG=$(git describe --match='v*' --abbrev=0 origin/master)
TAG=$(git describe --tags --match='v*' --abbrev=0 origin/master)

# If we're building in CircleCI, the version is "x.y.z+b<n>" where <n> is the
# If we're building in CircleCI, the version is "x.y.<b>" where <b> is the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why omit z here?

Copy link
Member Author

@schuyler schuyler Jul 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our app/build.gradle expects versionNumber to be in the form <major>.<minor>.<patch>. If we want the build number to appear in both the versionString and versionCode, it has to go somewhere, and I figured replacing the <patch> number of the latest release was the most innocuous way to do that.

The alternative would be to continue to append the build number to the original versionNumber but then (a.) build.gradle would need to be updated to permit that and (b.) the versionCode wouldn't change between dev builds. (Maybe that's actually what we want?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First, just to be clear on terminology, the fields are named versionName and versionCode.

The only practical effect of the versionCode is to prevent unintended downgrades. The Play Store will only perform automatic updates that increase the versionCode. As far as I know, only the Play Store enforces this; the phone doesn't care whether the vesionCode increases or not. Because we don't plan to ever publish development APKs to the Play Store, in some sense it doesn't matter what we use for dev builds.

Any of these choices are fine:

  • Set versionCode to 0 for all dev builds
  • When versionName is x.y.z, set versionCode to 1000000x + 10000y + z for all dev builds
  • When versionName is x.y.z at patchlevel p, set versionCode to 1000000x + 10000y + 100z + p for all dev builds
  • When versionName is x.y.z, set versionCode to 1000000x + 10000y + p for all dev builds

The first is the simplest (it's what build.gradle currently does, and I think it will work fine—the net effect is that any production release will supersede (automatically install over) any development release.

# latest build number.
if [ -n "$CIRCLE_BUILD_NUM" ]; then
echo "${TAG#v}+b${CIRCLE_BUILD_NUM}"
IFS='.' read -ra TAG_VERSION <<< "${TAG#v}"
echo "${TAG_VERSION[0]}.${TAG_VERSION[1]}.${CIRCLE_BUILD_NUM}"
exit
fi

# Otherwise, we're building locally, so the version should be "x.y.z~g<commit>"
# where <commit> is the latest unambiguous commit identifier. This version is
# actually treated by Debian as _less_ recent than "x.y.z".
# Otherwise, we're building locally, so the version should be "x.y.z-<commit>"
# where <commit> is the latest unambiguous commit identifier. "+dirty" is
# appended if the current tree is dirty.
COMMIT=$(git describe --abbrev --dirty=+dirty)
echo "${TAG#v}~${COMMIT#*g}"
echo "${TAG#v}-${COMMIT#*g}"