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

Added functionality to generate APK for a specified date. #4156

Merged
merged 9 commits into from
Jan 6, 2025
Merged
50 changes: 0 additions & 50 deletions .github/workflows/dummy_bundle.yml

This file was deleted.

71 changes: 71 additions & 0 deletions .github/workflows/dummy_bundle_and_apk.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: Generate dummy bundle and APK

# This workflow will trigger when the 'dummy_bundle_and_apk' is pushed.
on:
push:
tags:
- 'dummy_bundle_and_apk' # dummy_bundle_and_apk Tag.

jobs:
publish_dummy_bundle_and_apk:
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: 17
distribution: temurin

- name: Preparing signing material
env:
KEYSTORE: ${{ secrets.keystore }}
run: |
echo "$KEYSTORE" | base64 -d > kiwix-android.keystore

- name: Retrieve date from git revision
id: git_date
run: |
DATE=$(git log -1 --format=%cd --date=format:%Y-%m-%d || echo "NoCommits")
if [ "$DATE" = "NoCommits" ]; then
RELEASE_DATE=""
else
RELEASE_DATE="$DATE"
fi
echo "KIWIX_ANDROID_RELEASE_DATE=$RELEASE_DATE" >> $GITHUB_ENV
shell: bash

- name: Generate dummy Bundle and APKs
env:
KEY_ALIAS: ${{ secrets.KEY_ALIAS }}
KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}
KEY_STORE_PASSWORD: ${{ secrets.KEY_STORE_PASSWORD }}
KIWIX_ANDROID_RELEASE_DATE: ${{ env.KIWIX_ANDROID_RELEASE_DATE }}
run: |
./gradlew bundlePlayStore assembleRelease --scan


- name: Get Bundle and APKs name and path
id: get-bundle-and-apk-paths
run: |
BUNDLE_PATH="app/build/outputs/bundle/playStore/kiwix-playStore.aab"
BUNDLE_NAME="PlayStoreDummyBundle.aab"
echo "bundle_path=$BUNDLE_PATH" >> $GITHUB_ENV
echo "bundle_name=$BUNDLE_NAME" >> $GITHUB_ENV
APK_DIR="app/build/outputs/apk/release/"
echo "apk_dir=$APK_DIR" >> $GITHUB_ENV

- name: Upload Bundle as an artifact
uses: actions/upload-artifact@v4
with:
name: ${{ env.bundle_name }}
path: ${{ env.bundle_path }}

- name: Upload All Release APKs as artifacts
uses: actions/upload-artifact@v4
with:
name: ReleaseApks
path: ${{ env.apk_dir }}*.apk

14 changes: 14 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ jobs:
- name: Set tag variable
run: echo "TAG=$(echo ${GITHUB_REF:10})" >> $GITHUB_ENV

- name: Retrieve date from git revision
id: git_date
kelson42 marked this conversation as resolved.
Show resolved Hide resolved
run: |
DATE=$(git log -1 --format=%cd --date=format:%Y-%m-%d || echo "NoCommits")
if [ "$DATE" = "NoCommits" ]; then
Copy link
Collaborator

Choose a reason for hiding this comment

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

Which scenario should here lead to "NoCommits" ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This scenario can only occur when no commit is available in the concerned branch; I had added this just for the fallback mechanism. But it is unnecessary since this workflow always runs on the main branch and always has the commits.

RELEASE_DATE=""
else
RELEASE_DATE="$DATE"
fi
echo "KIWIX_ANDROID_RELEASE_DATE=$RELEASE_DATE" >> $GITHUB_ENV
kelson42 marked this conversation as resolved.
Show resolved Hide resolved
shell: bash

- name: Retrieve secrets to files
env:
KEYSTORE: ${{ secrets.keystore }}
Expand All @@ -37,6 +49,7 @@ jobs:
KEY_STORE_PASSWORD: ${{ secrets.KEY_STORE_PASSWORD }}
UNIVERSAL_RELEASE_APK: app/build/outputs/apk/standalone/*universal*.apk
ARCHIVE_NAME: kiwix-${{ github.event.release.tag_name }}.apk
KIWIX_ANDROID_RELEASE_DATE: ${{ env.KIWIX_ANDROID_RELEASE_DATE }}
run: |
./gradlew assembleStandalone
cp ${UNIVERSAL_RELEASE_APK} ${ARCHIVE_NAME}
Expand Down Expand Up @@ -64,6 +77,7 @@ jobs:
KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}
KEY_STORE_PASSWORD: ${{ secrets.KEY_STORE_PASSWORD }}
PLAYSTORE_JSON: ${{ secrets.PLAYSTORE_JSON }}
KIWIX_ANDROID_RELEASE_DATE: ${{ env.KIWIX_ANDROID_RELEASE_DATE }}
run: |
echo "$PLAYSTORE_JSON" > playstore.json
./gradlew publishPlayStoreBundle --scan
30 changes: 28 additions & 2 deletions .github/workflows/testing_release.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Publish App to Play Store

# Trigger the workflow on a schedule (every Monday at 12:00 UTC)
# This workflow is triggered on a schedule or when specific tags are pushed.
# It runs every Monday at 12:00 UTC and also when the 'internal_testing' tag is pushed.
on:
schedule:
- cron: '0 12 * * 1' # Runs every Monday at 12:00
Expand Down Expand Up @@ -29,10 +30,35 @@ jobs:
echo "$KEYSTORE" | base64 -d > kiwix-android.keystore
echo "$PLAYSTORE_JSON" > playstore.json

- name: Retrieve date from git revision
id: git_date
run: |
DATE=$(git log -1 --format=%cd --date=format:%Y-%m-%d || echo "NoCommits")
if [ "$DATE" = "NoCommits" ]; then
RELEASE_DATE=""
else
RELEASE_DATE="$DATE"
fi
echo "KIWIX_ANDROID_RELEASE_DATE=$RELEASE_DATE" >> $GITHUB_ENV
shell: bash

- name: Publish bundle in internal testing on Google Play
env:
KEY_ALIAS: ${{ secrets.KEY_ALIAS }}
KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}
KEY_STORE_PASSWORD: ${{ secrets.KEY_STORE_PASSWORD }}
KIWIX_ANDROID_RELEASE_DATE: ${{ env.KIWIX_ANDROID_RELEASE_DATE }}
run: |
./gradlew publishPlayStoreBundle --scan
OUTPUT=$(./gradlew publishPlayStoreBundle --scan 2>&1)
echo "$OUTPUT" > gradle_output.log
if echo "$OUTPUT" | grep -q "BUILD SUCCESSFUL"; then
echo "$OUTPUT"
exit 0
fi

if echo "$OUTPUT" | grep -q "Try another version code."; then
echo "Upload skipped because very same version. $OUTPUT"
exit 0
fi
echo "$OUTPUT"
exit 1
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ want to build the `app` module in the `debug` configuration. If you
are interested in our custom apps, they have their own repo
[kiwix-android-custom](https://github.com/kiwix/kiwix-android-custom).

## Release

We have an [automatic version code generation](https://github.com/kiwix/kiwix-android/blob/main/buildSrc/src/main/kotlin/VersionCodeGenerator.kt) system based on the current date. However, you
can override this by setting the environment variable `KIWIX_ANDROID_RELEASE_DATE` to a specific
date in the `YYYY-MM-DD` format. This will use the provided date for the version code calculation
instead of the current date.

## Libraries Used

- [Libkiwix](https://github.com/kiwix/java-libkiwix) - Kotlin/Java binding for the core Kiwix
Expand Down
42 changes: 28 additions & 14 deletions buildSrc/src/main/kotlin/VersionCodeGenerator.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import java.time.LocalDate
import java.time.temporal.ChronoUnit

/*
* Kiwix Android
* Copyright (c) 2024 Kiwix <android.kiwix.org>
Expand All @@ -19,20 +16,37 @@ import java.time.temporal.ChronoUnit
*
*/

fun String.getVersionCode(): Int {
// the date when the automatic version code generation started
val lastDate = LocalDate.of(2024, 7, 17)
import java.time.LocalDate
import java.time.temporal.ChronoUnit

// Calculate the number of days between the lastDate and today's date.
// This gives us the total number of days since the last version code was set.
val daysDifference = ChronoUnit.DAYS.between(lastDate, LocalDate.now()).toInt()
/**
* The date when the automatic version code generation started.
*/
const val LAST_DATE = "2024-07-17"

/**
* Base version code. This is the version code of the last release uploaded to the Play Store.
* We use this as the starting point for generating new version codes automatically.
*/
const val BASE_VERSION_CODE = 231101

// Base version code. This is the version code of the last release uploaded to the Play Store.
// We use this as the starting point for generating new version codes automatically.
val baseVersionCode = 231101
fun String.getVersionCode(): Int {
// Get the current date. If the "KIWIX_ANDROID_RELEASE_DATE" environment
// variable is set(in YYYY-MM-DD format).
// It uses the specified date to generate the APK version code.
// Otherwise, it generates the version code based on the current date.
// See https://github.com/kiwix/kiwix-android/issues/4120 for more details.
val currentDate = if (!System.getenv("KIWIX_ANDROID_RELEASE_DATE").isNullOrEmpty()) {
LocalDate.parse(System.getenv("KIWIX_ANDROID_RELEASE_DATE"))
} else {
LocalDate.now()
}
kelson42 marked this conversation as resolved.
Show resolved Hide resolved
// Calculate the number of days between the LAST_DATE and today's date.
// This gives us the total number of days since the last version code was set.
val daysDifference = ChronoUnit.DAYS.between(LocalDate.parse(LAST_DATE), currentDate).toInt()

// Generate and return the new version code.
// The new version code is calculated by adding the number of days since lastDate
// The new version code is calculated by adding the number of days since LAST_DATE
// to the base version code. This creates a unique version code for each day.
return baseVersionCode + daysDifference
return BASE_VERSION_CODE + daysDifference
}
Loading