def createVersion() {
// 定义一个版本号作为当次构建的版本,输出结果 20191210175842_69
return new Date().format('yyyyMMddHHmmss') + "_${env.BUILD_ID}"
}
pipeline {
agent any
environment {
//JCR_CREDS = credentials(' 61cf61c6-4e2a-47ed-8d29-adb2d14d07ed')
GIT_CREDS = credentials('e32346ef-255b-407a-9425-84a358e594c6')
servicename = "message"
imageurl = "192.168.1.252:8443/dev/"
tag = createVersion()
}
stages {
stage('git colne') {
steps {
git branch: 'dev', credentialsId: 'e32346ef-255b-407a-9425-84a358e594c6', url: 'http://192.168.1.254:81/mp/message.git'
}
}
stage('maven清理并编译') {
steps {
sh '''
/var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/maven3/bin/mvn clean compile package -U
'''
}
}
stage('create dockerfile'){
steps {
sh """
echo '''FROM 192.168.1.252:8443/library/jiuqi_jdk:201
WORKDIR /home
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
COPY mp-message-bootstrap/target/mp-message-bootstrap-*.jar mp-message-bootstrap.jar
EXPOSE 8080
ENV JAVA_TOOL_OPTIONS="-Xms512m -Xmx512m -Dspring.profiles.active=dev -Dspring.cloud.nacos.server-addr=192.168.1.253:8848"
ENTRYPOINT java -jar \$JAVA_TOOL_OPTIONS mp-message-bootstrap.jar
''' > Dockerfile
"""
}
}
stage('docker build and push register'){
steps{
script{
sh '''
containerid=$(docker ps -a | grep $servicename | awk '{print $1}')
if [ "$containerid" != "" ];then
docker rm -f $containerid
fi
imageid=$(docker images | grep $servicename | awk '{print $3}')
if [ "$imageid" != "" ];then
docker rmi $imageid
fi
docker build -t ${imageurl}${servicename}:${tag} .
docker push ${imageurl}${servicename}:${tag}
'''
}
}
}
stage('ansible run docker container') {
steps{
sh '''
ansible-playbook /var/jenkins_home/ansible_script/deploy.yml -e "servicename=${servicename} imagename=${imageurl}${servicename}:${tag} port=8080"
'''
}
}
}
}