Jenkins
Types of Jenkins Projects
| Type | Description |
|---|---|
| Freestyle | 単純
な単一
タスク、例
: run test |
| Pipeline | 完全 なデリバリーサイクル:test → build → … 単一 ブランチ向 け |
| Multi-branch Pipeline | Pipeline に似 ているが、複数 ブランチをサポート |
Credential Scopes
| Scope | Description |
|---|---|
| System | Jenkins Server でのみ利用 可能 、Jenkins Jobs には適用 されない |
| Global | グローバルにアクセス可能 |
| Project | プロジェクト限定 、multi-branch pipeline のみ |
Trigger Build
Push Notification
新 しい Commit 時 に Version Control が Jenkins に通知 :
- VCS に応 じた Jenkins Plugin をインストール
- Repository Server Hostname を設定
- Access Token / Credential を追加
Polling
Jenkins が定期的 にポーリング確認 。
Plugins
- Matrix Authorization Strategy
- Role-based Authorization Strategy
- Backup:
ThinBackup
System Administration
Backup
Filesystem Snapshots- 日常 バックアップ- バックアップ Plugins を使用
- Jenkins instance をバックアップする shell script を作成
Files To Backup
flowchart TD home[$JENKINS_HOME] config.xml[Configuration files] j[jobs] home ----> config.xml home ----> j
Pipelines
Jenkinsfile - Declarative
- 入門 しやすい
pipeline:最上位 でなければならないagent:どこで実行 するかを指定 (Build Agent)stages:作業 が行 われる場所stagesteps
pipeline {
agent any
stages {
stage("build") {
steps {
echo "building the application..."
script {
def test = 2 + 2 > 3 ? 'cool' : 'not cool'
echo test
}
}
}
stage("test") {
steps {
echo "testing the application..."
}
}
stage("deploy") {
steps {
echo "deploying the application..."
}
}
}
}Go Pipeline Demo
設定
:Manage Jenkins -> Tools -> Go
pipeline {
agent any
tools {
go 'Go1.19'
}
environment {
GO111MODULE='on'
}
stages {
stage('Test') {
steps {
git '<Repo URL>'
sh 'go test ./...'
}
}
}
}Docker Container Agent
pipeline {
agent {
docker { image 'golang:latest' }
}
stages {
stage('Dev') {
steps {
git 'https://github.com/kodekloudhub/go-webapp-sample.git'
sh 'go version'
}
}
}
}Scripted Pipeline
- 最初 の構文
- Groovy engine
- 高度 なスクリプト能力 、高 い柔軟性
node {
// scripted pipeline content
}Build Agents
- Windows
- Linux
- MacOS
- Docker
- 推奨 :Jenkins Server / Build Server を分離
Security
Access Control
- Authentication(認証 ):security realm を使用 して完了 。Security realm がユーザー ID とグループメンバーシップを決定 。
- Authorization(認可 ):authorization strategy によって完了 。ユーザー(直接 またはグループメンバーシップを通 じて)が権限 を持 っているかを制御 。
Common Mistakes
- Anyone can do anything
- Logged-in users can do anything
- Anonymous and authenticated users
- Built-in node(built-in node で実行 されるビルドは Jenkins プロセスと同 じファイルシステムアクセス権 を持 つ)
CLI
Service Management
sudo systemctl status jenkins
sudo systemctl start jenkins
sudo systemctl stop jenkins
sudo systemctl restart jenkinsSSH Authentication
SSH Key を設定 :
ls /home/mike/.ssh/
# jenkins_key jenkins_key.pubSSH key を Jenkins member setting に複製 :
cat /home/mike/.ssh/jenkins_key.pubSSH service を固定 ポートで待機 するように設定 :
curl -Lv http://localhost:8085/login 2>&1 | grep -i 'x-ssh-endpoint'SSH 経由 で Jenkins と対話 :
ssh -i /home/mike/.ssh/jenkins_key -l mike -p 8022 jenkins-server helpJenkins を再起動 :
ssh -i /home/mike/.ssh/jenkins_key -l mike -p 8022 jenkins-server safe-restartSetup
Linux (CentOS)
sudo yum install epel-release -y
sudo yum install java-11-openjdk -y
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo --no-check-certificate
sudo rpm --import http://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
sudo yum install jenkins -yポートを変更 :
sudo vi /lib/systemd/system/jenkins.serviceEnvironment="JENKINS_PORT=8090"初期 パスワードを取得 :
sudo cat /var/lib/jenkins/secrets/initialAdminPasswordDocker
docker pull jenkins/jenkins:lts-jdk17docker run -p 9992:8080 -p 50000:50000 -d \
-e JAVA_OPTS="-Djava.awt.headless=true -Dhudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT=true" \
-v "D:\Project-Workspace\Docker-Mount\jenkins_data_17_lts":/var/jenkins_home \
jenkins/jenkins:lts-jdk17- Port 8080:Jenkins Web UI
- Port 50000:Master / Slave 通信