CircleCI チートシート
CircleCI チートシート
インストール
| プラットフォーム | コマンド |
|---|---|
| Linux (curl) | `curl -fLSs https://raw.githubusercontent.com/CircleCI-Public/circleci-cli/master/install.sh \ |
| Linux (snap) | sudo snap install circleci |
| macOS (Homebrew) | brew install circleci |
| macOS (curl) | `curl -fLSs https://raw.githubusercontent.com/CircleCI-Public/circleci-cli/master/install.sh \ |
| Windows (Chocolatey) | choco install circleci-cli |
| Windows (Scoop) | scoop install circleci |
| Verify Installation | circleci version |
| Initial Setup | circleci setup |
| Update CLI | circleci update |
基本コマンド
| コマンド | 説明 |
|---|---|
circleci config validate | Validate your .circleci/config.yml file |
circleci config validate -c path/to/config.yml | 特定の設定ファイルを検証する |
circleci config process .circleci/config.yml | 設定を処理し、展開する(orbs を解決) |
circleci local execute | Dockerを使用してローカルでジョブを実行する |
circleci local execute --job job-name | ローカルで特定のジョブを実行する |
circleci follow | フォローしているプロジェクトをすべて表示 |
circleci follow github/org/repo | 特定のプロジェクトに従う |
circleci project info github/org/repo | プロジェクト情報を取得 |
circleci pipeline run github/org/repo | 新しいパイプラインをトリガー |
circleci pipeline run github/org/repo --branch develop | 特定のブランチでパイプラインをトリガー |
circleci pipeline list github/org/repo | プロジェクトの最近のパイプラインをリスト |
circleci pipeline get <pipeline-id> | 特定のパイプラインについての詳細を取得 |
circleci build list github/org/repo | プロジェクトの最近のビルドをリスト |
circleci build get <build-number> | 特定のビルドに関する情報を取得する |
circleci build cancel <build-number> | 実行中のビルドをキャンセル |
高度な使用法
| コマンド | 説明 |
|---|---|
circleci pipeline run github/org/repo --parameters '{"key":"value"}' | パラメータを使用してパイプラインをトリガー |
circleci local execute --env VAR1=value1 --env VAR2=value2 | 環境変数を使用してローカルで実行 |
circleci context list github org-name | 組織のすべてのコンテキストを一覧表示 |
circleci context create github org-name context-name | 新しいコンテキストを作成 |
circleci context show github org-name context-name | コンテキストの詳細を表示 |
circleci context delete github org-name context-name | コンテキストを削除 |
circleci context env-var list github org-name context-name | コンテキストの環境変数を一覧表示する |
circleci context env-var store github org-name context-name VAR_NAME | コンテキストに環境変数を保存する |
circleci orb list | 利用可能な公開 orbs をすべて一覧表示 |
circleci orb search <query> | キーワードでオーブを検索 |
circleci orb info namespace/orb@version | orbに関する詳細な情報を取得する |
circleci orb source namespace/orb@version | orbのソースコードを表示 |
circleci orb create namespace/orb | 新しいorb を作成 |
circleci orb publish path/to/orb.yml namespace/orb@dev:first | orbの開発バージョンを公開 |
circleci orb publish promote namespace/orb@dev:first patch | Orbを本番環境にプロモーション(パッチバージョン) |
circleci orb validate path/to/orb.yml | Orb 設定を検証する |
circleci api graphql-query --query '{ me { id name } }' | GraphQL APIクエリを実行する |
circleci api get /me | REST API の GET リクエストを作成する |
circleci diagnostic | CLIセットアップの診断チェックを実行 |
circleci config pack src/ > orb.yml | Orbソースファイルを単一のYAMLにパック |
設定
基本的な設定ファイルの構造
CircleCIの設定ファイルは.circleci/config.ymlリポジトリのルートに配置されています。
version: 2.1
# Define reusable execution environments
executors:
node-executor:
docker:
- image: cimg/node:18.16
resource_class: medium
working_directory: ~/project
# Define reusable command sequences
commands:
install-deps:
steps:
- restore_cache:
keys:
- v1-deps-{{ checksum "package-lock.json" }}
- run: npm ci
- save_cache:
key: v1-deps-{{ checksum "package-lock.json" }}
paths:
- node_modules
# Define jobs
jobs:
build:
executor: node-executor
steps:
- checkout
- install-deps
- run: npm run build
- persist_to_workspace:
root: .
paths:
- dist
test:
executor: node-executor
steps:
- checkout
- install-deps
- run: npm test
- store_test_results:
path: test-results
# Define workflows
workflows:
build-and-test:
jobs:
- build
- test:
requires:
- build
Dockerエグゼキュータの設定
jobs:
build:
docker:
- image: cimg/python:3.11
auth:
username: $DOCKERHUB_USERNAME
password: $DOCKERHUB_PASSWORD
environment:
FLASK_ENV: development
- image: cimg/postgres:14.0
environment:
POSTGRES_USER: testuser
POSTGRES_DB: testdb
resource_class: large
steps:
- checkout
- run: python app.py
マシンエグゼキュータの設定
jobs:
build:
machine:
image: ubuntu-2204:2023.04.2
docker_layer_caching: true
resource_class: large
steps:
- checkout
- run: docker build -t myapp .
Orbsの使用
version: 2.1
orbs:
node: circleci/node@5.1.0
aws-cli: circleci/aws-cli@3.1.0
slack: circleci/slack@4.12.0
jobs:
deploy:
executor: node/default
steps:
- checkout
- node/install-packages
- aws-cli/setup
- run: npm run deploy
- slack/notify:
event: pass
template: success_tagged_deploy_1
ワークフローのフィルタとスケジューリング
workflows:
version: 2
build-deploy:
jobs:
- build
- test:
requires:
- build
- deploy:
requires:
- test
filters:
branches:
only:
- main
- /release\/.*/
tags:
only: /^v.*/
nightly:
triggers:
- schedule:
cron: "0 0 * * *"
filters:
branches:
only: main
jobs:
- test
並列処理とテストの分割
jobs:
test:
docker:
- image: cimg/node:18.16
parallelism: 4
steps:
- checkout
- run: npm ci
- run:
name: Run Tests
command: |
TESTFILES=$(circleci tests glob "tests/**/*.test.js" | \
circleci tests split --split-by=timings)
npm test $TESTFILES
- store_test_results:
path: test-results
リソースクラス
jobs:
small-job:
docker:
- image: cimg/base:stable
resource_class: small # 1 vCPU, 2GB RAM
large-job:
docker:
- image: cimg/base:stable
resource_class: large # 4 vCPU, 8GB RAM
xlarge-job:
docker:
- image: cimg/base:stable
resource_class: xlarge # 8 vCPU, 16GB RAM
一般的なユースケース
ユースケース1: マルチステージNode.jsアプリケーションパイプライン
version: 2.1
orbs:
node: circleci/node@5.1.0
jobs:
build:
executor: node/default
steps:
- checkout
- node/install-packages:
pkg-manager: npm
- run:
name: Build Application
command: npm run build
- persist_to_workspace:
root: .
paths:
- dist
- node_modules
test:
executor: node/default
steps:
- checkout
- attach_workspace:
at: .
- run:
name: Run Unit Tests
command: npm test
- store_test_results:
path: test-results
deploy-staging:
executor: node/default
steps:
- attach_workspace:
at: .
- run:
name: Deploy to Staging
command: npm run deploy:staging
workflows:
build-test-deploy:
jobs:
- build
- test:
requires:
- build
- deploy-staging:
requires:
- test
filters:
branches:
only: develop
ユースケース2: Dockerイメージのビルドとプッシュ
version: 2.1
orbs:
docker: circleci/docker@2.2.0
jobs:
build-and-push:
executor: docker/docker
steps:
- setup_remote_docker:
docker_layer_caching: true
- checkout
- docker/check
- docker/build:
image: myorg/myapp
tag: ${CIRCLE_SHA1},latest
- docker/push:
image: myorg/myapp
tag: ${CIRCLE_SHA1},latest
workflows:
build-deploy:
jobs:
- build-and-push:
context: docker-hub-creds
filters:
branches:
only: main
ユースケース3: テスト分割による並列テスト
version: 2.1
jobs:
test:
docker:
- image: cimg/python:3.11
parallelism: 8
steps:
- checkout
- run: pip install -r requirements.txt
- run:
name: Run Tests in Parallel
command: |
TESTFILES=$(circleci tests glob "tests/**/test_*.py" | \
circleci tests split --split-by=timings)
pytest $TESTFILES \
--junitxml=test-results/junit.xml \
--cov=app \
--cov-report=html
- store_test_results:
path: test-results
- store_artifacts:
path: htmlcov
workflows:
test:
jobs:
- test
ユースケース4: パラメータを使用した条件付きワークフロー
version: 2.1
parameters:
run-integration-tests:
type: boolean
default: false
deployment-environment:
type: string
default: "staging"
jobs:
unit-test:
docker:
- image: cimg/node:18.16
steps:
- checkout
- run: npm ci
- run: npm run test:unit
integration-test:
docker:
- image: cimg/node:18.16
steps:
- checkout
- run: npm ci
- run: npm run test:integration
deploy:
docker:
- image: cimg/node:18.16
parameters:
environment:
type: string
steps:
- checkout
- run: npm ci
- run: npm run deploy:<< parameters.environment >>
workflows:
test-and-deploy:
jobs:
- unit-test
- integration-test:
when: << pipeline.parameters.run-integration-tests >>
- deploy:
environment: << pipeline.parameters.deployment-environment >>
requires:
- unit-test
filters:
branches:
only: main
ユースケース5: パスフィルタリングを使用したモノレポ
version: 2.1
setup: true
orbs:
path-filtering: circleci/path-filtering@0.1.3
workflows:
setup-workflow:
jobs:
- path-filtering/filter:
base-revision: main
config-path: .circleci/continue-config.yml
mapping: |
services/api/.* api-build true
services/web/.* web-build true
packages/.* all-build true
continue-config.yml:
version: 2.1
parameters:
api-build:
type: boolean
default: false
web-build:
type: boolean
default: false
all-build:
type: boolean
default: false
jobs:
build-api:
docker:
- image: cimg/node:18.16
steps:
- checkout
- run: cd services/api && npm ci && npm run build
build-web:
docker:
- image: cimg/node:18.16
steps:
- checkout
- run: cd services/web && npm ci && npm run build
workflows:
api-workflow:
when: << pipeline.parameters.api-build >>
jobs:
- build-api
web-workflow:
when: << pipeline.parameters.web-build >>
jobs:
- build-web
ベストプラクティス
-
一般的なタスクにOrbsを使用: 標準化された統合(AWS、Docker、Slack)のためにCircleCI orbsを活用し、設定の複雑さとメンテナンスの負担を軽減します。
-
戦略的にキャッシングを実装: チェックサムベースのキー(
{{ checksum "package-lock.json" }})を使用して依存関係をキャッシュし、依存関係が変更された際にキャッシュを無効化しながらビルドを高速化します。 -
リソースクラスを最適化: 各ジョブに適切なリソースクラスを選択します。単純なタスクには小さいインスタンスを使用し、コンパイルや複雑なテストなどの計算集中型の処理には大きいインスタンスを確保します。
-
Dockerレイヤーキャッシングを有効化: Dockerイメージをビルドするジョブの場合、
Note: Some placeholders (3-19) were left untranslated as no specific text was provided. I translated the surrounding context and maintained the structure.docker_layer_caching: trueビルド時間を大幅に短縮するために、変更されていないレイヤーを再利用する。
circleci tests splitテストの並列実行のための分割: 使用
--split-by=timings過去の実行時間に基づいて、並列コンテナ間でテストを分散し、効率を最大化する。
persist_to_workspaceワークスペースを使用して、ビルド成果物を保持し、
attach_workspace後続のジョブで再ビルドせず、時間を節約し、一貫性を確保する。
-
ワークフローフィルターの実装: ジョブの実行を制御するブランチとタグのフィルターを使用し、不要なデプロイメントを防ぎ、クレジットを節約しながらセキュリティを維持する。
-
コンテキストでシークレットを保存: プロジェクト環境変数ではなく、CircleCI コンテキストで機密性の高い認証情報を管理し、セキュリティ、アクセス制御、プロジェクト間の再利用性を向上させる。
-
設定をローカルで検証: 常に
circleci config validateを実行し、circleci local executeでジョブをローカルでテストして、早期に構文エラーや設定の問題を発見する。
トラブルシューティング
| 問題 | ソリューション |
|---|---|
| ”Config file not found” error | Ensure .circleci/config.yml exists in repository root. Run circleci config validate to check file location and syntax. |
| Local execution fails with Docker errors | Verify Docker is running: docker ps. Ensure the CLI has access to Docker socket. On Linux, add user to docker group: sudo usermod -aG docker $USER. |
| ”Invalid configuration” during validation | Run circleci config process .circleci/config.yml to see expanded config and identify syntax errors. Check YAML indentation (use spaces, not tabs). |
| Jobs not triggering on push | Verify project is followed: circleci follow github/org/repo. Check workflow filters aren’t excluding your branch. Ensure webhook is configured in VCS settings. |
| Workspace attachment fails | Ensure persist_to_workspace in upstream job completes successfully. Verify root and paths match between persist and attach. Check job dependencies in workflow. |
| Cache not restoring | Verify cache key matches between save_cache and restore_cache. Use fallback keys: keys: [v1-{{ checksum "file" }}, v1-]. Check cache hasn’t expired (30 days). |
| Authentication errors with CLI | Re-run circleci setup with a valid API token. Generate new token at https://app.circleci.com/settings/user/tokens. Verify token has correct permissions. |
| Parallel test splitting not working | Ensure test results are stored with store_test_results. Use glob patterns that match your test files. Verify parallelism is set greater than 1. |
| Out of memory errors | Increase resource_class to large or xlarge. Optimize memory-intensive operations. Check for memory leaks in application code. |
| Context environment variables not available | Verify job uses correct context in workflow: context: context-name. Check user has access to context in organization settings. Ensure variable names don’t conflict. |
| Orb import fails | Verify orb exists: circleci orb info namespace/orb@version. Check version is valid. For private orbs, ensure organization has access and use --org-id flag. |
- run: circleci-agent step haltを追加する。SSHを有効にしてジョブを再実行。タイムアウト前の10分以内に提供されたSSHコマンドを使用。 |