19
9

More than 3 years have passed since last update.

プルリク前後の apk ファイルを比較する GitHub Actions

Last updated at Posted at 2020-05-26

下記のような yaml ファイルと Danger 用の設定ファイルを、リポジトリの .github/workflows 配下に置きます。

.github/workflows/apk-info.yml
name: apk info

on: pull_request

env:
  GRADLE_BUILD_TASK: 'assembleDebug'

jobs:
  build-head:
    name: Build head
    runs-on: ubuntu-18.04
    steps:
      - name: Check out
        uses: actions/checkout@v2
        with:
          ref: ${{ github.event.pull_request.head.sha }}
      - name: Set up JDK
        uses: actions/setup-java@v1
        with:
          java-version: 1.8
      - name: Build with Gradle
        run: ./gradlew $GRADLE_BUILD_TASK
      - name: Get apk path
        id: apk-path
        run: |
          path=$(find **/build/outputs/apk -name '*.apk' -type f | head -1)
          echo "::set-output name=path::$path"
      - name: Upload apk file
        uses: actions/upload-artifact@v1
        with:
          name: head
          path: ${{ steps.apk-path.outputs.path }}
  build-base:
    name: Build base
    runs-on: ubuntu-18.04
    steps:
      - name: Check out
        uses: actions/checkout@v2
        with:
          ref: ${{ github.event.pull_request.base.sha }}
      - name: Set up JDK
        uses: actions/setup-java@v1
        with:
          java-version: 1.8
      - name: Build with Gradle
        run: ./gradlew $GRADLE_BUILD_TASK
      - name: Get apk path
        id: apk-path
        run: |
          path=$(find **/build/outputs/apk -name '*.apk' -type f | head -1)
          echo "::set-output name=path::$path"
      - name: Upload apk file
        uses: actions/upload-artifact@v1
        with:
          name: base
          path: ${{ steps.apk-path.outputs.path }}
  check:
    name: Check
    needs: [build-head, build-base]
    runs-on: macos-10.15 # apkanalyzerを利用する為
    steps:
      - name: Check out
        uses: actions/checkout@v2
        with:
          ref: ${{ github.event.pull_request.head.sha }}
          fetch-depth: 0 # for Danger
      - name: Download head apk file
        uses: actions/download-artifact@v1
        with:
          name: head
          path: head
      - name: Get head apk path
        id: head-apk-path
        run: |
          path=$(find head -name '*.apk' -type f | head -1)
          echo "::set-output name=path::$path"
      - name: Download base apk file
        uses: actions/download-artifact@v1
        with:
          name: base
          path: base
      - name: Get base apk path
        id: base-apk-path
        run: |
          path=$(find base -name '*.apk' -type f | head -1)
          echo "::set-output name=path::$path"
      - name: Set up Ruby for Danger
        uses: actions/setup-ruby@v1
        with:
          ruby-version: '2.6'
          architecture: 'x64'
      - name: Set up Danger
        run: gem install danger:6.2.0 danger-apkstats:0.2.0
      - name: Run Danger
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          HEAD_APK_PATH: ${{ steps.head-apk-path.outputs.path }}
          BASE_APK_PATH: ${{ steps.base-apk-path.outputs.path }}
        run: |
          danger --dangerfile='.github/workflows/apk-info.danger' --danger_id='${{ github.workflow }}' --remove-previous-comments --fail-on-errors=true
.github/workflows/apk-info.danger
# ref https://danger.systems/

head_apk_path = ENV['HEAD_APK_PATH']
base_apk_path = ENV['BASE_APK_PATH']

apkstats.apk_filepath=head_apk_path
apkstats.compare_with(base_apk_path, do_report: true)

GitHub Actions の pull request イベントの ${{ github.event.pull_request.base.sha }} で、プルリクの起点となった commit SHA が取得できるので、起点の commit SHA とプルリクの commit SHA の両地点でビルドして、Danger のプラグイン danger-apkstats で比較・レポートを行っています。

ワークフローが成功すると、次のようにプルリクへコメントが書き込まれます。

image.png

danger-apkstats の README に書かれているように、もし Permission 等に変更があれば変更内容が書き込まれます。

19
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
9