do-github-release #415
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on: | |
schedule: | |
- cron: "0 20 * * *" | |
workflow_dispatch: | |
name: do-github-release | |
jobs: | |
check-app-changes: | |
runs-on: ubuntu-latest | |
outputs: | |
app_changed: ${{ steps.check_app_changes.outputs.app_changed }} | |
steps: | |
- name: Checkout repo | |
uses: actions/checkout@v2 | |
with: | |
token: ${{secrets.ACTIONS_PAT}} | |
fetch-depth: 0 | |
- name: Check if app directory changed | |
id: check_app_changes | |
run: | | |
LAST_RELEASE_TAG=$(git describe --tags $(git rev-list --tags --max-count=1)) | |
echo "LAST_RELEASE_TAG was $LAST_RELEASE_TAG" | |
LAST_RELEASE_COMMIT=$(git rev-list -n 1 $LAST_RELEASE_TAG) | |
echo "LAST_RELEASE_COMMIT was $LAST_RELEASE_COMMIT" | |
changed_files=$(git diff-tree --no-commit-id --name-only $LAST_RELEASE_COMMIT $GITHUB_SHA | grep '^app' || echo "none") | |
echo "Changed app files: $changed_files" | |
if [ "$changed_files" != "none" ]; then | |
echo "App directory has changed since the last release." | |
echo "app_changed=true" >> "$GITHUB_OUTPUT" | |
else | |
echo "App directory hasn't changed since the last release." | |
echo "app_changed=false" >> "$GITHUB_OUTPUT" | |
fi | |
build-and-release: | |
runs-on: ubuntu-latest | |
needs: check-app-changes | |
if: ${{ needs.check-app-changes.outputs.app_changed == 'true' }} | |
steps: | |
- name: Checkout repo | |
uses: actions/checkout@v4 | |
with: | |
token: ${{secrets.ACTIONS_PAT}} | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: Setup Android SDK | |
uses: android-actions/setup-android@v3 | |
- name: Install Build Tools 29.0.3 | |
run: sdkmanager "build-tools;29.0.3" | |
# Uses semantic commits to automate version bumping. | |
# No scope or "fix:" = PATCH, "feat:" or "minor:" = MINOR, "BREAKING CHANGE:", "major:", or fix/feat with appended "!" = MAJOR | |
# Additional details: https://www.conventionalcommits.org/en/v1.0.0/ | |
- name: Increment version | |
run: | | |
#!/bin/bash | |
COMMIT_MSG=$(git log -1 --pretty=format:"%b" || git log -1 --pretty=format:"%B") | |
source version.properties | |
BUMP_TYPE="patchVersion" | |
if [[ $COMMIT_MSG == *"BREAKING CHANGE"* || $COMMIT_MSG == *"!"* || $COMMIT_MSG == *"major:"* ]]; then | |
majorVersion=$((majorVersion + 1)) | |
minorVersion=0 | |
patchVersion=0 | |
BUMP_TYPE="majorVersion" | |
elif [[ $COMMIT_MSG == *"feat:"* || $COMMIT_MSG == *"minor:"* ]]; then | |
minorVersion=$((minorVersion + 1)) | |
patchVersion=0 | |
BUMP_TYPE="minorVersion" | |
else | |
patchVersion=$((patchVersion + 1)) | |
fi | |
echo "majorVersion=$majorVersion" > version.properties | |
echo "minorVersion=$minorVersion" >> version.properties | |
echo "patchVersion=$patchVersion" >> version.properties | |
VERSION=$majorVersion.$minorVersion.$patchVersion | |
RELEASE=$majorVersion.$minorVersion | |
echo "COMMIT_MSG=$COMMIT_MSG" >> $GITHUB_ENV | |
echo "VERSION=$VERSION" >> $GITHUB_ENV | |
echo "RELEASE=$RELEASE" >> $GITHUB_ENV | |
echo "BUMP_TYPE=$BUMP_TYPE" >> $GITHUB_ENV | |
- name: Commit version changes | |
uses: EndBug/add-and-commit@v7 | |
with: | |
add: 'version.properties' | |
message: Bump ${{ env.BUMP_TYPE }} | |
author_email: git-action-bot@example.com | |
author_name: Git Action Bot | |
- name: Push changes | |
uses: ad-m/github-push-action@master | |
with: | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
branch: ${{ github.ref }} | |
- name: Make gradlew executable | |
run: chmod +x ./gradlew | |
- name: Build APK | |
run: ./gradlew app:assembleRelease | |
- name: Sign APK | |
uses: r0adkll/sign-android-release@v1 | |
with: | |
releaseDirectory: app/build/outputs/apk/release | |
signingKeyBase64: ${{ secrets.SIGNING_KEY }} | |
alias: ${{ secrets.ALIAS }} | |
keyStorePassword: ${{ secrets.KEY_STORE_PASSWORD }} | |
keyPassword: ${{ secrets.KEY_PASSWORD }} | |
- name: Upload built APK | |
uses: actions/upload-artifact@v2 | |
with: | |
name: Signed APK | |
path: app/build/outputs/ | |
- name: Get matching release | |
uses: cardinalby/git-get-release-action@1.2.4 | |
id: matching_release | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
with: | |
releaseName: v${{ env.RELEASE }} | |
doNotFailIfNotFound: 'true' | |
- name: Delete matching release if exists | |
if: steps.matching_release.outputs.id != '' | |
run: | | |
curl -X DELETE -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" "https://api.github.com/repos/${{ github.repository }}/releases/${{ steps.matching_release.outputs.id}}" | |
# Updates the latest release if just a new patch, drafts a new prerelease if major or minor version has changed | |
- name: Make github release | |
uses: svenstaro/upload-release-action@v2 | |
with: | |
repo_token: ${{ secrets.GITHUB_TOKEN }} | |
release_name: v${{ env.RELEASE }} | |
tag: ${{ env.VERSION }} | |
file: app/build/outputs/apk/release/app-release-unsigned-signed.apk | |
asset_name: Field-Book-v${{ env.RELEASE }}.apk | |
overwrite: ${{ env.BUMP_TYPE == 'patchVersion' }} | |
prerelease: ${{ env.BUMP_TYPE != 'patchVersion' }} | |
body: | | |
${{ steps.matching_release.outputs.body }} | |
${{ env.COMMIT_MSG }} | |
- name: Release APK to Play Store | |
uses: r0adkll/upload-google-play@v1 | |
with: | |
serviceAccountJsonPlainText: ${{ secrets.SERVICE_ACCOUNT_JSON }} | |
packageName: com.fieldbook.tracker | |
releaseFiles: app/build/outputs/apk/release/app-release-unsigned-signed.apk | |
track: alpha |