常用插件 #
本节介绍Jenkins中最常用的插件及其配置方法。
Git插件 #
安装 #
text
插件名称: Git
插件ID: git
配置 #
text
Manage Jenkins → Global Tool Configuration → Git
Name: Default
Path to Git executable: /usr/bin/git
使用 #
groovy
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git branch: 'main',
credentialsId: 'github-credentials',
url: 'https://github.com/user/repo.git'
}
}
}
}
高级配置 #
groovy
checkout([
$class: 'GitSCM',
branches: [[name: '*/main']],
extensions: [
[$class: 'CleanBeforeCheckout'],
[$class: 'CloneOption', depth: 1, noTags: true, shallow: true]
],
userRemoteConfigs: [[
url: 'https://github.com/user/repo.git',
credentialsId: 'github-credentials'
]]
])
GitHub插件 #
安装 #
text
插件名称: GitHub
插件ID: github
配置 #
text
Manage Jenkins → System → GitHub
GitHub Servers:
Name: GitHub
API URL: https://api.github.com
Credentials: github-token
☑ Manage hooks
使用 #
groovy
pipeline {
agent any
triggers {
githubPush()
}
stages {
stage('Build') {
steps {
checkout scm
}
}
}
}
GitLab插件 #
安装 #
text
插件名称: GitLab
插件ID: gitlab-plugin
配置 #
text
Manage Jenkins → System → GitLab
GitLab connections:
Name: GitLab
GitLab host URL: https://gitlab.example.com
Credentials: gitlab-token
使用 #
groovy
pipeline {
agent any
triggers {
gitlab(
triggerOnPush: true,
triggerOnMergeRequest: true,
branchFilterType: 'All'
)
}
stages {
stage('Build') {
steps {
checkout scm
}
}
}
}
Docker插件 #
安装 #
text
插件名称: Docker Pipeline
插件ID: docker-workflow
配置 #
text
Manage Jenkins → System → Docker
Docker Host URI: unix:///var/run/docker.sock
使用 #
groovy
pipeline {
agent {
docker {
image 'maven:3.8-openjdk-11'
args '-v $HOME/.m2:/root/.m2'
}
}
stages {
stage('Build') {
steps {
sh 'mvn clean package'
}
}
}
}
构建和推送镜像 #
groovy
pipeline {
agent any
stages {
stage('Build Image') {
steps {
script {
docker.build('myapp:${BUILD_NUMBER}')
}
}
}
stage('Push Image') {
steps {
script {
docker.withRegistry('https://registry.example.com', 'docker-credentials') {
docker.image('myapp:${BUILD_NUMBER}').push()
}
}
}
}
}
}
Kubernetes插件 #
安装 #
text
插件名称: Kubernetes
插件ID: kubernetes
配置 #
text
Manage Jenkins → Manage Nodes and Clouds → Configure Clouds
Add a new cloud: Kubernetes
Kubernetes URL: https://kubernetes.default
Kubernetes Namespace: jenkins
Credentials: kubeconfig
Jenkins URL: http://jenkins:8080
使用 #
groovy
pipeline {
agent {
kubernetes {
yaml '''
apiVersion: v1
kind: Pod
spec:
containers:
- name: maven
image: maven:3.8-openjdk-11
command:
- cat
tty: true
'''
}
}
stages {
stage('Build') {
steps {
container('maven') {
sh 'mvn clean package'
}
}
}
}
}
SonarQube插件 #
安装 #
text
插件名称: SonarQube Scanner for Jenkins
插件ID: sonar
配置 #
text
Manage Jenkins → System → SonarQube servers
Name: SonarQube
Server URL: http://sonarqube:9000
Server authentication token: sonar-token
使用 #
groovy
pipeline {
agent any
stages {
stage('SonarQube Analysis') {
steps {
withSonarQubeEnv('SonarQube') {
sh 'mvn sonar:sonar'
}
}
}
stage('Quality Gate') {
steps {
timeout(time: 5, unit: 'MINUTES') {
waitForQualityGate abortPipeline: true
}
}
}
}
}
Email Extension插件 #
安装 #
text
插件名称: Email Extension
插件ID: email-ext
配置 #
text
Manage Jenkins → System → Extended E-mail Notification
SMTP Server: smtp.example.com
Default Content Type: HTML
Default Recipients: team@example.com
使用 #
groovy
pipeline {
agent any
post {
success {
emailext(
subject: "Build Success: ${JOB_NAME} #${BUILD_NUMBER}",
body: '''${SCRIPT, template="groovy-html.template"}''',
to: 'team@example.com'
)
}
failure {
emailext(
subject: "Build Failed: ${JOB_NAME} #${BUILD_NUMBER}",
body: 'Build failed. Please check the logs.',
to: 'team@example.com'
)
}
}
}
Slack插件 #
安装 #
text
插件名称: Slack Notification
插件ID: slack
配置 #
text
Manage Jenkins → System → Slack
Workspace: your-workspace
Credential: slack-token
Default channel: #builds
使用 #
groovy
pipeline {
agent any
post {
success {
slackSend(
channel: '#builds',
color: 'good',
message: "Build Success: ${JOB_NAME} #${BUILD_NUMBER}"
)
}
failure {
slackSend(
channel: '#builds',
color: 'danger',
message: "Build Failed: ${JOB_NAME} #${BUILD_NUMBER}"
)
}
}
}
钉钉插件 #
安装 #
text
插件名称: DingTalk
插件ID: dingtalk
配置 #
text
Manage Jenkins → System → 钉钉配置
机器人名称: Jenkins Bot
Webhook: https://oapi.dingtalk.com/robot/send?access_token=xxx
使用 #
groovy
pipeline {
agent any
post {
success {
dingtalk(
robot: 'Jenkins Bot',
type: 'MARKDOWN',
title: '构建成功',
text: [
"### 构建成功",
"- 任务: ${JOB_NAME}",
"- 编号: #${BUILD_NUMBER}",
"- [查看详情](${BUILD_URL})"
]
)
}
}
}
Blue Ocean插件 #
安装 #
text
插件名称: Blue Ocean
插件ID: blueocean
使用 #
访问 /blue 路径进入Blue Ocean界面。
Role-based Strategy插件 #
安装 #
text
插件名称: Role-based Strategy
插件ID: role-strategy
配置 #
text
Manage Jenkins → System → Authorization
Strategy: Role-Based Strategy
Manage Roles:
Global Roles: admin, developer, viewer
Item Roles: project-admin, project-developer
Credentials Binding插件 #
安装 #
text
插件名称: Credentials Binding
插件ID: credentials-binding
使用 #
groovy
withCredentials([usernamePassword(
credentialsId: 'db-credentials',
usernameVariable: 'DB_USER',
passwordVariable: 'DB_PASS'
)]) {
sh 'mysql -u $DB_USER -p$DB_PASS'
}
下一步学习 #
小结 #
- Git插件是版本控制的基础
- Docker和Kubernetes插件支持容器化
- SonarQube插件用于代码质量分析
- 通知插件发送构建结果
- Blue Ocean提供现代化界面
- Role-based Strategy管理权限
最后更新:2026-03-28