Jenkins

Serveur d'automatisation open-source - pipelines de build, plugins extensibles, CI/CD auto-heberge

TL;DR

Quoi : Serveur d’automatisation open-source pour les pipelines CI/CD.

Pourquoi : Hautement extensible, 1800+ plugins, pipeline-as-code, contrôle auto-hébergé.

Quick Start

Installation avec Docker :

docker run -d -p 8080:8080 -p 50000:50000 \
  -v jenkins_home:/var/jenkins_home \
  --name jenkins \
  jenkins/jenkins:lts

# Récupérer le mot de passe admin initial
docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword

Accès : Ouvrez http://localhost:8080

Installation avec Homebrew (macOS) :

brew install jenkins-lts
brew services start jenkins-lts

Cheatsheet

ActionEmplacement
Créer un jobNouvel Item → Pipeline
Configurer un jobJob → Configurer
Voir les buildsJob → Historique des builds
Gérer les pluginsAdministrer Jenkins → Plugins
Config systèmeAdministrer Jenkins → Système
IdentifiantsAdministrer Jenkins → Identifiants

Gotchas

Declarative Pipeline (Jenkinsfile)

pipeline {
    agent any

    environment {
        APP_NAME = 'myapp'
    }

    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }

        stage('Build') {
            steps {
                sh 'npm install'
                sh 'npm run build'
            }
        }

        stage('Test') {
            steps {
                sh 'npm test'
            }
        }

        stage('Deploy') {
            when {
                branch 'main'
            }
            steps {
                sh './deploy.sh'
            }
        }
    }

    post {
        success {
            echo 'Build succeeded!'
        }
        failure {
            echo 'Build failed!'
        }
    }
}

Pipeline with Docker

pipeline {
    agent {
        docker {
            image 'node:18'
        }
    }

    stages {
        stage('Build') {
            steps {
                sh 'npm install'
                sh 'npm run build'
            }
        }
    }
}

Using credentials

pipeline {
    agent any

    environment {
        DOCKER_CREDS = credentials('docker-hub-creds')
    }

    stages {
        stage('Push') {
            steps {
                sh '''
                    echo $DOCKER_CREDS_PSW | docker login -u $DOCKER_CREDS_USR --password-stdin
                    docker push myimage:latest
                '''
            }
        }
    }
}

Parallel stages

stage('Tests') {
    parallel {
        stage('Unit Tests') {
            steps {
                sh 'npm run test:unit'
            }
        }
        stage('Integration Tests') {
            steps {
                sh 'npm run test:integration'
            }
        }
    }
}

Next Steps