0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GitHub ActionsでktlintFormatのコミットを作成する

Posted at

概要

Androidのアプリ開発でktlintFormatを適用したいです。
毎回自分でやるのは手間なので、GitHub Actionsでやらせていきます。

前提

ktlintFormatが実行できること
以下のコマンドが実行できればOK

./gradlew ktlintFormat

結論

ktlint.yaml
name: ktlint CI

permissions:
  actions: write
  checks: write
  contents: write

on:
  push:
    branches:
      - "master"
      - "docs/actions"
    paths-ignore:
      - README.md
  pull_request:
    branches: [ "master" ]
    paths-ignore:
      - README.md

jobs:
  ktlint:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
      - name: Set up Java 17
        uses: actions/setup-java@v3
        with:
          distribution: microsoft
          java-version: 17

      - name: Checkout
        uses: actions/checkout@v2
        with:
          ref: ${{ github.head_ref }} # 現在のbranchを指定する
          fetch-depth: 0

      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      - name: Run ktlintFormat
        run: ./gradlew ktlintFormat

      # ktlintFormatの結果、ファイルに差分の有無をチェックする
      - name: Check if there are any changes
        id: verify_diff
        run: |
          git diff --quiet . || echo "changed=true" >> $GITHUB_OUTPUT

      - name: Run Push
        # ファイルに差分があるときにのみpushする
        if: steps.verify_diff.outputs.changed == 'true'
        run: |
          git config user.name github-actions
          git config user.email github-actions@github.com
          git add app/src/*
          git commit -m "[Auto] ktlintFormat"
          git push

詳細

ポイントを絞って補足していきます。

権限の追加

コミットを作成するにあたって権限を付与する必要があります。
すべては不要かもしれません。

ktlint.yaml
permissions:
  actions: write
  checks: write
  contents: write

ブランチの変更

現在のブランチに切り替えなければならないケースがあります。

ktlint.yaml
   - name: Checkout
        uses: actions/checkout@v2
        with:
          ref: ${{ github.head_ref }} # 現在のbranchを指定する
          fetch-depth: 0

gradlewの実行権限

gradlewの実行権限を付与する

ktlint.yaml
      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

ktlintFomratの実行

ここまででktlintFomratを実行する準備ができました。
ktlintFormatを実行します。

ktlint.yaml
      - name: Run ktlintFormat
        run: ./gradlew ktlintFormat

ファイル変更の有無をチェック

ktlintFormatにより、ファイルに変更があるかどうかを確認します。

ktlint.yaml
      - name: Check if there are any changes
        id: verify_diff
        run: |
          git diff --quiet . || echo "changed=true" >> $GITHUB_OUTPUT

プッシュする

ファイルに変更がある場合にのみコミットを作成し、pushする

ktlint.yaml
     - name: Run Push
        if: steps.verify_diff.outputs.changed == 'true'
        run: |
          git config user.name github-actions
          git config user.email github-actions@github.com
          git add app/src/*
          git commit -m "[Auto] ktlintFormat"
          git push

注意

ktlintFormatを実行しても解決しない指摘に関しては、あらかじめ発生しないようにしておく必要があります。
例えば、以下の指摘の場合にはAndroid Studioの設定でワイルドカードのimportを禁止するようにしておくのがよいでしょう。

Wildcard import (cannot be auto-corrected)

参考

以下、参考にさせていただきました。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?