gradle配置docker
// 阿波羅地址
def apolloMeta=''
// 當前項目環境
def profile = ''
if (project.hasProperty("pro")) {
apolloMeta='http://***:8021'
profile = 'pro'
} else if (project.hasProperty("pre")) {
apolloMeta='http://***:8021'
profile = 'pre'
} else if (project.hasProperty("test")) {
apolloMeta='http://***:8021'
profile = 'test'
} else if (project.hasProperty("dev")) {
apolloMeta='http://***:8021'
profile = 'dev'
} else {
apolloMeta='http://***:8021'
}
docker {
baseImage 'openjdk:8-jdk-alpine'
maintainer '做者名'
}
task dockerBuilder(type: Docker) {
applicationName = jar.baseName
volume('/tmp')
addFile("${jar.baseName}-${jar.version}.jar", "app.jar")
setEnvironment('TZ', "Asia/Shanghai")
runCommand("ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime > /etc/timezone")
setEnvironment('JAVA_OPTS', "-Dfile.encoding=UTF8")
entryPoint(["java",
// apollo 地址
"-Dapollo.meta=${apolloMeta}",
// 指定環境
"-Dspring.profiles.active=${profile}",
"-Djava.security.egd=file:/dev/./urandom"])
// 爲了中間插入命令更方便一點
defaultCommand (["-jar","app.jar"])
複製代碼
jenkins 配置
配置全局工具
新建任務
pipeline腳本
pipeline {
agent any
tools {// 定義工具,全局工具配置名稱
maven "maven3"
gradle "gradle49"
jdk "jdk1.8"
}
parameters { //定義參數
gitParameter branchFilter: 'origin/(.*)', defaultValue: 'dev', name: 'branch', type: 'PT_BRANCH'
}
environment {
//定義倉庫地址
REPOSITORY = "git路徑"
//項目名稱
PROJECTNAME = "projectName"
//group
GROUP = "com.*.*"
//harbor 存儲鏡像標籤
HARBORTAG = "docker私服地址/路徑"
//版本
VERSION = "1.0.0"
//端口
PORT = 8001
//代碼檢測平臺路徑
sonarqubeScannerHome = "/home/sonarqube/sonar-scanner-2.8/"
}
stages {
stage('獲取代碼') {
steps {
echo "start fetch code from git:${REPOSITORY}"
//清空當前目錄
deleteDir()
//拉去代碼
git branch: "${params.branch}", url: "${REPOSITORY}"
}
}
stage('代碼靜態檢查') {
steps {
//僞代碼檢查
echo "start code check"
//sh "${sonarqubeScannerHome}/bin/sonar-scanner"
}
}
stage('停用服-刪除鏡像') {
steps {
script {
def dockerCon = sh returnStdout: true, script: "docker ps -q -a -f name=.*{docker服務名稱}.*"
dockerCon = dockerCon.trim()
echo "${dockerCon}"
if (dockerCon == '') {
echo "沒有啓動服務"
} else {
echo "容器ID:${dockerCon}"
echo "----停用容器----"
sh "docker stop ${dockerCon}"
echo '----刪除容器-----'
sh "docker rm ${dockerCon}"
}
//是否有鏡像
def dockerimages = sh returnStdout: true, script: "docker images ${HARBORTAG}/${PROJECTNAME} -q"
if (dockerimages == '') {
echo "沒有鏡像"
} else {
echo "刪除鏡像"
sh "docker rmi ${dockerimages}"
}
}
}
}
stage('編譯+單元測試') {
steps {
echo "start compile"
//切換目錄
dir('工程目錄') {
//從新打包
sh "gradle clean build dockerBuilder -Ptest -Ptest -x test --refresh-dependencies --no-daemon"
}
}
}
stage('構建鏡像-發佈鏡像') {
steps {
echo "start build image"
dir('工程目錄') {
//build鏡像
//sh 'gradle dockerBuilder'
//鏡像重命名
sh "docker tag ${GROUP}/${PROJECTNAME}:${VERSION} ${HARBORTAG}/${PROJECTNAME}:${VERSION}"
//刪除老名字的鏡像
sh "docker rmi ${GROUP}/${PROJECTNAME}:${VERSION}"
//登陸Harbor
//sh "docker login -u admin -p 111111 harbor地址"
//推送鏡像
// sh 'docker push ${HARBORTAG}/${PROJECTNAME}:${VERSION}'
}
}
}
stage('啓動服務') {
steps {
echo "---啓動服務----"
sh "docker run -d --name ${PROJECTNAME} -v /home/pinpoint:/usr/local/pinpoint:rw -p ${PORT}:${PORT} -t ${HARBORTAG}/${PROJECTNAME}:${VERSION} -javaagent:/usr/local/pinpoint/pinpoint-bootstrap-1.8.2.jar -Dpinpoint.agentId=*** Dpinpoint.applicationName=*** -jar app.jar"
}
}
}
post {
failure {
script {
echo "$BUILD_ID $BUILD_NUMBER"
//失敗了發送釘釘機器人信息
sh "/home/lxsh/dingding.sh 任務名稱 $BUILD_ID"
}
}
}
}
複製代碼