🪚

永久機関が完成しちまったな~!

2024/03/16に公開

GitHub Actionsで毎晩fastlaneを走らせて、VersionをBump upしてPR作成して auto merge!永久機関が完成しちまったな~!これでノーベル賞は俺んもんだぜ~!

Version

今回はAndroidで行っていくぜ~
Semantic Versioningを参考にするぜ~

build.gradle.kts
    // MAJOR * 100000 + MINOR * 10000 + PATCH * 1000
    versionCode = 123000
    versionName = "1.2.3"

今回はversionCodeを毎晩 Bump up させていくぜ~

fastlane

fastlane-plugin-versioning_androidを使用していくぜ~

fastlane
Fastfile
platform :android do
  desc "Bump up version code"
  lane :bump_up_version_code do
    sh "git switch main"
    previous_version_code = android_get_version_code(
        gradle_file: "app/build.gradle.kts"
    ).to_i
    version_name = android_get_version_name(
        gradle_file: "app/build.gradle.kts"
    )
    version = version_name.split(".")
    major_version = version[0].to_i * 100000
    minor_version = version[1].to_i * 10000
    patch_version = version[2].to_i * 1000
    current_version = major_version + minor_version + patch_version
    pre_release_version = previous_version_code - current_version

    add_git_tag(
        tag: "v#{version_name}-#{pre_release_version}",
    )
    push_git_tags

    new_version_code = android_set_version_code(
        gradle_file: "app/build.gradle.kts"
    ).to_i
    new_pre_release_version = new_version_code - current_version
    branch = "#{version_name}-#{new_pre_release_version}"

    sh "git switch -c night-bump/#{branch}"
    git_commit(
        path: ["app/build.gradle.kts"],
        message: ":up: Version Bump #{branch}"
    )
    push_to_git_remote(
        set_upstream: true
    )
  end
end

bump upする前にgit tagをpushするぜえ~
tagはv1.2.3-0をpush、versionCode = 123001になるぜ~

GitHub Actions

workflowを作っていくぜ~

Bump
bump.yml
name: Bump up version

on:
    schedule:
        - cron: '0 14 * * *'

jobs:
    bump:
        name: Bump up
        runs-on: ubuntu-latest
        timeout-minutes: 20

        permissions:
            contents: write
            pull-requests: write

        env:
            BUNDLE_GEMFILE: ${{ github.workspace }}/Gemfile
            BASE_BRANCH: main
            LABEL: nightly bump

        outputs:
            tag: ${{ steps.tag-selector.outputs.tag }}
            pr: ${{ steps.create-pr-selector.outputs.pr }}

        steps:
            - uses: actions/checkout@v4
            - uses: ruby/setup-ruby@v1
              with:
                ruby-version: '3.2'
                bundler-cache: true # runs 'bundle install' and caches installed gems automatically

            - name: Set up git
              run: |
                git config user.name github-actions
                git config user.email github-actions@github.com

            - name: Bump up version
              run: bundle exec fastlane bump_up_version_code

            - name: Get latest tag
              id: tag-selector
              run: |
                tag=$(git describe --tags `git rev-list --tags --max-count=1`)
                echo "tag=$tag" >> "$GITHUB_OUTPUT"

            - name: Set up version
              id: version-selector
              run: |
                version=$(git branch --show-current | sed "s/night-release\///")
                echo "version=$version" >> "$GITHUB_OUTPUT"

            - name: Create pull request
              id: create-pr-selector
              env:
                GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              run: |
                tag_name=${{ steps.tag-selector.outputs.tag }}
                title=${{ steps.version-selector.outputs.version }}
                body=$(gh api /repos/{owner}/{repo}/releases/generate-notes -f tag_name=$tag_name -f target_commitish=${{ env.BASE_BRANCH }} --jq .body)
                pr=$(gh pr create \
                    --base ${{ env.BASE_BRANCH }} \
                    --title ":up: Version Bump v$title" \
                    --body "$body" \
                    --label "${{ env.LABEL }}"
                    )
                echo "pr=$pr" >> "$GITHUB_OUTPUT"

            - name: Auto merge
              id: merge-selector
              env:
                GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              run: |
                pr=$(gh pr merge ${{ steps.create-pr-selector.outputs.pr }} --admin -d -m)
                echo "pr=$pr" >> "$GITHUB_OUTPUT"

    release:
        name: Call release
        needs: bump-up
        permissions:
            contents: write
            pull-requests: write

        uses: ./.github/workflows/releases.yml
        with:
            tag: ${{ needs.bump-up.outputs.tag }}
            pr: ${{ needs.bump-up.outputs.pr }}
        secrets:
            token: ${{ secrets.GITHUB_TOKEN }}

Releases
bump.yml
name: Releases

on:
    workflow_call:
        inputs:
            tag:
                required: true
                type: string
            pr:
                required: true
                type: string
        secrets:
            token:
                required: true
jobs:
    releases:
        name: Releases
        runs-on: ubuntu-latest
        timeout-minutes: 20
        permissions:
            contents: write
            pull-requests: write

        env:
            GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

        steps:
            - uses: actions/checkout@v4

            - 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

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

            - name: Build with Gradle
              run: ./gradlew assembleDebug

            - name: Create Releases
              id: create-releases
              run: |
                   tar -czf artifact-${{ inputs.tag }}.tar.gz -C \
                   ${{ github.workspace }}/app/build/outputs/apk/debug .

            - name: Create release
              id: create-release
              run: |
                release=$(gh release create ${{ inputs.tag }} ${{ github.workspace }}/artifact-${{ inputs.tag }}.tar.gz --generate-notes)
  1. fastlaneを実行させてPRを作るぜ~
  2. PR は auto merge しちゃうぜ~
  3. bump upする前のtagのReleasesを作成するぜ~
  4. 自動生成リリース ノートによっていい感じになるぜ~

RepositoryのPermission設定が重要だぜ~


これで永久機関が完成しちまったぜ~
止めない限り終わらないぜ~
GitHub Actionsは有能だな

GitHubで編集を提案

Discussion