ci-cd-pipelines について
ci-cd-pipelinesは、CI/CDパイプラインの設定とレビューに使う資料です。GitHub ActionsとGitLab CIの例やチェックリストを通じて、再利用可能なアクション、マトリックス、キャッシュ、シークレット管理を扱います。
主な機能
- 01GitHub Actionsワークフローの指針
- 02GitLab CIパイプラインの例
- 03再利用可能なGitHub Actions
- 04バージョンやシステムをまたぐマトリックスビルド
- 05パイプラインのキャッシュと並行実行
- 06シークレット管理とデプロイ保護
活用シーン
- 01GitHub Actionsワークフローの作成
- 02GitLab CIのステージ構成
- 03複数ワークフローで共通セットアップを再利用
- 04キャッシュとシークレット管理の確認
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ rohitg00/ awesome-claude-code-toolkit --skill ci-cd-pipelinesインストール後、Claude Code で/ci-cd-pipelinesを入力して読み込まれたか確認します。
詳細
CI/CD パイプライン
GitHub Actions ワークフロー
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npm run lint
- run: npm run typecheck
test:
runs-on: ubuntu-latest
needs: lint
services:
postgres:
image: postgres:16
env:
POSTGRES_DB: test
POSTGRES_USER: test
POSTGRES_PASSWORD: test
ports: ["5432:5432"]
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npm test -- --coverage
env:
DATABASE_URL: postgres://test:test@localhost:5432/test
- uses: codecov/codecov-action@v4
deploy:
runs-on: ubuntu-latest
needs: test
if: github.ref == 'refs/heads/main'
environment: production
steps:
- uses: actions/checkout@v4
- run: ./scripts/deploy.sh
古い実行をキャンセルするには、concurrency を使用します。ジョブの依存関係を定義するには、needs を使用します。
GitLab CI パイプライン
stages:
- validate
- test
- build
- deploy
variables:
NODE_IMAGE: node:22-alpine
lint:
stage: validate
image: $NODE_IMAGE
cache:
key: $CI_COMMIT_REF_SLUG
paths: [node_modules/]
script:
- npm ci
- npm run lint
- npm run typecheck
test:
stage: test
image: $NODE_IMAGE
services:
- postgres:16
variables:
POSTGRES_DB: test
DATABASE_URL: postgres://runner:@postgres:5432/test
script:
- npm ci
- npm test -- --coverage
coverage: '/Statements\s*:\s*(\d+\.?\d*)%/'
artifacts:
reports:
junit: coverage/junit.xml
coverage_report:
coverage_format: cobertura
path: coverage/cobertura.xml
build:
stage: build
image: docker:24
services: [docker:24-dind]
script:
- docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
rules:
- if: $CI_COMMIT_BRANCH == "main"
deploy:
stage: deploy
environment:
name: production
url: https://app.example.com
script:
- ./deploy.sh $CI_COMMIT_SHA
rules:
- if: $CI_COMMIT_BRANCH == "main"
when: manual
再利用可能な GitHub Action
# .github/actions/setup/action.yml
name: Setup
description: Install dependencies and cache
inputs:
node-version:
default: "22"
runs:
using: composite
steps:
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
cache: npm
- run: npm ci
shell: bash
# Usage in workflow
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup
- run: npm test
マトリックス戦略
test:
strategy:
fail-fast: false
matrix:
node: [20, 22]
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm ci && npm test
アンチパターン
- 実行間で依存関係(npm、pip、cargo)をキャッシュしない
- lint とテストを並列化できるのに、すべてのジョブを順番に実行する
- リポジトリまたは環境のシークレットではなく、ワークフローファイルにシークレットを保存する
- 急速なプッシュ時に冗長な CI 実行が発生する原因となる、
concurrencyグループの設定漏れ - マトリックスビルドで
fail-fast: falseを使用しない(1 つの失敗によってほかの実行もキャンセルされる) - 承認ゲートや環境保護ルールなしでデプロイする
チェックリスト
- CI 実行間で依存関係がキャッシュされる
- 同時実行グループによって古いパイプラインの実行がキャンセルされる
- lint、型チェック、テストが個別の並列化可能なジョブとして実行される
- テスト開始前にデータベースサービスのヘルスチェックが行われる
- カバレッジレポートがアップロードされ、追跡される
- 本番環境へのデプロイジョブに承認が必要である
- 再利用可能なアクション/テンプレートによって、共通のセットアップ手順が抽出される
- シークレットは CI プラットフォームに保存され、コードには決して保存されない
コメント
Markdown 対応