第25回/RedmineプラグインのGitHub Actionsの調査
メンバー: sanaksanak.icon
目的・背景:
汎用的に使えるRedmineプラグインのGitHub Actionsを作成して、Redmineプラグインのテストを自動化し、プラグインの開発・メンテナンスを効率的にできるようにしたい。
元のIssue: https://github.com/gtt-project/redmine_text_blocks/issues/16
これまでは、two-packさんの https://github.com/two-pack/redmine-plugin-test-action GitHub Actionプラグインを利用していたが、DockerHubの https://registry.hub.docker.com/r/twopackas/redmine-test-image のイメージを利用していて、GTTプラグインなど、ネイティブのGemビルドが必要な場合の対応が難しいため、複雑なパターンに対応できるRedmineプラグインのGitHub Action(※ベタ書き)を作成したい。
要件:
RedmineプラグインのGitHubリポジトリに設定することで、
Redmineプラグインのテストの自動実行が可能
Redmineプラグインがサポートする環境毎に組み合わせたテストが可能
Redmine: 4.2, 5.0, master(trunk)
Ruby: 2.6, 2.7, 3.0, 3.1
DB:
PostgreSQL: 10, 11, 12, 13, 14, (15)
PostGIS: 2.5, 3.0, 3.1, 3.2, (3.3)
MySQL: 5.7, 8.0
SQLite: - (OSバージョン)
なるべく実行時間を減らしたい
やること:
GitHub上で公開されているRedmine関連のGitHub Actions事例の調査
パッチ会リポジトリ
https://github.com/redmine-patch-meetup/redmine-dev-mirror/blob/develop/.github/workflows/test.yml
特徴:
DB毎の切り替え、共通処理が .github/actions/test-with-db.sh にまとめてあり、分かりやすい
https://github.com/redmine-patch-meetup/redmine-dev-mirror/blob/develop/.github/actions/test-with-db.sh
ただし、DB関連設定ファイルをRedmineリポジトリのconfig内に database.(mysql|postgres|sqlite).yml で作成する必要があるため、プラグインの場合は、定義保存用のファイルが別途必要
以下のコードでキャッシュを流用するようにして高速化を図っている
hashFiles('**/Gemfile') は配下のディレクトリ下の Gemfile を全て取ってきてハッシュ化するするもののよう (参考リンク: https://itneko.com/actions-hashfiles/ )
code:yml
with:
path: vendor/bundle
key: ${{ matrix.ruby }}-postgres-${{ hashFiles('**/Gemfile') }}
restore-keys: |
${{ matrix.ruby }}-postgres-
${{ matrix.ruby }}-
postgres/mysqlはサービス+Dockerイメージ利用、sqliteはrubyのDockerイメージをそのまま利用し、サービスはなし
Redmineプラグイン関連
https://github.com/nanego/redmine_datetime_custom_field/tree/master/.github/workflows
特徴:
DBはpostgresのみ
Redmineの最新パッチバージョン、master用にワークフローファイルを別分け
コンテナにrubyイメージを利用
code:yaml
container:
image: ruby:${{ matrix.ruby }}
ネイティブのGemビルド向きではない?
https://github.com/clear-code/redmine_full_text_search/blob/master/.github/workflows/test.yml
特徴:
DBはPostgreSQL・MySQLのみで、マトリックスは全組み合わせを定義
https://github.com/haru/redmine_theme_changer/blob/develop/.github/workflows/build.yml
特徴:
DBは全て(sqlite3, mysql, postgres)だが、バージョンは固定
マトリックスで、Redmineバージョンはステーブルブランチとmasterを利用し、 exclude で不要な組み合わせを弾いている
コンテナにrubyイメージを利用
https://github.com/cat-in-136/redmine_scheduling_poll/blob/master/.github/workflows/redmine_plugin.yml
特徴:
DBはsqlite3のみ
マトリックスは文字列で、RubyバージョンとGitHub上のRedmine/Redmicaリポジトリパスとタグバージョン
Zeitwerkチェックがある
https://github.com/AlphaNodes/redmine_sudo/blob/main/.github/workflows/tests.yml
特徴:
DBはpostgres, mysqlのみで、バージョンは固定
https://github.com/redmica/redmica_ui_extension/blob/master/.github/workflows/test.yml
特徴:
システム(Selenium)テストが含まれている
その他、AgilewareさんのCircle CIのRedmineプラグイン用環境 (石川さんより)
ドキュメント: https://circleci.com/developer/ja/orbs/orb/agileware-jp/redmine-plugin
リポジトリ: https://github.com/agileware-jp/redmine-plugin-orb
利用例: https://github.com/farend/redmine_message_customize/blob/master/.circleci/config.yml
実装方針
GTT関連プラグインでは、DBバージョン周りで特殊な要件が必要なこともあり、パッチ会のリポジトリを参考に実装を進め中
database設定ファイルについては、同じフォルダ内に含める予定
ファイル数が多いと、複数のGitHubリポジトリで扱う際に、処理が煩雑になるかも
複数のGitHubリポジトリ間でGitHub Actionsを共有する方法:
複数リポジトリでのGithub Actions運用 2021年の状況総まとめ
GitHub Actions アクションやワークフローを再利用してコードの重複をなくそう! - Qiita
1つの database.yml で定義する場合の例:
石川さんの redmine_development_docker の例: https://github.com/ishikawa999/redmine_development_docker/blob/master/overwrite_files/database.yml
code:yml
production:
adapter: <%= ENV'RAILS_DB_ADAPTER' %>
database: <%= ENV'RAILS_DB' %>
username: <%= ENV'RAILS_DB_USERNAME' %>
password: <%= ENV'RAILS_DB_PASSWORD' %>
host: <%= ENV'RAILS_DB_HOST' %>
encoding: <%= ENV'RAILS_DB_ENCODING' %>
development:
adapter: <%= ENV'RAILS_DB_ADAPTER' %>
database: <%= ENV'RAILS_DB' %>_development
username: <%= ENV'RAILS_DB_USERNAME' %>
password: <%= ENV'RAILS_DB_PASSWORD' %>
host: <%= ENV'RAILS_DB_HOST' %>
encoding: <%= ENV'RAILS_DB_ENCODING' %>
test:
adapter: <%= ENV'RAILS_DB_ADAPTER' %>
database: <%= ENV'RAILS_DB' %>_test
username: <%= ENV'RAILS_DB_USERNAME' %>
password: <%= ENV'RAILS_DB_PASSWORD' %>
host: <%= ENV'RAILS_DB_HOST' %>
encoding: <%= ENV'RAILS_DB_ENCODING' %>
@haruさんの redmine_theme_changer の例: https://github.com/haru/redmine_theme_changer/blob/develop/build-scripts/database.yml
code:yaml
sqlite3: &sqlite3
adapter: sqlite3
pool: 5
timeout: 5000
database: db/redmine.sqlite3
mysql: &mysql
adapter: mysql2
encoding: utf8
database: <%= ENV'DB_NAME' || 'redmine' %>
username: <%= ENV'DB_USERNAME' %>
password: <%= ENV'DB_PASSWORD' %>
host: <%= ENV'DB_HOST' %>
port: <%= ENV'DB_PORT' || 3306 %>
postgres: &postgres
adapter: postgresql
encoding: utf8
database: <%= ENV'DB_NAME' || 'redmine' %>
username: <%= ENV'DB_USERNAME' %>
password: <%= ENV'DB_PASSWORD' %>
host: <%= ENV'DB_HOST' %>
port: <%= ENV'DB_PORT' || 5432 %>
development:
<<: *<%= ENV'DB' || 'sqlite3' %>
test:
<<: *<%= ENV'DB' || 'sqlite3' %>
production:
<<: *<%= ENV'DB' || 'sqlite3' %>
=> database設定ファイルについては、AgilewareさんのCircle CIコードを参考に、test環境のみヒアドキュメントで記載する方向に
https://circleci.com/developer/ja/orbs/orb/agileware-jp/redmine-plugin#commands-generate-database_yml
code:yaml
if $DATABASE_ADAPTER = "postgresql" ; then
cat \<<-'EOF' > database.yml
test:
adapter: postgresql
database: circle_test
host: 127.0.0.1
username: postgres
password: postgres
EOF
=> プラグイン関連箇所については、パッチ会のリポジトリではカバーされていなかったので、最終的に、 redmine_sudo を参考に
実装
下記の個人(sanak)アカウントのPRで動作確認中
https://github.com/sanak/redmine_text_blocks/pull/1
GitHub Actionsの ubuntu-22.04 イメージ上では、 ruby/setup-ruby@v1 がRuby3.1以上しかサポートしていないようだったため、一旦 ubuntu-20.04 にダウングレード
https://github.com/sanak/redmine_text_blocks/runs/7608590631?check_suite_focus=true#step:7:8
code:text
Run ruby/setup-ruby@v1
Error: Error: Unknown version 2.6 for ruby on ubuntu-22.04
available versions for ruby on ubuntu-22.04: 3.1.0, 3.1.1, 3.1.2, 3.2.0-preview1, head, debug
Make sure you use the latest version of the action with - uses: ruby/setup-ruby@v1
将来が不安なので、コンテナにrubyイメージを利用 のパターンについて一度トライ
=> OKそうなので、こちらを採用します。
データベース種別毎に一つのファイル内にJobを記載していくのは少し冗長になりそうだったので、 test-postgres.yml などのようにデータベース種別毎にファイルを分けるようにしました。
最終
社内レビューですが、以下のPRで取り込まれました。
https://github.com/gtt-project/redmine_text_blocks/pull/26
10/29以降追記
DB毎にファイルが分かれていて、各ファイル後半の steps 箇所の重複が気になったので、DBバージョンのmatrixは犠牲にして、一つのファイル内に収める。
redmine_version 箇所について、Redmine固定でなく、RedMicaでも使いたかったので、以下を参考に、どちらもサポートできるように変更。
https://stackoverflow.com/questions/66025220/paired-values-in-github-actions-matrix
システムテストとコアテスト(いずれもコメントアウト)を追加