jenkins-pipeline

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Jenkins Pipeline

Jenkins Pipeline

Overview

概述

Create enterprise-grade Jenkins pipelines using declarative and scripted approaches to automate building, testing, and deploying with advanced control flow.
使用声明式和脚本式方法创建企业级Jenkins流水线,通过高级控制流实现构建、测试和部署的自动化。

When to Use

适用场景

  • Enterprise CI/CD infrastructure
  • Complex multi-stage builds
  • On-premise deployment automation
  • Parameterized builds
  • 企业级CI/CD基础设施
  • 复杂的多阶段构建
  • 本地部署自动化
  • 参数化构建

Implementation Examples

实现示例

1. Declarative Pipeline (Jenkinsfile)

1. Declarative Pipeline (Jenkinsfile)

groovy
pipeline {
    agent { label 'linux-docker' }
    environment {
        REGISTRY = 'docker.io'
        IMAGE_NAME = 'myapp'
    }
    parameters {
        string(name: 'DEPLOY_ENV', defaultValue: 'staging')
    }
    stages {
        stage('Checkout') { steps { checkout scm } }
        stage('Install') { steps { sh 'npm ci' } }
        stage('Lint') { steps { sh 'npm run lint' } }
        stage('Test') {
            steps {
                sh 'npm run test:coverage'
                junit 'test-results.xml'
            }
        }
        stage('Build') {
            steps {
                sh 'npm run build'
                archiveArtifacts artifacts: 'dist/**/*'
            }
        }
        stage('Deploy') {
            when { branch 'main' }
            steps {
                sh 'kubectl set image deployment/app app=${REGISTRY}/${IMAGE_NAME}:latest'
            }
        }
    }
    post {
        always { cleanWs() }
        failure { echo 'Pipeline failed!' }
    }
}
groovy
pipeline {
    agent { label 'linux-docker' }
    environment {
        REGISTRY = 'docker.io'
        IMAGE_NAME = 'myapp'
    }
    parameters {
        string(name: 'DEPLOY_ENV', defaultValue: 'staging')
    }
    stages {
        stage('Checkout') { steps { checkout scm } }
        stage('Install') { steps { sh 'npm ci' } }
        stage('Lint') { steps { sh 'npm run lint' } }
        stage('Test') {
            steps {
                sh 'npm run test:coverage'
                junit 'test-results.xml'
            }
        }
        stage('Build') {
            steps {
                sh 'npm run build'
                archiveArtifacts artifacts: 'dist/**/*'
            }
        }
        stage('Deploy') {
            when { branch 'main' }
            steps {
                sh 'kubectl set image deployment/app app=${REGISTRY}/${IMAGE_NAME}:latest'
            }
        }
    }
    post {
        always { cleanWs() }
        failure { echo 'Pipeline failed!' }
    }
}

2. Scripted Pipeline (Groovy)

2. Scripted Pipeline (Groovy)

groovy
// Jenkinsfile - Scripted Pipeline

node('linux-docker') {
    def imageTag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
    def registry = 'docker.io'

    try {
        stage('Checkout') { checkout scm }
        stage('Install') { sh 'npm ci' }
        stage('Test') { sh 'npm test' }
        stage('Build') { sh 'npm run build' }

        currentBuild.result = 'SUCCESS'
    } catch (Exception e) {
        currentBuild.result = 'FAILURE'
        error("Build failed: ${e.message}")
    }
}
groovy
// Jenkinsfile - Scripted Pipeline

node('linux-docker') {
    def imageTag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
    def registry = 'docker.io'

    try {
        stage('Checkout') { checkout scm }
        stage('Install') { sh 'npm ci' }
        stage('Test') { sh 'npm test' }
        stage('Build') { sh 'npm run build' }

        currentBuild.result = 'SUCCESS'
    } catch (Exception e) {
        currentBuild.result = 'FAILURE'
        error("Build failed: ${e.message}")
    }
}

3. Multi-Branch Pipeline

3. Multi-Branch Pipeline

groovy
pipeline {
    agent any
    stages {
        stage('Build') { steps { sh 'npm run build' } }
        stage('Test') { steps { sh 'npm test' } }
        stage('Deploy') {
            when { branch 'main' }
            steps { sh 'npm run deploy:prod' }
        }
    }
}
groovy
pipeline {
    agent any
    stages {
        stage('Build') { steps { sh 'npm run build' } }
        stage('Test') { steps { sh 'npm test' } }
        stage('Deploy') {
            when { branch 'main' }
            steps { sh 'npm run deploy:prod' }
        }
    }
}

4. Parameterized Pipeline

4. Parameterized Pipeline

groovy
pipeline {
    agent any
    parameters {
        string(name: 'VERSION', defaultValue: '1.0.0', description: 'Version to release')
        choice(name: 'ENV', choices: ['staging', 'prod'], description: 'Deployment environment')
    }
    stages {
        stage('Build') { steps { sh 'npm run build' } }
        stage('Test') { steps { sh 'npm test' } }
        stage('Deploy') {
            steps { sh "npm run deploy:${params.ENV}" }
        }
    }
}
groovy
pipeline {
    agent any
    parameters {
        string(name: 'VERSION', defaultValue: '1.0.0', description: 'Version to release')
        choice(name: 'ENV', choices: ['staging', 'prod'], description: 'Deployment environment')
    }
    stages {
        stage('Build') { steps { sh 'npm run build' } }
        stage('Test') { steps { sh 'npm test' } }
        stage('Deploy') {
            steps { sh "npm run deploy:${params.ENV}" }
        }
    }
}

5. Pipeline with Credentials

5. Pipeline with Credentials

groovy
pipeline {
    agent any
    environment {
        DOCKER_CREDS = credentials('docker-hub')
    }
    stages {
        stage('Build & Push') {
            steps {
                sh '''
                    echo $DOCKER_CREDS_PSW | docker login -u $DOCKER_CREDS_USR --password-stdin
                    docker build -t myapp:latest .
                    docker push myapp:latest
                '''
            }
        }
    }
}
groovy
pipeline {
    agent any
    environment {
        DOCKER_CREDS = credentials('docker-hub')
    }
    stages {
        stage('Build & Push') {
            steps {
                sh '''
                    echo $DOCKER_CREDS_PSW | docker login -u $DOCKER_CREDS_USR --password-stdin
                    docker build -t myapp:latest .
                    docker push myapp:latest
                '''
            }
        }
    }
}

Best Practices

最佳实践

✅ DO

✅ 建议做法

  • Use declarative pipelines for clarity
  • Use credentials plugin for secrets
  • Archive artifacts and reports
  • Implement approval gates for production
  • Keep pipelines modular and reusable
  • 使用声明式流水线以提升清晰度
  • 使用凭据插件管理密钥
  • 归档构建产物和报告
  • 为生产环境设置审批关卡
  • 保持流水线模块化且可复用

❌ DON'T

❌ 避免做法

  • Store credentials in pipeline code
  • Ignore pipeline errors
  • Skip test coverage reporting
  • Use deprecated plugins
  • 在流水线代码中存储凭据
  • 忽略流水线错误
  • 跳过测试覆盖率报告
  • 使用已弃用的插件

Resources

资源