do-github-release #489
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
name: do-github-release | |
on: | |
schedule: | |
# Run every Monday at 20:00 (8:00 PM) | |
- cron: "0 20 * * 1" | |
workflow_dispatch: | |
inputs: | |
bump_type: | |
description: "Specify the type of version bump (major, minor, patch)" | |
required: false | |
default: "patch" | |
repository_dispatch: | |
types: [trigger-release] | |
jobs: | |
release: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Repo | |
uses: actions/checkout@v4 | |
with: | |
token: ${{ secrets.ACTIONS_PAT }} | |
fetch-depth: 0 | |
- name: Determine Bump Type Based on Trigger | |
id: determine-bump-type | |
run: | | |
if [ "${{ github.event_name }}" == "schedule" ]; then | |
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 HEAD | grep '^app' || echo "none") | |
if [ "$changed_files" != "none" ]; then | |
echo "App directory has changed since the last release, proceeding with new release" | |
else | |
echo "No app directory changes since the last release. Skipping release." | |
exit 0 | |
fi | |
BUMP_TYPE="patch" | |
elif [ "${{ github.event_name }}" == "workflow_dispatch" ]; then | |
BUMP_TYPE="${{ github.event.inputs.bump_type }}" | |
elif [ "${{ github.event_name }}" == "repository_dispatch" ]; then | |
BUMP_TYPE="${{ github.event.client_payload.bump_type }}" | |
else | |
echo "Unknown trigger source." | |
exit 1 | |
fi | |
if [[ ! "$BUMP_TYPE" =~ ^(major|minor|patch)$ ]]; then | |
echo "Invalid bump type: $BUMP_TYPE" | |
exit 1 | |
fi | |
echo "BUMP_TYPE=$BUMP_TYPE" >> $GITHUB_ENV | |
- name: Bump Version in version.properties | |
id: bump-version | |
run: | | |
echo "Bumping version with BUMP_TYPE: $BUMP_TYPE" | |
source version.properties | |
if [ "$BUMP_TYPE" == "major" ]; then | |
majorVersion=$((majorVersion + 1)) | |
minorVersion=0 | |
patchVersion=0 | |
elif [ "$BUMP_TYPE" == "minor" ]; then | |
minorVersion=$((minorVersion + 1)) | |
patchVersion=0 | |
else | |
patchVersion=$((patchVersion + 1)) | |
fi | |
echo "majorVersion=$majorVersion" > version.properties | |
echo "minorVersion=$minorVersion" >> version.properties | |
echo "patchVersion=$patchVersion" >> version.properties | |
VERSION=$majorVersion.$minorVersion.$patchVersion | |
echo "VERSION=$VERSION" >> $GITHUB_ENV | |
- name: Process Changelog Changes | |
id: process-changelog | |
run: | | |
unreleased_section=$(sed -n '/## \[Unreleased\]/,/^## /p' CHANGELOG.md | sed '1,2d' | sed '$d') | |
if [ -z "$unreleased_section" ]; then | |
changelog_escaped="No release notes" | |
else | |
changelog_escaped=$(echo "$unreleased_section" | sed 's/^- /✔ /g' | sed ':a;N;$!ba;s/\n/%0A/g') | |
fi | |
echo "changelog_additions=$changelog_escaped" >> $GITHUB_OUTPUT | |
today=$(date +'%Y-%m-%d') | |
sed -i "s/## \[Unreleased\]/## [v$VERSION] - $today/" CHANGELOG.md | |
sed -i '/## \[v'$VERSION'\]/i ## [Unreleased]\n\n### Added\n\n### Changed\n\n### Fixed\n' CHANGELOG.md | |
REPO_URL="https://github.com/${{ github.repository }}/releases/tag" | |
printf "\n[v$VERSION]: $REPO_URL/$VERSION" >> CHANGELOG.md | |
- name: Set Up Build Environment | |
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" | |
- 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@v4 | |
with: | |
name: Signed APK | |
path: app/build/outputs/ | |
- name: Make GitHub Release | |
uses: svenstaro/upload-release-action@v2 | |
with: | |
repo_token: ${{ secrets.GITHUB_TOKEN }} | |
release_name: "v${{ env.VERSION }}" | |
tag: "${{ env.VERSION }}" | |
file: app/build/outputs/apk/release/app-release-unsigned-signed.apk | |
asset_name: "Field-Book-v${{ env.VERSION }}.apk" | |
body: ${{ steps.process-changelog.outputs.changelog_additions }} | |
- name: Commit Version and Changelog Changes | |
if: ${{ success() }} | |
uses: EndBug/add-and-commit@v7 | |
with: | |
add: | | |
version.properties | |
CHANGELOG.md | |
message: "Bump ${{ env.BUMP_TYPE }} and update changelog for v${{ env.VERSION }}" | |
token: ${{ secrets.ACTIONS_PAT }} | |
- name: Push Changes | |
uses: ad-m/github-push-action@master | |
with: | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
branch: ${{ github.ref }} | |
#- name: Check date for Google Play upload | |
# id: date_check | |
# run: | | |
# CURRENT_DATE=$(date +%m%d) | |
# echo "CURRENT_DATE: $CURRENT_DATE" | |
# if [ "$CURRENT_DATE" -ge 0415 ] && [ "$CURRENT_DATE" -le 0915 ]; then | |
# echo "UPLOAD_TO_PLAY_STORE=false" >> $GITHUB_ENV | |
# else | |
# echo "UPLOAD_TO_PLAY_STORE=true" >> $GITHUB_ENV | |
# fi | |
#- name: Release APK to Play Store | |
# if: env.UPLOAD_TO_PLAY_STORE == 'true' | |
# 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 |