• jenkins--pipline(二)


    pipeline示例

    1.声明式pipeline
    该方式要想在 sh '执行命令' 中使用变量,需要先在environment{}中提前把变量定义好,才可以在sh代码中使用变量。

    #!groovy
    pipeline {
        agent {node {label 'master'}}
        environment {
            PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
        }
        
        stages {
            stage("pull code") {
                steps {
                    sh "git config --global http.sslVerify false"
                    dir("${env.WORKSPACE}") {
                        git branch: 'master',
                        credentialsId: '036938fb-888b-47d3-92cf-f9acc7f4fdaf',
                        url: 'http://gitlab.xxx.com:8880/dev-java/mybank-demo-maven.git'
                    }
                }
            }
    
            stage("compile code") {
                steps {
                    sh 'mvn -U -B clean install'
                }
            }
    
            stage("deploy project") {
                environment {
                    WORKSPACE = "${env.WORKSPACE}"
                    WAR_PATH = "${env.WORKSPACE}/target/ROOT.war"
                    tomcat_home="/usr/local/tomcat2"
                    app_home="${tomcat_home}/webapps/ROOT/"
                }
    
                steps {
                    sh """
                    set -e -u -x
    
                    echo "JOB_PATH: ${WORKSPACE}"
    
                    echo "[INFO] 关闭tomcat"
                    sudo ${tomcat_home}/bin/shutdown.sh
    
                    echo "[INFO] 清空app_home: $app_home"
                    sudo rm -rf ${app_home}*
    
                    echo "[INFO] 拷贝编译完成的war包"
                    sudo cp -f "${WAR_PATH}" ${app_home}
                    cd ${app_home}
                    sudo jar xf ROOT.war
    
                    echo "[INFO] 启动tomcat"
                    sudo ${tomcat_home}/bin/startup.sh
                    """
                }
            }
        }
    }
    2.脚本式pipeline
    该方式要想在sh代码中使用变量,则需要这样定义: env.tomcat_home = "/usr/local/tomcat2",然后在sh代码中这样引用变量${tomcat_home}即可

    #!groovy
    node {
        stage("pull code") {
            sh 'git config --global http.sslVerify false'
            dir("${env.WORKSPACE}") {
                git branch: 'master',
                credentialsId: '036938fb-888b-47d3-92cf-f9acc7f4fdaf',
                url: 'http://gitlab.xxx.com:8880/dev-java/mybank-demo-maven.git'
            }
        }
    
        stage("compile code") {
            sh """
            mvn -U -B clean install
            """
        }
    
        
        stage("deploy project") {
            env.WORKSPACE = "${env.WORKSPACE}"
            env.WAR_PATH = "${env.WORKSPACE}/target/ROOT.war"
            env.tomcat_home = "/usr/local/tomcat2"
            env.app_home = "${tomcat_home}/webapps/ROOT/"
    
            sh """
            set -e -u -x
    
            echo "JOB_PATH: ${WORKSPACE}"
    
            echo "[INFO] 关闭tomcat"
            sudo ${tomcat_home}/bin/shutdown.sh
    
            echo "[INFO] 清空app_home: $app_home"
            sudo rm -rf ${app_home}*
    
            echo "[INFO] 拷贝编译完成的war包"
            sudo cp -f ${WAR_PATH} ${app_home}
            cd ${app_home}
            sudo jar xf ROOT.war
    
            echo "[INFO] 启动tomcat"
            sudo ${tomcat_home}/bin/startup.sh
            """
        }
    }
    以上两种pipeline写法可以达到相同的效果。

  • 相关阅读:
    典型用户
    站立会议5
    站立会议4
    《构建之法》阅读笔记05-需求分析
    站立会议3
    编写Android程序Eclipse连不上手机。
    站立会议2
    站立会议1
    第七周学习进度
    shiro之 散列算法(加密算法)
  • 原文地址:https://www.cnblogs.com/absoluteli/p/13960409.html
Copyright © 2020-2023  润新知