GitLab Advanced CI/CD - ハンズオンラボ: パイプラインテストの最適化
このラボの目的は、アプリケーションのテストを設定できるさまざまな方法を探ることです。
完了までの推定時間: 15 分
目標
- 失敗後にパイプラインを停止する
- ユニットテストのレポート
このラボでは、アプリケーションのテストを設定できるさまざまな方法を探ります。現在、プロジェクトには次のテストセットアップがあります:
stages:
- deps
- test
default:
image: node:latest
install deps:
stage: deps
script:
- npm install jest
cache:
key: $CI_COMMIT_REF_SLUG
paths:
- node_modules
test binarysearch:
stage: test
script:
- node_modules/.bin/jest binarysearch.test.js
cache:
key: $CI_COMMIT_REF_SLUG
paths:
- node_modules
test linearsearch:
stage: test
script:
- node_modules/.bin/jest linearsearch.test.js
cache:
key: $CI_COMMIT_REF_SLUG
paths:
- node_modules
このラボでは、単一のジョブが失敗した場合にテストパイプラインを確実に実行できるようにする方法を探ります。また、テストジョブにテストレポートを追加する方法も確認します。
タスク A. 失敗後にパイプラインを停止する
この例では、テストの 1 つが失敗した場合にパイプラインをキャンセルする方法を見てみましょう。
Nodeプロジェクトリポジトリに移動します。Build > Pipeline Editor を選択します。
.gitlab-ci.ymlファイルの stages セクションのすぐ下に、ジョブを自動キャンセルするワークフローを追加します。workflow: auto_cancel: on_job_failure: allこの設定では、いずれかのジョブが失敗すると、パイプライン全体が失敗します。これをテストするために、意図的に失敗するテストジョブを作成できます。
test fail: stage: test script: - jet test.jsCommit changes を選択します。
パイプラインが失敗したジョブをどのように処理するかを見てみましょう。
左サイドバーで、Build > Pipelines を選択します。
最新のパイプラインを選択して、ジョブを観察します。
test failジョブが失敗すると、他のジョブがキャンセルされ、灰色のスラッシュアイコンが表示されることに注目してください。自動キャンセルが機能していることを確認できたので、失敗するジョブを削除しましょう。
リポジトリに移動します。
Build > Pipeline Editor を選択します。
test failジョブを削除します。.gitlab-ci.ymlファイルは次のようになります:stages: - deps - test workflow: auto_cancel: on_job_failure: all default: image: node:latest install deps: stage: deps script: - npm install jest cache: key: $CI_COMMIT_REF_SLUG paths: - node_modules test binarysearch: stage: test script: - node_modules/.bin/jest binarysearch.test.js cache: key: $CI_COMMIT_REF_SLUG paths: - node_modules test linearsearch: stage: test script: - node_modules/.bin/jest linearsearch.test.js cache: key: $CI_COMMIT_REF_SLUG paths: - node_modulesCommit changes を選択します。
タスク B. テストレポートを追加する
このタスクでは、テストジョブにテストレポートを追加します。
まだ Pipeline Editor を表示していない場合は、Build > Pipeline Editor に移動します。
test binarysearchとtest linearsearchジョブのjestコマンドを調整して、コマンドにtestResultsProcessorを追加します。これは--ci --testResultsProcessor=jest-junitフラグをコマンドに追加することで実現できます。--ciフラグにより、Jest は CI 環境で実行されていると見なします。これを機能させるには、install depsにjest-junitを追加してインストールする必要もあります。変更後のジョブの例を以下に示します:install deps: stage: deps script: - npm install jest jest-junit cache: key: $CI_COMMIT_REF_SLUG paths: - node_modules test binarysearch: stage: test script: - node_modules/.bin/jest --ci --testResultsProcessor=jest-junit binarysearch.test.js cache: key: $CI_COMMIT_REF_SLUG paths: - node_modules test linearsearch: stage: test script: - node_modules/.bin/jest --ci --testResultsProcessor=jest-junit linearsearch.test.js cache: key: $CI_COMMIT_REF_SLUG paths: - node_modulesテスト結果をパイプラインからアクセスできるようにするには、JUnit ファイルに保存する必要があります。そのために、両方のテストの
scriptキーワードの後に次のコードスニペットを追加する必要があります:artifacts: when: always reports: junit: junit.xmlテストは次のようになります:
test binarysearch: stage: test script: - node_modules/.bin/jest --ci --testResultsProcessor=jest-junit binarysearch.test.js artifacts: when: always reports: junit: junit.xml cache: key: $CI_COMMIT_REF_SLUG paths: - node_modules test linearsearch: stage: test script: - node_modules/.bin/jest --ci --testResultsProcessor=jest-junit linearsearch.test.js artifacts: when: always reports: junit: junit.xml cache: key: $CI_COMMIT_REF_SLUG paths: - node_modulesこれらの変更を行った後、Commit changes を選択します。
左サイドバーで、Build > Pipelines を選択します。
最新のパイプラインを選択します。
テストが完了するまで待ちます。テストジョブの完了後にページを更新して、
Testsタブを選択します。タブにテスト結果のレポートが表示されます。
ラボガイドの完了
このラボ演習が完了しました。このコースの他のラボガイドを参照できます。
ご提案は?
ラボへの変更をご希望の場合は、マージリクエストで変更内容を送信してください。
