实现方式:定义自己的函数,实现命令的前面添加 ’#!/bin/sh -e\n’

def mysh(cmd, returnStatus) {
    return sh (script: '#!/bin/sh -e\n'+ cmd, returnStatus: returnStatus)
}
pipeline {
    agent any
    tools {
        jdk 'jdk1.8'
    }
    stages {
        stage("清空工作目录") {
            steps {
                deleteDir()
            }
        }
        stage("拉取代码") {
            steps {
                git branch: "${branch}", credentialsId: '', url: ""
                buildName '#${BUILD_NUMBER}--( 发布服务器ip:${server})'
            }
        }
        stage("编译构建") {
            steps {
                script {
                    mysh ("mvn  clean package -Dmaven.test.skip=true",true)
                }
            }
        }
        stage('打包镜像') {
            steps {
                script {
                    mysh ('''
                        archive_path=`find ./ -maxdepth 3  -name  "*.jar"`
                        archive_file=$(echo ${archive_path}|awk -F"/" '{print $NF}')
                        image_name=repository.jcfc.cn:9443/image-${runenv}-${archive_file}
                        image_full_name=${image_name}:${BUILD_NUMBER}
                        echo "-------------变量-------------"
                        echo "制品路径:${archive_path}"
                        echo "制品名称:${archive_file}"
                        echo "镜像名名称:${image_full_name}"
                        echo "------------------------------"
                        echo " "
                        echo "------------------------------"
                        echo "1. 创建镜像打包目录:"
                        mkdir -p ./buildimage && cp ${archive_path} ./buildimage && cd ./buildimage
                        pwd
                        echo " "
                        echo "2. 生成启动脚本:"
                        echo "java -jar ${jvm_args} ./$archive_file ${run_args}" > start.sh && chmod +x ./start.sh
                        cat ./start.sh
                        echo " "
                        echo "3. 下载dockerfile"
                        echo " "
                        wget http://xxxx.cn/repository/soft/dockerfile/base-Dockerfile
                        echo " "
                        echo "4. 开始构建镜像:"
                        echo "命令:docker build -t ${image_full_name} -f ./base-Dockerfile ."
                        docker build -t ${image_name} -f ./base-Dockerfile .
                        echo "------------------------------"
                        echo " "
                        echo "构建镜像完成。"
                        docker images|grep ${image_name}
                    ''',true)
                }
            }
        }
    }
}

发表评论

邮箱地址不会被公开。