def pipeId = 1130561944231279390
def pipeLogId
def isTagOrBranch
def tagOrBranch
def imageId
def addPipelineLog() {
    def isUserTriggered = currentBuild.rawBuild.getCause(hudson.model.Cause$UserIdCause) != null
    def isGitLabWebHookTriggered = currentBuild.rawBuild.getCause(com.dabsquared.gitlabjenkins.cause.GitLabWebHookCause) != null
    def isTimerTriggered = currentBuild.rawBuild.getCause(hudson.triggers.TimerTrigger$TimerTriggerCause) != null
    def isUpstreamTriggered = currentBuild.rawBuild.getCause(hudson.model.Cause.UpstreamCause) != null
    println "trigger type:"+currentBuild.rawBuild.getCauses().dump()
    println "params.logId: "+params.logId
    println "params.projectVersionConfigId: "+params.projectVersionConfigId
    println "params.publishLogId: "+params.publishLogId
    println "params.preExecuteEndPipeStageId: "+params.preExecuteEndPipeStageId

    def reqPipeLogId = params.logId
    def command = $/curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZW1zY210In0.7Kno6QoIFmT8nQv-C2edlP514ekqy9pPNID_Vf__DPc' -d '{"pipeId":1130561944231279390,"pipeLogId":"$reqPipeLogId","isUserTriggered":$isUserTriggered,"isGitLabWebHookTriggered":$isGitLabWebHookTriggered,"isTimerTriggered":$isTimerTriggered,"isUpstreamTriggered":$isUpstreamTriggered}' "http://10.145.196.76:12000/pipeline/pipelines/pipelineLog" /$
    res = sh(returnStdout: true, script: command).trim()
    println "add pipeline log result:\n" + res

    return parseJsonText(res).body.content
}

def getStageLog(log) {
    def stageLog = log.substring(log.lastIndexOf("[Pipeline] stage"))
    echo "log size:" + stageLog.length()

    return URLEncoder.encode(stageLog, "UTF-8")
}

def toUpdatePipelineLogCurlSh(result, duration, log, pipeLogId, pipeStageId) {
    def tmpDir = 'deploy_tmp'
    def tmpFile = 'updatePipelineLog_params_tmp'

    def data = '{"flag":"'+result+'","duration":"'+duration+'","message":"'+log+'","pipeLogId":"'+pipeLogId+'","pipeStageId":"'+pipeStageId+'","preExecuteEndPipeStageId":"'+preExecuteEndPipeStageId+'"}';
    writeToFile(env.WORKSPACE, tmpDir, tmpFile, data)

    def tmpFilePath = tmpDir + '/' + tmpFile
    def curl = 'curl -X PUT  --header \'Content-Type: application/json\' --header \'Accept: application/json\' --header \'token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZW1zY210In0.7Kno6QoIFmT8nQv-C2edlP514ekqy9pPNID_Vf__DPc\' --data-binary "@'+ tmpFilePath +'" "http://10.145.196.76:12000/pipeline/pipelines/pipelineStageLog" '
    return curl
}

def toEndPipelineLogCurlSh(result, pipeLogId, projectVersionConfigId, publishLogId) {
    def curl = 'curl -X GET  --header \'token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZW1zY210In0.7Kno6QoIFmT8nQv-C2edlP514ekqy9pPNID_Vf__DPc\' "http://10.145.196.76:12000/pipeline/pipelines/pipeLineLog/'+pipeLogId+'?flag='+result+'&projectVersionConfigId='+projectVersionConfigId+'&publishLogId='+publishLogId+'" '
    return curl
}

//预执行结束阶段校验
def preExecuteEndPipelineStageCheck(pipeStageId) {
    def pipeStageIdStr = '' + pipeStageId
    def isEndPoint = (params.preExecuteEndPipeStageId != null && pipeStageIdStr == params.preExecuteEndPipeStageId)
    if (isEndPoint) {
        println "this stage is pre-execute end stage, pre-execute finished "
        error "preExecuteFinished"
    }
}

//若异常是预执行结束,将result置为SUCCESS
def setPreExecuteResult(result, msg) {
    if (msg == 'preExecuteFinished') {
        result = 'SUCCESS'
        println "this exception message is pre-execute finished , set result to SUCCESS "
        println "本阶段是预执行结束阶段,预执行完成"
        cleanWs()
        println "clean workspace"
    }
    return result
}

//sonar check analysis
def toSonarCheckCurlSh(appId, appKey) {
    def curl = 'curl -X POST --header \'token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZW1zY210In0.7Kno6QoIFmT8nQv-C2edlP514ekqy9pPNID_Vf__DPc\' -d \'appId='+appId+'&appKey='+appKey+'\' "http://10.145.196.76:12000/quality/analysis" '
    return curl
}

def analysisSonarCheck(appId, appKey) {
    def data = '?appId='+appId+'&appKey='+appKey
    def url = 'http://10.145.196.76:12000/quality/analysis'+data
    println "request " + url + " to analysis sonar check result \nparams: " + data
    return paasPost(url, null)
}

def getSonarAppProfile(appId) {
    /*
    // GET
    def get = new URL("http://10.145.196.76:12000/quality/app/profile/"+appId).openConnection();
    get.setRequestProperty("token","eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZW1zY210In0.7Kno6QoIFmT8nQv-C2edlP514ekqy9pPNID_Vf__DPc")

    def getRC = get.getResponseCode();
    def res;
    if(getRC.equals(200)) {
        res=get.getInputStream().getText()
        println(res);
    } else {
        error "Sonar Check Stage - getSonarAppProfile(" + appId + ") get sonar app profile error :" + getRC
    }

    return parseJsonText(res).body.content
    */

    def url = 'http://10.145.196.76:12000/quality/app/profile/'+appId
    println "request " + url + " to get sonar app profile"
    return paasGet(url)
}

def toApproveCurlSh(pipeId, pipeLogId, pipeStageId, buildId, jobName) {
    def curl = 'curl -X POST --header \'Content-Type: application/json\' --header \'Accept: application/json\' --header \'token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZW1zY210In0.7Kno6QoIFmT8nQv-C2edlP514ekqy9pPNID_Vf__DPc\' -d \'{"pipeId":"'+pipeId+'","pipeLogId":"'+pipeLogId+'","pipeStageId":"'+pipeStageId+'","inputId":"'+pipeStageId+'","buildId":"'+buildId+'","jobName":"'+jobName+'"}\' "http://10.145.196.76:12000/pipeline/pipelines/addPersonalTask" '
    return curl
}

//sync harbor image by our api
def syncPaasImage(imageId, targetCeEnv, pipeLogId, pipeStageId) {
    def url = 'http://10.145.196.76:12000/pipeline/image/sync'
    def data = '{"imageId":"'+imageId+'","targetCeEnv":"'+targetCeEnv+'","pipeLogId":"'+pipeLogId+'","pipeStageId":"'+pipeStageId+'"}'
    println "request " + url + " to sync harbor image by pipeline's api\nparams: " + data
    return paasPost(url, data)
}

//sync harbor image by ce-api directly
def syncPaasImage(url, sourceAddress, destAddress, codePackagePath, imageName, pipeLogId) {
    if (imageName == null || imageName == '' || imageName == 'null') {
        //适用于构建镜像阶段(选择镜像阶段不需要,yaml中已有镜像名)
        imageName = getImageName(codePackagePath, pipeLogId)
    }
    sourceAddress = sourceAddress+':'+imageName
    destAddress = destAddress+':'+imageName
    def data = '{"pullpoint":"'+sourceAddress+'","pushpoint":"'+destAddress+'"}'
    println "request " + url + " to sync harbor image by ce-api directly\nparams: " + data
    return paasPost(url, data)
}

//record sync-image info
def recordSyncImageInfo(imageId, ceEnvKey, pipeLogId, pipeStageId) {
    def url = 'http://10.145.196.76:12000/pipeline/image/syncImage'
    def data = '{"imageId":"'+imageId+'", "imageCeEnv":"'+ceEnvKey+'",' +
        '"pipeStageId":"'+pipeStageId+'", "pipeLogId":"'+pipeLogId+'"}'
    println "request " + url + " to save sync-image info\nparams: " + data
    return paasPost(url, data)
}

//build harbor image
def buildPaasImage(ceRemoteAddress, imgNamespaceName, repoName,
    tagName, codeFilePath, hasAdditionalFile, curlUploadFilePara,
    dockerFileType, dockerfilePath, dockerFileContent) {

    //upload files
    uploadImageFiles(codeFilePath, hasAdditionalFile, curlUploadFilePara,
        ceRemoteAddress, imgNamespaceName, repoName, tagName);

    //dockerfile
    dockerfile = getDockerfileContent(dockerFileType, dockerfilePath, dockerFileContent)

    //build image
    buildPaasImage(dockerfile,
        ceRemoteAddress, imgNamespaceName, repoName, tagName);

    //wait image build finished
    waitImageBuildFinished(ceRemoteAddress, imgNamespaceName, repoName, tagName);
}

//record image info
def recordImageInfo(ceEnvKey, imgNamespaceName, repoName, tagName,
    isTagOrBranch, tagOrBranch, pipeLogId, pipeStageId) {
    def url = 'http://10.145.196.76:12000/pipeline/image'
    def data = '{"imageCeEnv":"'+ceEnvKey+'", "imageNamespace":"'+imgNamespaceName+'", "imageRepo":"'+repoName+'", "imageName":"'+tagName+'",' +
        '"sourceCodeType":"'+isTagOrBranch+'", "sourceCode":"'+tagOrBranch+'",' +
        '"appId":"123", "baseId":"1107175323881272818", "pipeId":"1130561944231279390", "pipeStageId":"'+pipeStageId+'",' +
        '"pipeLogId":"'+pipeLogId+'"}'
    println "request " + url + " to save image info\nparams: " + data
    return paasPost(url, data)

}

def uploadImageFiles(codeFilePath, hasAdditionalFile, curlUploadFilePara,
    ceRemoteAddress, imgNamespaceName, repoName, tagName) {

    def otherFilePara = ""
    if (hasAdditionalFile) {
        otherFilePara = curlUploadFilePara
    }
    def uploadCommand = '''
        #上传构建镜像所需文件
        curl -F "files=@'''+codeFilePath+'''" '''+otherFilePara+''' --header "token:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZW1zY210In0.7Kno6QoIFmT8nQv-C2edlP514ekqy9pPNID_Vf__DPc" "'''+ceRemoteAddress+'''/zuul/ceimage/uploadfiles?namespace-name='''+imgNamespaceName+'''&repo-name='''+repoName+'''&tag-name='''+tagName+'''&username=temscmt"
    '''
    def uploadRes = sh(returnStdout: true, script: uploadCommand).trim()
    println "upload image related files to paas container result:\n " + uploadRes
    handleException(uploadRes, 'upload image related files to paas container')
}

def getDockerfileContent(dockerFileType, dockerfilePath, dockerFileContent) {
    def tmpDir = 'deploy_tmp'

    //dockerfile内容输出到文件
    if (dockerFileType == 'INPUT') {
        def tmpFile = 'Dockerfile_devops_tmp'
        println "dockerfile content is by user input online, so need to write to a tmp file"
        writeToFile(env.WORKSPACE, tmpDir, tmpFile, dockerFileContent)
        dockerfilePath = tmpDir + '/' + tmpFile
    }

    dockerfile = readFile encoding: 'UTF-8', file: dockerfilePath
    dockerfile = dockerfile.replaceAll("\n", "\\\\n")

    return dockerfile
}

def writeToFile(currentPath, relativeDirPath, filename, content) {
    println "write to file " + currentPath + " " + relativeDirPath + " " +filename
    writeFile encoding: 'UTF-8', file: relativeDirPath+'/'+filename, text: content
    println "write to file finish"
}

def buildPaasImage(dockerfile,
    ceRemoteAddress, imgNamespaceName, repoName, tagName) {
    def baseImageName='centos-jdk8'
    def baseImageTag='latest'

    def url = ceRemoteAddress + '/ceimage/build'
    def data = '{"baseImageName": "'+baseImageName+'","baseImageTag": "'+baseImageTag+'", "dockerfile": "'+dockerfile+'", "namespaceName": "'+imgNamespaceName+'", "repoName": "'+repoName+'", "tag": "'+tagName+'", "username":"temscmt"}'
    println "request " + url + " to build image\nparams: " + data
    paasPost(url, data)

    /*
    def buildCommand = '''
        #构建镜像
        curl -X POST --header 'Content-Type: application/json' \
        --header 'Accept: application/json' \
        --header "token:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZW1zY210In0.7Kno6QoIFmT8nQv-C2edlP514ekqy9pPNID_Vf__DPc" \
        -d '{"baseImageName": "'''+baseImageName+'''","baseImageTag": "'''+baseImageTag+'''", "dockerfile": "'''+dockerfile+'''", "namespaceName": "'''+imgNamespaceName+'''", "repoName": "'''+repoName+'''", "tag": "'''+tagName+'''", "username":"temscmt"}' \
        "'''+ceRemoteAddress+'''/ceimage/build"
    '''
    def buildRes = sh(returnStdout: true, script: buildCommand).trim()
    handleException(buildRes, 'build image to paas container error')
    */
}

def waitImageBuildFinished(ceRemoteAddress, imgNamespaceName, repoName, tagName) {
    timeout(10) {
        waitUntil {
            def res = getCeImageBuildDetail(ceRemoteAddress, imgNamespaceName, repoName, tagName)
            if (res == null) {
                println " image build detail body null 镜像还未找到... "
                return false
            } else if (res.status == '1') {
                println " image build detail status 1 镜像构建中 ... "
                return false
            } else if (res.status == '2') {
                println " image build detail status 2 镜像构建成功 ... "
                return true
            } else if (res.status == '3') {
                println " image build detail status 3 镜像构建失败 ... "
                error "镜像构建失败"
            } else {
                println " image build detail status other ... " + res.status
                return false
            }
        }
    }
}

def getCeImageBuildDetail(ceRemoteAddress, imgNamespaceName, repoName, tagName) {
    def url = ceRemoteAddress + '/ceimage/buildrecords/namespaces/' + imgNamespaceName + '/repos/' + repoName + '/tags/' + tagName
    println "request " + url + " to get image build detail"
    return paasGet(url)
}

//select harbor image
def selectPaasImage(ceRemoteAddress, imgNamespaceName, repoName, tagName) {
    def res = getCeImageBuildDetail(ceRemoteAddress, imgNamespaceName, repoName, tagName)
    if (res != null && res.status == '2') {
        println " 镜像存在"
    } else {
        println " image status other ... " + res.status
        error "镜像状态异常"
    }
}

//ansible deploy
def ansibleDeploy() {
    def url = 'http://10.145.196.76:12000/pipeline/pipelines/get/stage/config?pipeId=1130561944231279390'
    println "request " + url + " to get ansible inventory and playbook"
    def res = paasGet(url)

    def inventory = res.inventory
    //println "res.inventory:\n" + inventory
    def playbook = res.playbook
    println "res.playbook:\n" + playbook

    def tmpDir = 'deploy_tmp';
    println "inventory content is by user input online, so need to write to a tmp file"
    writeToFile(env.WORKSPACE, tmpDir, 'inventory_tmp', inventory)
    println "playbook content is by user input online, so need to write to a tmp file"
    writeToFile(env.WORKSPACE, tmpDir, 'playbook_tmp.yml', playbook)
}

//deploy paas app
def deployPaasApp(pipeStageId, codePackagePath, pipeLogId) {
    def codeFileNoExt = ''
    if (codePackagePath != null && codePackagePath != '' && codePackagePath != 'null') {
        //适用于构建镜像阶段(选择镜像阶段不需要,yaml中已有镜像名)
        codeFileNoExt = getCodeFileNameNoExt(codePackagePath)
    }
    def url = 'http://10.145.196.76:12000/pipeline/pipelines/paasdeploy'
    def data = '{"pipeId":"1130561944231279390","pipeStageId":"'+pipeStageId+'","parameters":{"codeFileNoExt":"'+codeFileNoExt+'","buildId":"'+env.BUILD_ID+'","pipeLogId":"'+pipeLogId+'"}}'
    println "request " + url + " to deploy paas app\nparams: " + data
    return paasPost(url, data)

    /*
    def command = '''
        #容器应用发布
        curl -X POST --header 'Content-Type: application/json' \
        --header 'Accept: application/json' \
        --header "token:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZW1zY210In0.7Kno6QoIFmT8nQv-C2edlP514ekqy9pPNID_Vf__DPc" \
        -d '{"pipeId": "1130561944231279390","pipeStageId": "'''+pipeStageId+'''", "parameters": {"codeFileNoExt":"'''+codeFileNoExt+'''", "buildId": "'''+env.BUILD_ID+'''", "pipeLogId": "'''+pipeLogId+'''"}}' \
        "http://10.145.196.76:12000/pipeline/pipelines/paasdeploy"
    '''
    def res = sh(returnStdout: true, script: command).trim()
    handleException(res, 'deploy app to paas container error')
    */
}

//get image name by code package's fuzzy path user input
def getImageName(codePackagePath, pipeLogId) {
    codeFileNoExt = getCodeFileNameNoExt(codePackagePath)
    return formatImageName(codeFileNoExt, pipeLogId)
}

//get image name by code package's exact relative path
def getImageNameByExactPath(codeFilePath, pipeLogId) {
    codeFileNoExt = getCodeFileNameNoExtByExactPath(codeFilePath)
    return formatImageName(codeFileNoExt, pipeLogId)
}

//format image name
def formatImageName(codeFileNoExt, pipeLogId) {
    return codeFileNoExt + '-te-msc-paas-gateway-' + pipeLogId + '-' + env.BUILD_ID
}

//get code package filename without extension by fuzzy path user input
def getCodeFileNameNoExt(codePackagePath) {
    def codeFilePath = getCodeFilePath(codePackagePath)
    return getCodeFileNameNoExtByExactPath(codeFilePath)
}

//get code package filename without extension by its exact relative path
def getCodeFileNameNoExtByExactPath(codeFilePath) {
    def command = '''
        #获取包名
        codeFile=$(basename '''+codeFilePath+''')

        #获取不含后缀的包名
        echo ${codeFile%.*}
    '''
    return sh(returnStdout: true, script: command).trim()
}

//find code package and return relative path by fuzzy path user input
def getCodeFilePath(codePackagePath) {
    def command = '''
        #获取包文件
        codePackageSearchName=$(basename "'''+codePackagePath+'''")
        codePackageDirname=$(dirname "'''+codePackagePath+'''")
        codeFile=$(find $codePackageDirname -name $codePackageSearchName)

        #若模糊匹配的文件有多个,只取第一个
        echo $codeFile | awk '{ print $1 }'
    '''
    return sh(returnStdout: true, script: command).trim()
}

// PAAS GET
def paasGet(url) {
    def req = new URL(url).openConnection();
    req.setRequestProperty("token","eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZW1zY210In0.7Kno6QoIFmT8nQv-C2edlP514ekqy9pPNID_Vf__DPc")
    def getRC = req.getResponseCode()
    println "response code:"+getRC
    def res;
    if(getRC.equals(200)) {
        res=req.getInputStream().getText()
        def code = parseJsonText(res).code
        println "response paas json code:"+code
        if (code != '000000') {
            error "ERROR: request " + url + " " + code +  " error.\n" + res
        }
        println("result:\n" + res)
    } else {
        println "ERROR: request " + url + " " + getRC + " error."
        error req.getInputStream().getText()
    }
    return parseJsonText(res).body.content
}

// PAAS POST
def paasPost(url, data) {
    def req = new URL(url).openConnection()
    req.setRequestMethod("POST")
    req.setDoOutput(true)
    req.setRequestProperty("token","eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZW1zY210In0.7Kno6QoIFmT8nQv-C2edlP514ekqy9pPNID_Vf__DPc")
    req.setRequestProperty("Content-Type", "application/json")
    if (data != null) {
        req.getOutputStream().write(data.getBytes("UTF-8"))
    }
    def postRC = req.getResponseCode()
    println "response code:"+postRC
    def body;
    if(postRC.equals(200)) {
        body = req.getInputStream().getText()
        def code = parseJsonText(body).code
        println "response paas json code:"+code
        if (code != '000000') {
            error "ERROR: request " + url + " " + code +  " error.\n" + body
        }
        println("result:\n" + body)
    }else{
        println "ERROR: request " + url+ " " + postRC + " error."
        error req.getInputStream().getText()
    }
    return parseJsonText(body).body.content
}

def parseJsonText(String json) {
    return new groovy.json.JsonSlurperClassic().parseText(json)
}

def handleException(res, msg) {
    def code = parseJsonText(res).code
    if (code != '000000') {
        error "ERROR: " + msg + " " + code +  " \n" + res
    }
}

node{
stage('GIT拉取代码') {
pipeLogId = addPipelineLog()
def pipeStageId = 1130561945160317727
try{
isTagOrBranch = 'TAG'
tagOrBranch = params.tagOrBranch
checkout([
$class: 'GitSCM',branches: [[name: "${params.tagOrBranch}"]],
userRemoteConfigs: [[url: 'http://10.145.196.76:9080/Shtel-PaaS-Platform/shtel-paas-service-gateway.git', credentialsId: 'c7887cee85a24fdfa8e372d317fa1d83']]
])
preExecuteEndPipelineStageCheck(pipeStageId)
sleep(unit: 'MILLISECONDS', time: 500)
sh(script: toUpdatePipelineLogCurlSh(currentBuild.currentResult, currentBuild.duration, getStageLog(currentBuild.rawBuild.log), pipeLogId, pipeStageId))
}catch(e){
result = 'FAILURE'
echo e.getMessage()
result = setPreExecuteResult(result, e.getMessage())
sleep(unit: 'MILLISECONDS', time: 500)
sh(script: toUpdatePipelineLogCurlSh(result, currentBuild.duration, getStageLog(currentBuild.rawBuild.log), pipeLogId, pipeStageId))
sh(script: toEndPipelineLogCurlSh(result, pipeLogId, params.projectVersionConfigId, params.publishLogId))
throw e
}
}
}
node{
stage('Maven构建') {
def mvnHome = tool 'maventool'
def pipeStageId = 1130561945160317728
try{
sh "${mvnHome}/bin/mvn -f pom.xml clean package -X -U -Dmaven.test.skip=true"
preExecuteEndPipelineStageCheck(pipeStageId)
sleep(unit: 'MILLISECONDS', time: 500)
sh(script: toUpdatePipelineLogCurlSh(currentBuild.currentResult, '', getStageLog(currentBuild.rawBuild.log), pipeLogId, pipeStageId))
}catch(e){
result = 'FAILURE'
echo e.getMessage()
result = setPreExecuteResult(result, e.getMessage())
sleep(unit: 'MILLISECONDS', time: 500)
sh(script: toUpdatePipelineLogCurlSh(result, '', getStageLog(currentBuild.rawBuild.log), pipeLogId, pipeStageId))
sh(script: toEndPipelineLogCurlSh(result, pipeLogId, params.projectVersionConfigId, params.publishLogId))
throw e
}
}
}
node{
stage('Paas构建镜像') {
def pipeStageId = 1130561945160317729
try{
ceEnvKey = 'test'
ceRemoteAddress = 'http://10.145.208.200:31601'
imgNamespaceName = 'te-msc-paas'
repoName = 'te-msc-paas-gateway'
codePackagePath = '*.tar.gz'
hasAdditionalFile = false
curlUploadFilePara = ''
dockerFileType = 'INPUT'
dockerfilePath = ''
dockerFileContent = '''FROM hub.paas/base/centos-jdk8-cn:latest
ADD *.tar.gz /usr/local/gateway-service/
RUN chmod u+x /usr/local/gateway-service/bin/run.sh
EXPOSE 8888
CMD '/usr/local/gateway-service/bin/run.sh' '''
def codeFilePath = getCodeFilePath(codePackagePath)
def tagName = getImageNameByExactPath(codeFilePath, pipeLogId)
buildPaasImage(ceRemoteAddress, imgNamespaceName, repoName, tagName, codeFilePath, hasAdditionalFile, curlUploadFilePara, dockerFileType, dockerfilePath, dockerFileContent)
imageId = recordImageInfo(ceEnvKey, imgNamespaceName, repoName, tagName, isTagOrBranch, tagOrBranch, pipeLogId, pipeStageId)
preExecuteEndPipelineStageCheck(pipeStageId)
sleep(unit: 'MILLISECONDS', time: 500)
sh(script: toUpdatePipelineLogCurlSh(currentBuild.currentResult, '', getStageLog(currentBuild.rawBuild.log), pipeLogId, pipeStageId))
}catch(e){
result = 'FAILURE'
echo e.getMessage()
result = setPreExecuteResult(result, e.getMessage())
sleep(unit: 'MILLISECONDS', time: 500)
sh(script: toUpdatePipelineLogCurlSh(result, '', getStageLog(currentBuild.rawBuild.log), pipeLogId, pipeStageId))
sh(script: toEndPipelineLogCurlSh(result, pipeLogId, params.projectVersionConfigId, params.publishLogId))
throw e
}
}
}
node{
stage('paas发布部署') {
def pipeStageId = 1130561945161366306
try{
deployPaasApp(pipeStageId, '*.tar.gz', pipeLogId)
preExecuteEndPipelineStageCheck(pipeStageId)
sleep(unit: 'MILLISECONDS', time: 500)
sh(script: toUpdatePipelineLogCurlSh(currentBuild.currentResult, '', getStageLog(currentBuild.rawBuild.log), pipeLogId, pipeStageId))
sh(script: toEndPipelineLogCurlSh(currentBuild.currentResult, pipeLogId, params.projectVersionConfigId, params.publishLogId))
}catch(e){
result = 'FAILURE'
echo e.getMessage()
result = setPreExecuteResult(result, e.getMessage())
sleep(unit: 'MILLISECONDS', time: 500)
sh(script: toUpdatePipelineLogCurlSh(result, '', getStageLog(currentBuild.rawBuild.log), pipeLogId, pipeStageId))
sh(script: toEndPipelineLogCurlSh(result, pipeLogId, params.projectVersionConfigId, params.publishLogId))
throw e
}
}
}

node{
    stage('clean workspace') {
        cleanWs()
    }
}

Jenkins 自动化构建的更多相关文章

  1. Jenkins自动化构建python nose测试

    [本文出自天外归云的博客园] 简介 通过Jenkins自动化构建python nose测试分两步: 1. 创建节点(节点就是执行自动化测试的机器): 2. 创建任务并绑定节点(用指定的机器来跑我们创建 ...

  2. Jenkins自动化构建系列:01敏捷开发、自动化构建与持续集成

    <SVN与TortoiseSVN实战系列>已写完,今天新开一个<Jenkins自动化构建系列>,上周听了Bob Jiang老师的Agile1001公开课,一直想写个总结,这篇关 ...

  3. Gitlab+Jenkins用钩子实现git提交后jenkins自动化构建

    Gitlab+Jenkins用钩子实现git提交后jenkins自动化构建 一.Jenkins 进入项目---->Configure 1.设置项目代码从git中拉取 2.设置钩子程序 二.git ...

  4. Jenkins自动化构建配置

    写在前头 我每次修改代码一点东西,都要进行一个重新发布.重新发布的流程大概如下: 将最新代码同步上传到git上面 Maven打包 mvn clean install 将最新的jar包上传到服务器上面, ...

  5. 使用jenkins自动化构建android和ios应用

    背景 随着业务需求的演进,工程的复杂度会逐渐增加,自动化的践行日益强烈.事实上,工程的自动化一直是我们努力的目标,能有效提高我们的生产效率,最大化减少人为出错的概率,实现一些复杂的业务需求应变.场景如 ...

  6. 用Docker运行Jenkins自动化构建.NET Core项目

    目标 当代码提交到GitHub后,自动生成构建项目并部署到服务器.接下来介绍一下如何在容器中运行Jenkins,并自动化构建GitHub上的项目,使用自动化构建来解放你的双手. 前置条件 一台已经安装 ...

  7. Jenkins自动化构建(一)执行selenium+python脚本

    Jenkins执行python写的selenium自动化脚本,通常会遇到,执行打不开浏览器,查看jenkins构建Console Output控制台输出信息,发现脚本是执行了的,但是出错了,打开浏览器 ...

  8. Jenkins自动化构建vue项目然后发布到远程Linux服务器

    部署Jenkins参照另一篇博客: centos7安装Jenkins及其卸载 一.jenkins相关插件的安装 1.安装Publish Over SSH插件用于SSH连接远程的服务器. 登录 jenk ...

  9. Jenkins自动化构建PHP实列教程

    安装Jenkins 请参考群主的安装教程 进入jenkins,添加SSH server,并且安装gitlab,Generic Webhook Trigger Plugin,GitHub plugin, ...

  10. jenkins自动化构建iOS应用配置过程中遇到的问题

    最近配置jenkins来自动构建iOS应用,期间遇上不少问题.在这里分享给大家,也给自己留个底,方便下次解决问题. 首先说明下基本情况,我们因为部署jenkins的机器不是Mac,所以不能安装Xcod ...

随机推荐

  1. java日志框架系列(9):logback框架过滤器(filter)详解

    过滤器放在了logback-classic模块中. 1.logback-classic模块中过滤器 分类(2种):常规过滤器.TurboFilter过滤器. 1.常规过滤器 常规过滤器可以通过自定义进 ...

  2. Python——类和对象(二)

    一.实例方法和自动绑定self 在类中定义的实例方法,Python会自动绑定方法的第一个参数(通常是self,下文也默认为self),第一个参数总会指向调用该方法的对象,因为实例方法(包括构造方法)第 ...

  3. 串口控制RGB灯程序

    实验目的: 通过上位机给串口发送数据(字符); STM32收到数据进入中断程序原封不动返回上位机,并且根据收到的信息产出相应的进行操作.(1- led_on  2 – ledoff...); 源码   ...

  4. LeetCode 754. Reach a Number

    754. Reach a Number(到达终点数字) 链接:https://leetcode-cn.com/problems/reach-a-number/ 题目: 在一根无限长的数轴上,你站在0的 ...

  5. React/react相关小结

    React React组件由React元素组成,React组件使用React.Component或React.PureComponent来生成:React元素使用JSX的语法来编写或使用React.c ...

  6. 让一个父级div根据子级div高度而自适应高度

    需求是点击上传的时候进行子级div高度不定,相对来说父级div高度也不能固定,把元素都设置成普通标准流,然后样式可以使用margin内边距或者padding外边距来进行调节 放上代码供参考: .opu ...

  7. springboot项目命linux环境下命令启动

    测试环境:dev nohup java -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1099 \-Dcom.s ...

  8. 在网页中添加google搜索

    网页中插入谷歌搜索,至于怎么上谷歌,后面有时间会更,推荐百度 <form method="GET" action="http://www.google.com.hk ...

  9. leetcode-29.两数相除(不用乘除法和mod)

    如题,不用乘除法和mod实现两数相除. 这里引用一位clever boy 的解法. class Solution { public: int divide(int dividend, int divi ...

  10. MySQL Windows安装连接

    1.首先进入mysql的安装目录下的bin目录,例如我的是[C:\WINDOWS\System32\cmd.exe]   2.接着输入cmd,回车     3.在命令行中输入命令[mysql -hlo ...