メインコンテンツへスキップする

継続的インテグレーション

ソフトウェアの品質を保証するために、継続的インテグレーションワークフロー、CI を導入する組織が増えています。 CIでは、コードベースに対するすべての変更に対して、一連の自動化された検証を継続的に実行します。 CIでは、さまざまな種類の検証を実行できます。

  • 最新バージョンのコンパイルまたはビルドを行い、それが壊れていないことを確認する
  • コーディング規約を強制するためのリンティング
  • それぞれのコンポーネントが期待通りに動作し、コードベースへの変更が他の場所でリグレッションを引き起こさないことを検証する単体テスト
  • 既知の脆弱性がコードベースに持ち込まれていないことを検証するためのセキュリティスキャン
  • などなどたくさん!

Ent コミュニティとの議論から、Ent を使用している多くのチームが既に CI を使用しており、Ent 特有の検証をワークフローに適用したいと考えていることがわかりました。

To support the community with this effort we have started this guide which documents common best practices to verify in CI and introduces ent/contrib/ci a GitHub Action we maintain that codifies them.

生成されたファイルがすべてチェックインされていることを検証する

Ent はコード生成に大きく依存しています。 私たちの経験では、生成されたコードは常にソース管理下にチェックインされるべきです。 これには2つの理由があります。

  • 生成されたコードがソース管理にチェックインされていれば、メインのアプリケーションコードと一緒に読むことができます。 コードがレビューされるとき、あるいはリポジトリがブラウズされるときに、生成されたコードが存在することは、コード全体がどのように動作するかを把握するために必要です。
  • チームメンバー間の開発環境の違いも簡単に発見でき、修正することができます。 全員が同じコードを実行しているので、「私のマシンでは動いたのに」というような問題が発生する可能性を低くできます。

If you're using GitHub for source control, it's easy to verify that all generated files are checked in with the ent/contrib/ci GitHub Action. GitHub以外の場合は、既存のCIフローに組み込めるシンプルなbashスクリプトを提供します。

リポジトリに `.github/workflows/ent-ci.yaml` という名前のファイルを追加するだけです。
name: EntCI
on:
push:
# masterに変更があるたびに実行されます
branches:
- master
# Run on PRs where something changed under the `ent/` directory.
pull_request:
paths:
- 'ent/*'
jobs:
ent:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3.0.1
- uses: actions/setup-go@v3
with:
go-version-file: 'go.mod'
- uses: ent/contrib/ci@master

Lint migration files

Changes to your project's Ent schema almost always result in a modification of your database. If you are using Versioned Migrations to manage changes to your database schema, you can run migration linting as part of your continuous integration flow. This is done for multiple reasons:

  • Linting replays your migration directory on a database container to make sure all SQL statements are valid and in the correct order.
  • Migration directory integrity is enforced - ensuring that history wasn't accidentally changed and that migrations that are planned in parallel are unified to a clean linear history.
  • Destructive changes are detected notifying you of any potential data loss that may be caused by your migrations way before they reach your production database.
  • Linting detects data-dependant changes that may fail upon deployment and require more careful review from your side.

If you're using GitHub, you can use the Official Atlas Action to run migration linting during CI.

Add .github/workflows/atlas-ci.yaml to your repo with the following contents:

name: Atlas CI
on:
# Run whenever code is changed in the master branch,
# change this to your root branch.
push:
branches:
- master
# Run on PRs where something changed under the `ent/migrate/migrations/` directory.
pull_request:
paths:
- 'ent/migrate/migrations/*'
jobs:
lint:
services:
# Spin up a mysql:8.0.29 container to be used as the dev-database for analysis.
mysql:
image: mysql:8.0.29
env:
MYSQL_ROOT_PASSWORD: pass
MYSQL_DATABASE: test
ports:
- "3306:3306"
options: >-
--health-cmd "mysqladmin ping -ppass"
--health-interval 10s
--health-start-period 10s
--health-timeout 5s
--health-retries 10
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ariga/setup-atlas@v0
with:
cloud-token: ${{ secrets.ATLAS_CLOUD_TOKEN }}
- uses: ariga/atlas-action/migrate/lint@v1
with:
dir: 'file://ent/migrate/migrations'
dir-name: 'my-project' # The name of the project in Atlas Cloud
dev-url: "mysql://root:pass@localhost:3306/dev"

Notice that running atlas migrate lint requires a clean dev-database which is provided by the services block in the example code above.