# 【GitHub Actions】オレオレAndroidプロジェクトCI設定

aridaiのGitHub Actionsのオレオレ設定をメモしておきます。

# おしながき

# CI

通常のCIです。
master へのPush・Pull Requestのタイミングで以下を実行します。

  • [skip ci] 有効化
  • Androidプロジェクトのビルド
  • 単体テスト
  • Lint
  • Danger (プルリクのときのみ)
name: CI

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

jobs:
  prepare:
    runs-on: ubuntu-latest
    if: "!(contains(github.event.head_commit.message, 'skip') && contains(github.event.head_commit.message, 'ci'))"
    steps:
      - name: Prepare
        run: ''

  build:
    runs-on: ubuntu-latest
    needs: [prepare]
    steps:
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.event.inputs.ref }}

      - uses: actions/cache@v2
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
          restore-keys: |
            ${{ runner.os }}-gradle-

      - name: Build
        run: ./gradlew assembleDebug

      - name: Test
        run: ./gradlew testDebugUnitTest

      - name: Lint
        run: ./gradlew lintDebug ktlintCheck

      - name: Setup Ruby
        uses: actions/setup-ruby@v1
        if: github.ref != 'refs/heads/master'
        with:
          ruby-version: '2.6'
          architecture: 'x64'

      - name: Run Danger
        if: github.ref != 'refs/heads/master'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gem install bundler
          bundle install
          danger

# Workflow Dispatch

↓ 手動でActionを実行します。
AndroidプロジェクトのビルドとAPKファイルのアップロードを行います。

Workflow Dispatch

name: Workflow Dispatch

on:
  workflow_dispatch:
    inputs:
      ref:
        description: 'branch | tag | commit SHA'
        required: true
        default: master

jobs:
  build:
    runs-on: ubuntu-latest
    steps:

      - uses: actions/checkout@v2
        with:
          ref: ${{ github.event.inputs.ref }}

      - uses: actions/cache@v2
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
          restore-keys: |
            ${{ runner.os }}-gradle-

      - name: Build
        run: ./gradlew assembleDebug

      - name: Find APK
        id: find-apk
        run: |
          path=$(find **/build/outputs/apk -name '*.apk' -type f | head -1)
          echo "::set-output name=path::$path"

      - name: Upload APK
        uses: actions/upload-artifact@v2
        with:
          name: apk
          path: ${{ steps.find-apk.outputs.path }}

# Release

タグが追加されたときのReleaseの発行を行います。

name: Release

on:
  push:
    tags:
      - "*"

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.event.inputs.ref }}

      - uses: actions/cache@v2
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
          restore-keys: |
            ${{ runner.os }}-gradle-

      - name: Build
        run: ./gradlew assembleDebug

      - name: Find APK
        id: find-apk
        run: |
          path=$(find **/build/outputs/apk -name '*.apk' -type f | head -1)
          echo "::set-output name=path::$path"

      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          files: ${{ steps.find-apk.outputs.path }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

びぇびぇミミッミ