Plugins Jenkins
Introduction
Les plugins étendent les fonctionnalités de Jenkins. Il existe plus de 1800 plugins disponibles.
Gestion des plugins
Via l'interface
- Manage Jenkins > Manage Plugins
- Onglets :
- Updates : Mises à jour disponibles
- Available : Plugins à installer
- Installed : Plugins installés
- Advanced : Upload manuel
Via CLI
# Lister les plugins installés
java -jar jenkins-cli.jar -s http://localhost:8080/ list-plugins
# Installer un plugin
java -jar jenkins-cli.jar -s http://localhost:8080/ install-plugin git
# Installer plusieurs plugins
java -jar jenkins-cli.jar -s http://localhost:8080/ install-plugin \
git docker-workflow kubernetes blueocean
# Redémarrer Jenkins
java -jar jenkins-cli.jar -s http://localhost:8080/ safe-restart
Via Docker
FROM jenkins/jenkins:lts-jdk17
# Copier la liste des plugins
COPY plugins.txt /usr/share/jenkins/ref/plugins.txt
# Installer les plugins
RUN jenkins-plugin-cli --plugin-file /usr/share/jenkins/ref/plugins.txt
# plugins.txt
git:latest
docker-workflow:latest
kubernetes:latest
blueocean:latest
configuration-as-code:latest
credentials-binding:latest
pipeline-stage-view:latest
Plugins essentiels par catégorie
Source Control
| Plugin | Description |
|---|---|
| Git | Intégration Git |
| GitHub | Webhooks, status checks |
| GitLab | Intégration GitLab |
| Bitbucket | Intégration Bitbucket |
// Exemple Git
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git branch: 'main',
url: 'https://github.com/org/repo.git',
credentialsId: 'github-creds'
}
}
}
}
Build Tools
| Plugin | Description |
|---|---|
| Maven Integration | Support Maven |
| Gradle | Support Gradle |
| NodeJS | Support Node.js/npm |
| Go | Support Go |
// NodeJS Plugin
pipeline {
agent any
tools {
nodejs 'Node-18'
}
stages {
stage('Build') {
steps {
sh 'npm ci'
sh 'npm run build'
}
}
}
}
Containers
| Plugin | Description |
|---|---|
| Docker Pipeline | Build/push images Docker |
| Docker | Agents Docker |
| Kubernetes | Agents K8s dynamiques |
// Docker Pipeline
pipeline {
agent any
stages {
stage('Build Image') {
steps {
script {
def image = docker.build("my-app:${BUILD_NUMBER}")
docker.withRegistry('https://registry.example.com', 'docker-creds') {
image.push()
image.push('latest')
}
}
}
}
}
}
Quality
| Plugin | Description |
|---|---|
| SonarQube Scanner | Analyse de code |
| Warnings Next Generation | Rapports d'analyse |
| JaCoCo | Couverture de code |
| JUnit | Résultats de tests |
// SonarQube
pipeline {
agent any
environment {
SONAR_TOKEN = credentials('sonar-token')
}
stages {
stage('SonarQube Analysis') {
steps {
withSonarQubeEnv('SonarQube') {
sh '''
sonar-scanner \
-Dsonar.projectKey=my-project \
-Dsonar.sources=src \
-Dsonar.login=${SONAR_TOKEN}
'''
}
}
}
stage('Quality Gate') {
steps {
timeout(time: 10, unit: 'MINUTES') {
waitForQualityGate abortPipeline: true
}
}
}
}
}
Notifications
| Plugin | Description |
|---|---|
| Slack Notification | Messages Slack |
| Email Extension | Emails avancés |
| Microsoft Teams | Messages Teams |
// Slack
post {
success {
slackSend(
channel: '#builds',
color: 'good',
message: "✅ *${JOB_NAME}* #${BUILD_NUMBER} succeeded\n${BUILD_URL}"
)
}
failure {
slackSend(
channel: '#builds',
color: 'danger',
message: "❌ *${JOB_NAME}* #${BUILD_NUMBER} failed\n${BUILD_URL}"
)
}
}
// Email Extension
post {
failure {
emailext(
subject: "FAILED: ${JOB_NAME} #${BUILD_NUMBER}",
body: '''
<h2>Build Failed</h2>
<p>Job: ${JOB_NAME}</p>
<p>Build: ${BUILD_NUMBER}</p>
<p>URL: ${BUILD_URL}</p>
<p>Console: ${BUILD_URL}console</p>
''',
mimeType: 'text/html',
to: '[email protected]',
attachLog: true
)
}
}
Credentials
| Plugin | Description |
|---|---|
| Credentials Binding | Injection de secrets |
| HashiCorp Vault | Secrets Vault |
| AWS Credentials | Credentials AWS |
// Credentials Binding
pipeline {
agent any
stages {
stage('Deploy') {
steps {
withCredentials([
usernamePassword(
credentialsId: 'docker-hub',
usernameVariable: 'DOCKER_USER',
passwordVariable: 'DOCKER_PASS'
),
string(
credentialsId: 'api-key',
variable: 'API_KEY'
),
file(
credentialsId: 'kubeconfig',
variable: 'KUBECONFIG'
)
]) {
sh 'echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin'
sh 'kubectl --kubeconfig=$KUBECONFIG apply -f k8s/'
}
}
}
}
}
Pipeline
| Plugin | Description |
|---|---|
| Pipeline | Support pipelines |
| Pipeline: Stage View | Visualisation stages |
| Blue Ocean | Interface moderne |
| Pipeline Utility Steps | Utilitaires |
Blue Ocean
Interface moderne pour Jenkins.
Installation
# Via CLI
java -jar jenkins-cli.jar install-plugin blueocean
# Via plugins.txt
blueocean:latest
Fonctionnalités
- Interface utilisateur moderne
- Éditeur visuel de pipelines
- Visualisation des branches
- Intégration GitHub/GitLab
- Logs améliorés
Accès
http://jenkins:8080/blue
Configuration as Code (JCasC)
Plugin
# plugins.txt
configuration-as-code:latest
Configuration des plugins
# jenkins.yaml
jenkins:
systemMessage: "Jenkins configured with JCasC"
unclassified:
# SonarQube
sonarGlobalConfiguration:
buildWrapperEnabled: true
installations:
- name: "SonarQube"
serverUrl: "https://sonar.example.com"
credentialsId: "sonar-token"
# Slack
slackNotifier:
teamDomain: "mycompany"
tokenCredentialId: "slack-token"
room: "#jenkins"
# Git
gitSCM:
globalConfigName: "Jenkins"
globalConfigEmail: "[email protected]"
tool:
# NodeJS
nodejs:
installations:
- name: "Node-18"
properties:
- installSource:
installers:
- nodeJSInstaller:
id: "18.19.0"
npmPackagesRefreshHours: 72
- name: "Node-20"
properties:
- installSource:
installers:
- nodeJSInstaller:
id: "20.10.0"
# Maven
maven:
installations:
- name: "Maven-3.9"
properties:
- installSource:
installers:
- maven:
id: "3.9.6"
# Docker
dockerTool:
installations:
- name: "Docker"
properties:
- installSource:
installers:
- fromDocker:
version: "latest"
Plugin Kubernetes
Configuration
# jenkins.yaml
jenkins:
clouds:
- kubernetes:
name: "kubernetes"
serverUrl: "https://kubernetes.default"
namespace: "jenkins"
jenkinsUrl: "http://jenkins:8080"
jenkinsTunnel: "jenkins-agent:50000"
containerCap: 10
podLabels:
- key: "jenkins"
value: "agent"
templates:
- name: "default"
label: "kubernetes-agent"
nodeUsageMode: NORMAL
containers:
- name: "jnlp"
image: "jenkins/inbound-agent:latest"
workingDir: "/home/jenkins/agent"
resourceRequestCpu: "500m"
resourceRequestMemory: "512Mi"
volumes:
- hostPathVolume:
hostPath: "/var/run/docker.sock"
mountPath: "/var/run/docker.sock"
Utilisation
pipeline {
agent {
kubernetes {
yaml '''
apiVersion: v1
kind: Pod
metadata:
labels:
app: jenkins-agent
spec:
containers:
- name: maven
image: maven:3.8-openjdk-17
command: ['sleep', 'infinity']
volumeMounts:
- name: m2-cache
mountPath: /root/.m2
- name: docker
image: docker:24-dind
securityContext:
privileged: true
volumes:
- name: m2-cache
persistentVolumeClaim:
claimName: maven-cache
'''
}
}
stages {
stage('Build') {
steps {
container('maven') {
sh 'mvn clean package'
}
}
}
stage('Docker Build') {
steps {
container('docker') {
sh 'docker build -t my-app .'
}
}
}
}
}
Plugin Docker
Docker Pipeline
pipeline {
agent any
stages {
stage('Build in Docker') {
agent {
docker {
image 'node:18'
args '-v /tmp:/tmp'
}
}
steps {
sh 'npm ci && npm run build'
}
}
stage('Build Docker Image') {
steps {
script {
def app = docker.build("my-app:${BUILD_NUMBER}")
docker.withRegistry('https://registry.example.com', 'docker-creds') {
app.push()
app.push('latest')
}
}
}
}
stage('Test with Docker Compose') {
steps {
sh 'docker-compose -f docker-compose.test.yml up -d'
sh 'npm run test:e2e'
sh 'docker-compose -f docker-compose.test.yml down'
}
}
}
}
Résumé des plugins recommandés
# plugins.txt - Installation minimale recommandée
# Core Pipeline
workflow-aggregator:latest
pipeline-stage-view:latest
pipeline-utility-steps:latest
# Source Control
git:latest
github:latest
github-branch-source:latest
# Build Tools
nodejs:latest
maven-plugin:latest
# Containers
docker-workflow:latest
kubernetes:latest
# Quality
sonar:latest
junit:latest
jacoco:latest
warnings-ng:latest
# Notifications
slack:latest
email-ext:latest
# Credentials
credentials-binding:latest
# Configuration
configuration-as-code:latest
job-dsl:latest
# UI
blueocean:latest
# Utilities
timestamper:latest
ansicolor:latest
rebuild:latest