コンテンツにスキップ

CI パイプライン設定の問題点を見つけよ

原文

Your teammate's CI pipeline config:

deploy:
  script:
    - npm install
    - npm run build
    - npm run deploy
  only:
    - main

Two things missing that could seriously hurt you in production. Can you spot them?

要約

npm install → build → deploy という最小構成の CI パイプラインには少なくとも2つの重大な欠陥がある。テストの欠如とロールバック/失敗時の対応がない点が主な問題。

回答

欠けているもの2つ

1. テスト(Testing Step)

deploy:
  script:
    - npm install
    - npm run lint       # ← 追加
    - npm run test       # ← 追加:ここで失敗したらデプロイしない
    - npm run build
    - npm run deploy

テストなしで main ブランチへのプッシュが即デプロイされる → バグが本番に直行する。

2. ロールバック戦略 / 失敗時の安全網

deploy:
  script:
    - npm install
    - npm run test
    - npm run build
    - npm run deploy
  only:
    - main
  on_failure:           # ← 追加:失敗時の通知・ロールバック
    - notify_slack
    - rollback_deploy

デプロイが失敗しても自動ロールバックがなければ本番が壊れたまま放置される。

解説

改善後の完全なパイプライン例

stages:
  - test
  - build
  - deploy

test:
  stage: test
  script:
    - npm ci                  # install の代わりに ci(ロックファイル厳守)
    - npm run lint
    - npm run test
  only:
    - main

build:
  stage: build
  script:
    - npm run build
  artifacts:
    paths:
      - dist/
  only:
    - main

deploy:
  stage: deploy
  script:
    - npm run deploy
  environment:
    name: production          # ← 環境名の明示
  only:
    - main
  when: on_success            # ← 前段が成功した場合のみデプロイ

よくある追加の見落とし

問題 対策
npm install ではなく npm ci を使うべき ci はロックファイルを厳守、クリーンインストール
環境変数・シークレット管理がない CI/CDのシークレット管理機能を使う(GitLab Variables, GitHub Secrets等)
キャッシュがない node_modules のキャッシュで実行時間を短縮
デプロイ通知がない Slack/PagerDuty等への成功・失敗通知

→ 関連: CI/CD概念難易度K8Sデプロイ設計判断

リンク