Jenkins完全ガイド
Jenkins完全ガイド
Section titled “Jenkins完全ガイド”Jenkinsの実践的な使い方を、実務で使える実装例とベストプラクティスとともに詳しく解説します。
1. Jenkinsとは
Section titled “1. Jenkinsとは”Jenkinsの特徴
Section titled “Jenkinsの特徴”Jenkinsは、オープンソースの自動化サーバーです。CI/CDパイプラインを構築・実行できます。
Jenkinsの特徴 ├─ オープンソース(無料) ├─ 高い柔軟性 ├─ 豊富なプラグイン ├─ 分散ビルド対応 └─ パイプライン as CodeなぜJenkinsを選ぶのか
Section titled “なぜJenkinsを選ぶのか”Jenkinsを選ぶべき場合:
- 高い柔軟性が必要
- オンプレミス環境が必要
- 豊富なプラグインが必要
- カスタマイズ性が重要
Jenkinsを選ばないべき場合:
- シンプルなCI/CDが必要(GitHub Actionsの方が適している場合がある)
- クラウドネイティブが必要(GitLab CI/CDの方が適している場合がある)
2. Jenkinsのインストールとセットアップ
Section titled “2. Jenkinsのインストールとセットアップ”Dockerでのインストール
Section titled “Dockerでのインストール”# Jenkinsの起動docker run -d \ --name jenkins \ -p 8080:8080 \ -p 50000:50000 \ -v jenkins_home:/var/jenkins_home \ jenkins/jenkins:lts
# 初期パスワードの取得docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPasswordLinuxでのインストール
Section titled “Linuxでのインストール”# Ubuntu/Debianwget -q -O - https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo apt-key add -sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'sudo apt-get updatesudo apt-get install jenkins
# サービスを開始sudo systemctl start jenkinssudo systemctl enable jenkins- ブラウザで
http://localhost:8080にアクセス - 初期パスワードを入力
- 推奨プラグインをインストール
- 管理者アカウントを作成
3. 基本的なジョブの作成
Section titled “3. 基本的なジョブの作成”Freestyle Project
Section titled “Freestyle Project”// Jenkins UIでの設定// 1. 新しいアイテム → Freestyle project// 2. ソースコード管理: Git// - Repository URL: https://github.com/user/repo.git// 3. ビルドステップ: Execute shell// - Command:// npm ci// npm test// npm run buildPipeline Project
Section titled “Pipeline Project”// Jenkinsfilepipeline { agent any
stages { stage('Checkout') { steps { checkout scm } }
stage('Install') { steps { sh 'npm ci' } }
stage('Test') { steps { sh 'npm test' } }
stage('Build') { steps { sh 'npm run build' } }
stage('Deploy') { steps { sh 'npm run deploy' } } }
post { always { junit 'test-results.xml' } success { echo 'Pipeline succeeded!' } failure { echo 'Pipeline failed!' } }}4. Pipeline as Code
Section titled “4. Pipeline as Code”Declarative Pipeline
Section titled “Declarative Pipeline”// Jenkinsfilepipeline { agent any
environment { NODE_VERSION = '18' NPM_TOKEN = credentials('npm-token') }
options { timeout(time: 1, unit: 'HOURS') retry(3) timestamps() }
triggers { cron('H */4 * * *') // 4時間ごと pollSCM('H * * * *') // 1時間ごと }
stages { stage('Checkout') { steps { checkout scm } }
stage('Install') { steps { sh ''' nvm use ${NODE_VERSION} npm ci ''' } }
stage('Test') { parallel { stage('Unit Tests') { steps { sh 'npm run test:unit' } } stage('Integration Tests') { steps { sh 'npm run test:integration' } } } }
stage('Build') { steps { sh 'npm run build' archiveArtifacts artifacts: 'dist/**', fingerprint: true } }
stage('Deploy') { when { branch 'main' } steps { sh 'npm run deploy:production' } } }
post { always { cleanWs() } success { emailext ( subject: "Pipeline Success: ${env.JOB_NAME} - ${env.BUILD_NUMBER}", body: "Pipeline succeeded!", to: "${env.CHANGE_AUTHOR_EMAIL}" ) } failure { emailext ( subject: "Pipeline Failed: ${env.JOB_NAME} - ${env.BUILD_NUMBER}", body: "Pipeline failed!", to: "${env.CHANGE_AUTHOR_EMAIL}" ) } }}Scripted Pipeline
Section titled “Scripted Pipeline”// Jenkinsfilenode { stage('Checkout') { checkout scm }
stage('Install') { sh 'npm ci' }
stage('Test') { def testResults = sh( script: 'npm test', returnStatus: true )
if (testResults != 0) { error('Tests failed!') } }
stage('Build') { sh 'npm run build' }
stage('Deploy') { if (env.BRANCH_NAME == 'main') { sh 'npm run deploy:production' } }}5. マルチブランチパイプライン
Section titled “5. マルチブランチパイプライン”マルチブランチパイプラインの設定
Section titled “マルチブランチパイプラインの設定”// Jenkinsfilepipeline { agent any
stages { stage('Build') { steps { sh 'npm ci' sh 'npm run build' } }
stage('Test') { steps { sh 'npm test' } }
stage('Deploy') { when { branch 'main' } steps { sh 'npm run deploy:production' } } }}// Jenkins UIでの設定// 1. 新しいアイテム → Multibranch Pipeline// 2. Branch Sources: Git// - Project Repository: https://github.com/user/repo.git// - Behaviors: Discover branches// 3. Build Configuration: Mode: by Jenkinsfile6. 分散ビルド
Section titled “6. 分散ビルド”Agentの設定
Section titled “Agentの設定”// Jenkinsfilepipeline { agent { label 'linux && docker' }
stages { stage('Build') { steps { sh 'npm run build' } } }}Agentの追加
Section titled “Agentの追加”// Jenkins UIでの設定// 1. Manage Jenkins → Manage Nodes and Clouds// 2. New Node// 3. Node名を入力// 4. Remote root directory: /home/jenkins// 5. Launch method: Launch agent agents via SSH// - Host: agent-server-ip// - Credentials: SSH認証情報7. プラグイン
Section titled “7. プラグイン”よく使われるプラグイン
Section titled “よく使われるプラグイン”// 推奨プラグイン- Git Plugin- Pipeline Plugin- Docker Pipeline Plugin- Slack Notification Plugin- Email Extension Plugin- JUnit Plugin- HTML Publisher Plugin- Blue Ocean Pluginプラグインのインストール
Section titled “プラグインのインストール”# コマンドラインでのインストールjenkins-plugin-cli --plugins \ git \ pipeline-stage-view \ docker-workflow \ slack8. 認証とセキュリティ
Section titled “8. 認証とセキュリティ”ユーザー管理
Section titled “ユーザー管理”// Jenkins UIでの設定// 1. Manage Jenkins → Configure Global Security// 2. Security Realm: Jenkins' own user database// 3. Authorization: Matrix-based security// 4. ユーザーに権限を付与認証情報の管理
Section titled “認証情報の管理”// Jenkins UIでの設定// 1. Manage Jenkins → Credentials// 2. Add Credentials// 3. Kind: Secret text / Username with password / SSH Username with private key// 4. IDを設定(例: github-token)
// Jenkinsfileでの使用withCredentials([string(credentialsId: 'github-token', variable: 'GITHUB_TOKEN')]) { sh 'git push https://${GITHUB_TOKEN}@github.com/user/repo.git'}9. 実践的なベストプラクティス
Section titled “9. 実践的なベストプラクティス”パイプラインの共有ライブラリ
Section titled “パイプラインの共有ライブラリ”def call(Map config) { stage('Deploy') { sh """ echo "Deploying to ${config.environment}" npm run deploy:${config.environment} """ }}
// Jenkinsfileでの使用@Library('shared-library') _pipeline { agent any stages { stage('Deploy') { steps { deploy environment: 'production' } } }}パラメータ化ビルド
Section titled “パラメータ化ビルド”// Jenkinsfilepipeline { agent any
parameters { choice( name: 'ENVIRONMENT', choices: ['dev', 'staging', 'production'], description: 'Deployment environment' ) string( name: 'VERSION', defaultValue: '1.0.0', description: 'Application version' ) }
stages { stage('Deploy') { steps { sh "npm run deploy:${params.ENVIRONMENT} --version=${params.VERSION}" } } }}// Jenkinsfilepipeline { agent any
stages { stage('Test') { parallel { stage('Unit Tests') { steps { sh 'npm run test:unit' } } stage('Integration Tests') { steps { sh 'npm run test:integration' } } stage('E2E Tests') { steps { sh 'npm run test:e2e' } } } } }}10. よくある問題と解決方法
Section titled “10. よくある問題と解決方法”問題1: ビルドが遅い
Section titled “問題1: ビルドが遅い”// 解決: 並列実行とキャッシュの使用pipeline { agent any
options { skipDefaultCheckout() }
stages { stage('Cache') { steps { cache(maxCacheSize: 250, caches: [ arbitraryFileCache( path: 'node_modules', fingerprint: [ includes: 'package-lock.json' ] ) ]) { sh 'npm ci' } } } }}問題2: 認証情報の漏洩
Section titled “問題2: 認証情報の漏洩”// 解決: Credentials Binding Pluginの使用withCredentials([usernamePassword(credentialsId: 'db-credentials', usernameVariable: 'DB_USER', passwordVariable: 'DB_PASS')]) { sh 'echo $DB_USER:$DB_PASS'}問題3: パイプラインの複雑化
Section titled “問題3: パイプラインの複雑化”// 解決: 共有ライブラリの使用@Library('shared-library') _pipeline { agent any stages { stage('Build') { steps { build() } } stage('Test') { steps { test() } } stage('Deploy') { steps { deploy() } } }}Jenkins完全ガイドのポイント:
- Pipeline as Code: Jenkinsfileによるパイプライン定義
- 分散ビルド: Agentによる並列実行
- プラグイン: 豊富な機能拡張
- 認証とセキュリティ: ユーザー管理と認証情報管理
- ベストプラクティス: 共有ライブラリ、パラメータ化、並列実行
適切なJenkinsの使用により、柔軟で強力なCI/CDシステムを構築できます。