GitHub Actions MysQL+Rails 구성으로 Annotate 제작 PR 수행

17126 단어 annotatetech

개요


MySQL+Rails로 구성할 때 GiitHub ActionsAnnotate에서 모델에 대한 설명을 했기 때문에 그 메모입니다.

Gist


이거 봐. 특별해.
https://gist.github.com/junara/99df854e984ae283c38117c865bf2845

배경.


Annotate .댓글이 있어서 편해요.특히 모델에 대한 평론이 있으면 매우 편리하다.
모델을 볼 때는 열에 대한 의식이 있어야 하는데 원형을 유지하면 열을 모르지.schema.rb를 보면 되는데 거기까지 이동선도 없어요.
좀 느끼하지만 모델에 schema를 쓰면 편해요.
그리고 이 schema에 관해서는migration과 함께 추가하는 경우가 많다고 생각합니다.
Annotate: Configuration in Rails
    'skip_on_db_migrate'   => 'false',
근데 이걸 잊을 때도 있어.빈도도 많지 않으니 주의할 때 하면 된다.
사실 나도 다음 지령의 순간에 낼 것이다.
bundle exec annotate --models
이 정도면 충분해요.
//하지만, 엔지니어니까 가능하면 이 작업도 한꺼번에 자동화하고 싶어요.그렇죠?
그래서 기리허브 액션스를 만들었다.

실현된 일


Annotate에서 모델에 주석을 달아 GiitHub Actions가 언제든지 Pull Request를 만들 수 있도록 합니다.

컨디션


Rails(6.1로 시도해 보았지만 annotate가 움직이면 무엇이든 됩니다.)
Annotate

메서드


Annotate는 데이터베이스의 schema를 보고 있습니다.(단지migraton 데이터베이스가 없으면 전혀 논평을 할 수 없기 때문에 저는 이렇게 고찰했습니다.)
따라서 주석만 붙이면 데이터베이스의migration이 필요하다.
선택할 수 있는 것은 다음과 같은 두 가지가 있다.
동적 데이터.yml를 sqlite3과 대응하는 유사하게 바꾸기
  • GiitHub Action 내에서 MySQL 컨테이너를 준비하여migration
  • 이번에 CI로 GiitHub Actions를 만들면 MySQL 컨테이너도 읽을 수 있을 것 같아서 GiitHub Actions에서 MySQL contect를 만드는 방법을 채택했습니다.
    한편, Pull Request 제작은 https://github.com/peter-evans/create-pull-request를 사용했다.위 페이지의 샘플은 고스란히 남아 있다.
    구체적인 코드는 함께 설명한다.

    database.yml


    다음 데이터베이스입니다.yml을 제작했습니다.중점은 아니지만 매개 변수는 환경 변수입니다.CI를 도입했다면 똑같은 느낌을 받았을 거라고 생각합니다.
    이번에는 테스트 환경만 사용합니다.config/database.yml
    default: &default
      adapter: mysql2
      host: <%= ENV['MYSQL_HOST'] || '127.0.0.1' %>
      port: <%= ENV['MYSQL_PORT'] || 3306 %>
      encoding: utf8mb4
      charset: utf8mb4
      collation: utf8mb4_unicode_ci
      pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
      timeout: 5000
    
    development:
      <<: *default
      database: <%= ENV['MYSQL_DATABASE'] || 'junara_annotate_development' %>
      username: <%= ENV['MYSQL_USER'] || 'root' %>
      password: <%= ENV['MYSQL_PASSWORD'] || 'password' %>
    
    test:
      <<: *default
      database: junara_annotate_test
      username: root
      password: password
    
    production:
      <<: *default
      database: junara_annotate_production
    
    

    GTHub Actions용 yaml


    GiitHub Action을 정의합니다.
    MySQL+Rails에서는 GiitHub Actions에서 CI를 조합할 때 고정된 구성이다.
    Pull Request 제작도 https://github.com/peter-evans/create-pull-request에 적힌 내용에서 삭제했다.
    요점git checkout db/schema.rb.데이터베이스의 구성이 완전히 일치하지 않기 때문에schema.rb에서 차별이 생기다.하지만 이번 관심은 shcema다.rb가 아니기 때문에 나쁘지 않아요..github/workflows/main.yml
    name: Annotate
    on:
      workflow_dispatch: # Actionsタブから実行できるようにする。
    
    jobs:
      createAnnotatePullRequest:
        services:
          mysql:
            image: mysql:5.7
            env:
              MYSQL_ROOT_PASSWORD: password
              MYSQL_DATABASE: junara_annotate_test
            ports:
              - 3306:3306
            options: --health-cmd "mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries 10
    
        strategy:
          fail-fast: false
          matrix:
            os: [ ubuntu-latest ]
            ruby: [ 3.0 ] # ruby version指定。matrixでなくても可。
    
        env:
          ENV: test
          MYSQL_HOST: 127.0.0.1 # localhostだとつながらない
          MYSQL_PORT: 3306
          MYSQL_DATABASE: junara_annotate_test # servicesの MYSQL_DATABASEと合わせる
          MYSQL_USER: root
          MYSQL_PASSWORD: password # servicesの MYSQL_ROOT_PASSWORDとあわせる
        runs-on: ${{ matrix.os }}
    
        steps:
          - uses: actions/checkout@v2
          - name: Set up Ruby
            uses: ruby/setup-ruby@v1
            with:
              ruby-version: ${{ matrix.ruby }}
              bundler-cache: true # cacheする。
    
          - name: Run annotate
            run: |
              bundle exec rails db:migrate:reset # annotateが読み取れるようにmigrationし直す
              bundle exec annotate --models --with-comment # annotateを実行する
    
          - name: Skip db/schema.rb
            run: |
              git checkout db/schema.rb # mysqlの起動設定が、異なるといつもとことなる schema.rbがcommitされてしまうので、commitされないようにする。
    
          - name: set datetime_str to env
            run: |
              echo "DATETIME_STR=$(date '+%Y%m%d%H%M%S')" >> $GITHUB_ENV # PRのTITLEに日付を入れるため
    
          - name: Create Pull Request
            id: cpr
            uses: peter-evans/create-pull-request@v3 # GitHub ActionsでPull requestを作る時の定番
            with:
              token: ${{ secrets.GITHUB_TOKEN }} # committerがGitHubならこれでOK
              commit-message: Update annotate # コミットメッセージ
              committer: GitHub <[email protected]>
              author: ${{ github.actor }} <${{ github.actor }}@users.noreply.github.com> # ここら辺変更する場合は tokenの変更も必要そう(未確認)
              signoff: false
              branch: create-pull-request/annotate/${{ env.DATETIME_STR }} # ブランチ名。
              delete-branch: true
              title: '[Annotate] annotate --models --with-comment'
              body: |
                - Update model annotations
                - Auto-generated by [create-pull-request][1]
    
                [1]: https://github.com/peter-evans/create-pull-request
              draft: false
              base: development # PRを作成するベースブランチ。このGitHub Actionsを実行する時にここからbranchを作成し、ここに(developmentブランチに)PRを作成する。
    

    좋은 웹페이지 즐겨찾기