Skip to content

Commit

Permalink
Android Release Automation Workflow (#33)
Browse files Browse the repository at this point in the history
* Setup signingConfigs with my keystore file.

* Create release.yml script that will build apk file and do release everytime that is a push on the main branch.

* Fix gradlew no permission error.

* Use java 17 on script.

* Use variables from github secrets.

* decode google-services.json file

* List output apk-s. and also upload the apk regardless it's name

* add signing config on release build type.

* update script to decode keystore file.

* add logging for keystore existence

* Add script to automatically release the app on github. Also add latest git commit hash to apk file name.

* replace ghaction-create-release with actions/create-release

* use gh to create release.

* Trigger when new pushes are on appRelease branch.

* parse json to get upload_url

* instead of using --json to parse upload url. Make directly an api call.

* Use personal github token.

* define a valid tag name.

* use github cli "gh" to upload release.

* remove print upload url.

* Trigger GitHub Actions

* remove upload of apk when i create the release

* Add more descriptive naming on steps.

* Fetch latest commits as notes for release.

* remove APK_PATH and APK_NAME_BASE and replace usage directly with respective values

* Create new TAG for each release to get release notes (commits) from latest release to current release.

* Use PERSONAL_GITHUB_TOKEN to give permission when pushing new tag.

* Clone repo with PERSONAL_GITHUB_TOKEN.

* Push NEW_TAG with PERSONAL_GITHUB_TOKEN Access.

* Handle when there is the first release.

* Use keystore properties from github when build is from CI. And load locally when build is not CI.

* Change target branch.

---------

Co-authored-by: Diar Gegaj <[email protected]>
  • Loading branch information
diargegaj and Diar Gegaj authored Oct 12, 2023
1 parent 26638c1 commit b7285c0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 31 deletions.
66 changes: 39 additions & 27 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,62 +12,74 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
token: ${{ secrets.PERSONAL_GITHUB_TOKEN }}

- name: Set up JDK 17
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: '17'

- name: Grant execute permission for gradlew
- name: Grant execute permission for Gradle Wrapper
run: chmod +x gradlew

- name: Decode google-services.json
- name: Decode and store Google services configuration
run: echo "${{ secrets.GOOGLE_SERVICES_JSON }}" | base64 -d > app/google-services.json

- name: Decode Keystore
- name: Decode and store keystore
run: echo "${{ secrets.KEYSTORE_PATH }}" | base64 -d > app/keystore.jks

- name: Check Keystore Existence
- name: Verify keystore existence
run: ls -al ${{ github.workspace }}/app/

- name: Get the version (commit SHA)
id: get_version
run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"

- name: Build and release APK
- name: Build the release APK
run: ./gradlew assembleRelease
env:
KEYSTORE_PATH: ${{ github.workspace }}/app/keystore.jks
STORE_PASSWORD: ${{ secrets.STORE_PASSWORD }}
KEY_ALIAS: ${{ secrets.KEY_ALIAS }}
KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}

- name: Rename APK
run: mv ${{ github.workspace }}/app/build/outputs/apk/release/app-release.apk ${{ github.workspace }}/app/build/outputs/apk/release/app-release-${{ steps.get_version.outputs.sha_short }}.apk
- name: Copy and rename APK for release
run: cp ./app/build/outputs/apk/release/app-release.apk ./app/build/outputs/apk/release/app-release-${{ steps.get_version.outputs.sha_short }}.apk

- name: List directory contents
run: ls -al ${{ github.workspace }}/app/build/outputs/apk/release/
- name: Display directory contents
run: ls -al ./app/build/outputs/apk/release/

- name: Create Release
- name: Create GitHub release
id: create_release
uses: actions/create-release@v1
with:
tag_name: ${{ github.sha }}
release_name: Release ${{ github.sha }}
draft: false
prerelease: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
NEW_TAG="release-${{ steps.get_version.outputs.sha_short }}"
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "initial")
git tag $NEW_TAG
git push https://${{ secrets.PERSONAL_GITHUB_TOKEN }}@github.com/${{ github.repository }}.git $NEW_TAG
- name: Upload Release Asset
id: upload_release_asset
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ${{ github.workspace }}/app/build/outputs/apk/release/app-release-${{ steps.get_version.outputs.sha_short }}.apk
asset_name: app-release-${{ steps.get_version.outputs.sha_short }}.apk
asset_content_type: application/vnd.android.package-archive
# Check if it's the initial release
if [ "$LATEST_TAG" = "initial" ]; then
RELEASE_NOTES=$(git log --oneline)
else
RELEASE_NOTES=$(git log ${LATEST_TAG}..HEAD --oneline)
fi
gh release create $NEW_TAG \
--title "Release for commit $NEW_TAG" \
--notes "$RELEASE_NOTES" \
--repo ${{ github.repository }}
UPLOAD_URL=$(gh api repos/${{ github.repository }}/releases/tags/$NEW_TAG --jq '.upload_url')
echo "::set-output name=upload_url::$UPLOAD_URL"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.PERSONAL_GITHUB_TOKEN }}


- name: Upload APK asset to GitHub release
run: |
gh release upload release-${{ steps.get_version.outputs.sha_short }} \
./app/build/outputs/apk/release/app-release-${{ steps.get_version.outputs.sha_short }}.apk
env:
GITHUB_TOKEN: ${{ secrets.PERSONAL_GITHUB_TOKEN }}
25 changes: 21 additions & 4 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import java.io.FileInputStream
import java.util.Properties

plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
Expand All @@ -6,16 +9,30 @@ plugins {
id("com.google.gms.google-services")
}

val keystoreProperties = Properties()
val keystoreFile = rootProject.file("keystore.properties")

if (keystoreFile.exists()) {
keystoreProperties.load(FileInputStream(keystoreFile))
}

android {
namespace = "com.diargegaj.recipesharing"
compileSdk = 34

signingConfigs {
create("release") {
storeFile = file(System.getenv("KEYSTORE_PATH") ?: "")
storePassword = System.getenv("STORE_PASSWORD")
keyAlias = System.getenv("KEY_ALIAS")
keyPassword = System.getenv("KEY_PASSWORD")
if (System.getenv("CI") == "true") {
storeFile = file(System.getenv("KEYSTORE_PATH") ?: "")
storePassword = System.getenv("STORE_PASSWORD")
keyAlias = System.getenv("KEY_ALIAS")
keyPassword = System.getenv("KEY_PASSWORD")
} else {
storeFile = keystoreProperties["storeFile"]?.let { file(it) }
storePassword = keystoreProperties["storePassword"].toString()
keyAlias = keystoreProperties["keyAlias"].toString()
keyPassword = keystoreProperties["keyPassword"].toString()
}
}
}

Expand Down

0 comments on commit b7285c0

Please sign in to comment.