Jenkins

开源自动化服务器 - 构建流水线、可扩展插件、自托管 CI/CD

TL;DR

是什么:开源的 CI/CD 自动化服务器。

为什么用:高度可扩展、1800+ 插件、流水线即代码、自托管控制。

Quick Start

使用 Docker 安装

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

# 获取初始管理员密码
docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword

访问:打开 http://localhost:8080

使用 Homebrew 安装(macOS)

brew install jenkins-lts
brew services start jenkins-lts

Cheatsheet

操作位置
创建任务New Item → Pipeline
配置任务Job → Configure
查看构建Job → Build History
管理插件Manage Jenkins → Plugins
系统配置Manage Jenkins → System
凭证管理Manage Jenkins → Credentials

Gotchas

声明式 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!'
        }
    }
}

使用 Docker 的 Pipeline

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

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

使用凭证

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
                '''
            }
        }
    }
}

并行阶段

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

Next Steps