注意:PPX......假设 X 是,就代表这个例子是第几章节的例子,和 Pipeline Doc 中文版-X-......中的 X 对应

本系列主贴直达:https://testerhome.com/topics/11265

Pipeline1-Demo-Helloworld

node {
    echo "Hello World!!!"
    echo 'Hello World!!!'
}

PP2-Demo-build-declarative: https://gitee.com/roclli/4-declarative.git
PP2-Demo-build-script:

node {
    stage('Build') {
        echo 'Building....'
        def username = 'Jenkins'
        echo 'Hello Mr. ${username}'
        echo "I said, Hello Mr. ${username}"
        checkout([$class: 'GitSCM', branches: [[name: '*/master'], [name: '*/testbr']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'https://gitee.com/roclli/simple-maven-project-with-tests.git']]])
        sh "mvn -B -Dmaven.test.failure.ignore verify"
    }
    stage('Test') {
        echo 'Testing....'
    }
    stage('Deploy') {
        echo 'Deploying....'
    }
}

Pipeline2-Demo-checkoutscm: https://gitee.com/roclli/simple-maven-project-with-tests.git

PP2-Demo-handingfailure-declarative: https://gitee.com/roclli/7-parameters-script.git

PP2-Demo-handingfailure-script

node {
    stage('Test') {
        try {
            checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'https://gitee.com/roclli/7-parameters-script.git']]])
            sh 'mvn verify'
        }
        finally {
            junit '**/target/surefire-reports/TEST-**.xml'
        }
    }
}

PP2-Demo-multipleagent-declartive: https://gitee.com/roclli/9-multipleagent.git
PP2-Demo-multipleagent-script

node {
    stage('Build') {
        node {
            echo "---1in linux,start to checkout---"
            checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'https://gitee.com/roclli/9-multipleagent.git']]])
            echo "---1in linux,start to  -B -Dmaven.test.failure.ignore verify---"
            sh "mvn -B -Dmaven.test.failure.ignore verify"
            echo "---1in linux,start to stash *.jar---"
            stash includes: '**/target/*.jar', name: 'app'
        }
    }

    stage('Test') {
        node('linux') {
            echo "---2in linux,start to checkout---"
            checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'https://gitee.com/roclli/9-multipleagent.git']]])
            try {
                echo "---2in linux,start to unstash app---"
                unstash 'app'
                echo "---2in linux,start to  -B -Dmaven.test.failure.ignore verify---"
                sh "mvn -B -Dmaven.test.failure.ignore verify"
            }
            finally {
                echo "---2in linux,start to junit TEST-*.xml report---"
                junit '**/target/surefire-reports/TEST-*.xml'
            }
        }
        node('windows') {
            echo "---3in windows,start to checkout---"
            checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'https://gitee.com/roclli/9-multipleagent.git']]])
            try {
                echo "---3in windows,start to unstash app---"
                unstash 'app'
                echo "---3in windows,start to  -B -Dmaven.test.failure.ignore verify---"
                bat 'mvn -B -Dmaven.test.failure.ignore verify'
            }
            finally {
                echo "---3in windows,start to junit TEST-*.xml report---"
                junit '**/target/surefire-reports/TEST-*.xml'
            }
        }
    }
}

PP2-Demo-parameters-declarative: https://gitee.com/roclli/7-parameters-script.git
PP2-Demo-parameters-script

properties([parameters([string(defaultValue: 'Hello', description: 'How should I greet the world?', name: 'Greeting')])])

node {
    echo "---will output params.Greeting's values:"
    echo "${params.Greeting} World!"
}

PP2-Demo-setenv-declarative: https://gitee.com/roclli/6-setting-env.git
PP2-Demo-setenv-script

node {
    stage('set-env'){
        echo 'going to git clone......'
        git url: 'https://gitee.com/roclli/6-setting-env.git'
        echo "going to build.........."
        echo "---1:${tool 'M3'}---"
        withEnv(["PATH_MAVEN=${tool 'M3'}/bin"]){
            echo "---2:${PATH_MAVEN}---"
            sh "${PATH_MAVEN}/mvn -B verify || true"
            sh "printenv"
        }
    }

}

PP2-Demo-test-declarative: https://gitee.com/roclli/5-build-tests.git
PP2-Demo-test-script

node {
//    try {
        stage('Build'){
            echo 'going to git clone......'
            git url: 'https://gitee.com/roclli/5-build-tests.git'
            echo "going to build.........."
            sh "mvn -B -Dmaven.test.failure.ignore verify  || true"
            echo "going to archiveArtifacts jar.........."
            archiveArtifacts artifacts: '**/target/**.jar', fingerprint: true
        }

        stage('Test'){
    //        sh 'mvn clean install || true'
    //        sh "mvn -B -Dmaven.test.failure.ignore verify"
            echo "junit testreport.........."
            junit '**/target/surefire-reports/TEST-**.xml'
        }
//    } catch(e) {
//        currentBuild.result = "FAILURE"
//        throw e
//    }

    stage('Deploy'){
        echo "---env.BUILD_NUMBER is:${env.BUILD_NUMBER}---"
        echo "---currentBuild.result is:${currentBuild.result}---"
        echo "---env.BUILD_ID is:${env.BUILD_ID}---"
        echo "---env.JOB_NAME is:${env.JOB_NAME}---"
        echo "---env.JENKINS_URL is:${env.JENKINS_URL}---"
//        echo "---"${BUILD_NUMBER}"---"
//        echo "---1:"currentBuild.result"----2:${currentBuild.result}---"
//        //SUCCESS, UNSTABLE, or FAILURE (may be null for an ongoing build)
        if(currentBuild.result == null || currentBuild.result == 'SUCCESS') {
            echo "---currentBuild.result is:${currentBuild.result}------"
        }
        else {
            echo "---currentBuild.result is:${currentBuild.result},so, will make publish"
        }
    }

}

PP3-Demo-MultiBranch-Pipeline: https://gitee.com/roclli/simple-maven-project-with-tests.git

PP4-Demo-cache-data-declarative: https://gitee.com/roclli/pp4-1-cache-data.git

PP4-Demo-cache-data-script

node {
//    echo "---$HOME---"
//    echo "---$HOME/.m2:/root/.m2---"
//    echo "---$M2_HOME/repo---"
//    sh "ls -lrt $M2_HOME/repo"
    /* Requires the Docker Pipeline plugin to be installed
    注意:正常情况下应该使用$HOME/.m2
    我本地用的是用的是特殊的maven
    docker.image('maven:3-alpine').inside('-v $HOME/.m2:/root/.m2') {
    */
    docker.image('maven:3-alpine').inside('-v $M2_HOME/repo:/root/.m2') {
//        checkout([$class: 'GitSCM', branches: [[name: '*/master'], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'https://gitee.com/roclli/pp4-1-cache-data.git']]])
        git url: 'https://gitee.com/roclli/pp4-1-cache-data.git'
        stage('Build') {
            //执行编译打包
            sh 'mvn -D$HOME/.m2=/root/.m2 -DskipTests clean package'
        }
    }
}

PP4-Demo-custom-exec-env-declartive: https://gitee.com/roclli/pp4-0-custom-exec-env
PP4-Demo-custom-exec-env-script

node {
    /* Requires the Docker Pipeline plugin to be installed */
    docker.image('node:7-alpine').inside {
        stage('Test') {
            sh 'node --version'
        }
    }
}

PP4-Demo-dockerfile-declarative: https://gitee.com/roclli/pp4-3-dockerfile.git

PP4-Demo-multip-container-declarative: https://gitee.com/roclli/pp4-2-multip-container
PP4-Demo-multip-container-script

node {
    /* Requires the Docker Pipeline plugin to be installed */
    stage('Back-end') {
        docker.image('maven:3-alpine').inside {
            sh 'mvn --version'
        }
    }

    stage('Front-end') {
        docker.image('node:7-alpine').inside {
            sh 'node --version'
        }
    }
}

PP4-Demo-sidecar-script-1:

node {
//    checkout scm
    /*
     * In order to communicate with the MySQL server, this Pipeline explicitly
     * maps the port (`3306`) to a known port on the host machine.
     */
    docker.image('mysql:5').withRun('-e "MYSQL_ROOT_PASSWORD=my-secret-pw" -p 3306:3306') { c ->
        /* Wait until mysql service is up */
//        echo "---start mysql---"
//        sh "service mysql start"
        //shijianzhengming实践证明,不需要单独启动MySQL
//        sh 'while ! mysqladmin ping -h0.0.0.0 --silent; do sleep 1; done'
//        echo "---output pwd start---"
//        sh "pwd"
//        echo "---output pwd end---"
//        sh "cat /etc/os-release"
//        echo "---output cat /etc/os-release end---"
        sh 'while ! mysqladmin ping -h0.0.0.0 --silent; do sleep 1; done'
//        sh "while ! /usr/bin/mysql -uroot -h0.0.0.0 -p my-secret-pw -e 'show databases' --silent; do sleep 1; done"
        /* Run some tests which require MySQL */
//        sh 'make check'
        echo "---run some tests which require MySQL---"
    }
}

PP4-Demo-sidecar-script-2

node {
//    checkout scm
    docker.image('mysql:5').withRun('-e "MYSQL_ROOT_PASSWORD=my-secret-pw"') { c ->
        docker.image('mysql:5').inside("--link ${c.id}:db") {
            /* Wait until mysql service is up */
            echo "---1output cat /etc/os-release end---"
            sh "cat /etc/os-release"
            echo "---1output cat /etc/os-release end---"
            sh 'while ! mysqladmin ping -hdb --silent; do sleep 1; done'
        }
        docker.image('centos:7').inside("--link ${c.id}:db") {
            /*
             * Run some tests which require MySQL, and assume that it is
             * available on the host name `db`
             */
//            sh 'make check'
            echo "---2output cat /etc/os-release end---"
            sh "cat /etc/os-release"
            echo "---2output cat /etc/os-release end---"
            echo "---run test whcih require MySQL---"
        }
    }
}

本系列主贴直达:https://testerhome.com/topics/11265


↙↙↙阅读原文可查看相关链接,并与作者交流