jenkins 流水线 pipeline 构建 docker

pipeline {
    agent any
    tools {
        maven 'MAVEN3.8.6' 
    }
	parameters {
	  string defaultValue: 'app', description: '服务名', name: 'appName', trim: true
	  string defaultValue: '0.1', description: '服务版本', name: 'appVesion', trim: true
	  gitParameter branch: '', branchFilter: 'origin/(.*)', defaultValue: 'master', description: '选择分支', name: 'branch', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'GitParameterDefinition'
	}
    stages {
        stage('拉取代码') {
            steps {
                echo "选择分支 >>  ${params.branch} "
                git branch: "${params.branch}", url: 'https://gitee.com/xxxxx.git'
            } 
        }
        stage('编译代码') {
            steps {
                sh "mvn package -Dmaven.test.skip=true"
            } 
        }
        stage('打包镜像') {
            steps {
                echo "服务名 >>  ${params.appName} "
                echo "服务版本 >>  ${params.appVesion} "
                sh '''
                    SERVER_NAME=$appName
                    CONTAINER_ID=`docker ps |grep $SERVER_NAME |awk \'{print $1}\'`
                    IMAGE_ID=`docker images |grep $SERVER_NAME |awk \'{print $3}\'`
                    echo "CONTAINER_ID : $CONTAINER_ID"
                    echo "SERVER_NAME : $SERVER_NAME"
                    echo "IMAGE_ID : $IMAGE_ID"
                    if [ "$IMAGE_ID" !=  "" ] ; then
                    	if [ "$CONTAINER_ID" !=  "" ] ; then
                    		CONTAINER_INFO=`docker ps |grep $SERVER_NAME |awk \'{print $0}\'`
                    		echo "删除容器 $CONTAINER_INFO"
                    		docker rm -f $CONTAINER_ID
                    	fi
                    	IMAGE_INFO=`docker images |grep app |awk \'{print $0}\'`
                    	echo "删除镜像 $IMAGE_INFO"
                    	docker rmi -f $IMAGE_ID
                    fi
                '''
            } 
        }
		stage('启动镜像') {
            steps {
                sh '''
                    SERVER_NAME=$appName
                    SERVER_VERSION=$appVesion
                    cd $WORKSPACE
                    docker build -t $SERVER_NAME:$SERVER_VERSION .
                    docker run -d -p 18080:8080 $SERVER_NAME:$SERVER_VERSION
                '''
            } 
        }
        
    }
}

jenkins 流水线 pipeline 构建 docker_第1张图片

你可能感兴趣的:(工具,部署,JAVA,jenkins,运维,docker)