Last active
July 10, 2024 09:20
-
-
Save yasuyuki-kamata/fd8dee7f9224ad1633f346da9b27aa9e to your computer and use it in GitHub Desktop.
Androidプロジェクトのビルドの自動化とリリースの作成をするGitHub Actions
This file contains hidden or 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
# MIT License | |
# | |
# Copyright (c) 2024 Yasuyuki Kamata | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
name: Android CI and Release | |
on: | |
push: | |
branches: [ "main" ] | |
pull_request: | |
branches: [ "main" ] | |
workflow_dispatch: | |
inputs: | |
version: | |
description: 'App version' | |
required: true | |
default: '1.0.0' | |
permissions: | |
contents: write | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Check if version exists | |
run: | | |
if git rev-parse v${{ github.event.inputs.version }} >/dev/null 2>&1; then | |
echo "Error: Version ${{ github.event.inputs.version }} already exists" | |
exit 1 | |
fi | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
cache: gradle | |
- name: Grant execute permission for gradlew | |
run: chmod +x gradlew | |
- name: Build with Gradle | |
run: ./gradlew build | |
- name: Run tests | |
run: ./gradlew test | |
- name: Build Debug APK | |
run: ./gradlew assembleDebug | |
- name: Rename APK | |
run: mv app/build/outputs/apk/debug/app-debug.apk app/build/outputs/apk/debug/app-debug.apk | |
- name: Upload APK | |
uses: actions/upload-artifact@v4 | |
with: | |
name: app-debug.apk | |
path: app/build/outputs/apk/debug/app-debug.apk | |
retention-days: 5 | |
- name: Generate Changelog | |
id: generate_changelog | |
if: github.event_name == 'workflow_dispatch' | |
run: | | |
echo "Generating changelog..." | |
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "") | |
REPO_URL="https://github.com/${{ github.repository }}" | |
if [ -z "$LATEST_TAG" ]; then | |
# no tags (initial release) | |
echo "## Changes in this release:" > changelog.txt | |
echo "- Initial release" >> changelog.txt | |
git log --pretty=format:"- [%s]($REPO_URL/commit/%H)" >> changelog.txt | |
else | |
# tags exist | |
echo "## Changes in this release:" > changelog.txt | |
git log $LATEST_TAG..HEAD --pretty=format:"- %s($REPO_URL/commit/%H)" >> changelog.txt | |
echo "" >> changelog.txt | |
echo "" >> changelog.txt | |
echo "**Full Changelog**: $REPO_URL/compare/$LATEST_TAG...v${{ github.event.inputs.version }}" >> changelog.txt | |
fi | |
changelog=$(cat changelog.txt) | |
echo "CHANGELOG<<EOF" >> $GITHUB_OUTPUT | |
echo "$changelog" >> $GITHUB_OUTPUT | |
echo "EOF" >> $GITHUB_OUTPUT | |
- name: Create Release | |
if: github.event_name == 'workflow_dispatch' | |
uses: softprops/action-gh-release@v2 | |
with: | |
tag_name: v${{ github.event.inputs.version }} | |
name: Release ${{ github.event.inputs.version }} | |
body: | | |
Release of version ${{ github.event.inputs.version }} | |
${{ steps.generate_changelog.outputs.CHANGELOG }} | |
draft: false | |
prerelease: false | |
files: app/build/outputs/apk/debug/app-debug.apk | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment