pipeline docker 方式
2022-12-06
简单示例
pipeline {
agent {
label 'etek'
//docker {
// label 'etek'
// image 'maven:3.8.1-adoptopenjdk-11'
// args '-v /home/jenkins/key:/root/key'
// }
}
// setting default value to using cache
// parameters { booleanParam(name: 'useCache', defaultValue: true, description: '') }
// Poll scm every 3 minutes to trigger build.
triggers {
# 自动执行,每3分钟执行一次;
pollSCM 'H/3 * * * *'
}
// keep 30 days, 200 builds.
options {
buildDiscarder(logRotator(artifactDaysToKeepStr: '',
artifactNumToKeepStr: '', daysToKeepStr: '30', numToKeepStr: '200'))
timestamps()
}
// global environment
environment {
NAMESPACE = 'etek'
DEFAULT_IMAGE_TAG = '1.0.0-snapshot'
ALI_REGISTRY = 'harbor.xxxx.com'
ALI_RREGISTRY_CREDENTIAL_ID = 'harbor-usrpwd'
BRANCH_PREFIX = 'main'
TAG_PREFIX = 'v'
NEED_BUILDING = true
NEED_DEPLOY = true
EMAIL = 'xxxx@xxxx.com.cn'
}
stages {
stage ('Prepare'){
steps{
script{
def branch_name = env.BRANCH_NAME
if (branch_name.startsWith("${BRANCH_PREFIX}")) {
def job_names = env.JOB_NAME =~ /.*\/(.*)\//
env.JOB_NAME = job_names[0][1]
echo "JOB_NAMES is ${JOB_NAME}"
NEED_DEPLOY = false
NEED_BUILDING = false
}else if (branch_name.startsWith("${TAG_PREFIX}")) {
def tag_judge = branch_name.indexOf('_')
def tag_name = branch_name =~ /_(.*)/
def TAG_NAME = tag_name[0][1]
def tag_image = branch_name =~ /v(.*)_/
def TAG_IMAGE = tag_image[0][1]
service_name = "${TAG_NAME}"
if (tag_judge == -1){
echo 'tag is not right'
NEED_DEPLOY = false
NEED_BUILDING = false
}else if (TAG_IMAGE.startsWith("${DEFAULT_IMAGE_TAG}")){
env.DOCKERFILE = "Dockerfile-${TAG_NAME}"
echo "Tag New TAG_NAME ${TAG_NAME}"
echo "Tag New TAG_IMAGE ${TAG_IMAGE}"
echo "Tag New DOCKERFILE ${DOCKERFILE}"
IMAGE_URL = "${ALI_REGISTRY}/${NAMESPACE}/${TAG_NAME}:${DEFAULT_IMAGE_TAG}"
echo "${IMAGE_URL}"
}else{
env.DOCKERFILE = "Dockerfile-${TAG_NAME}"
echo "Tag New TAG_NAME ${TAG_NAME}"
echo "Tag New TAG_IMAGE ${TAG_IMAGE}"
echo "Tag New DOCKERFILE ${DOCKERFILE}"
IMAGE_URL = "${ALI_REGISTRY}/${NAMESPACE}/${TAG_NAME}:${TAG_IMAGE}"
echo "${IMAGE_URL}"
NEED_DEPLOY = false
}
}else{
echo "${branch_name} no need to build"
NEED_BUILDING = false
NEED_DEPLOY = false
}
}
}
}
stage('check and 拉取代码') {
steps {
# checkout scm 实现代码的检测并下载代码;
checkout scm
}
}
stage('Build'){
when {
expression {
NEED_BUILDING
}
}
steps{
sh 'printenv'
script {
# 这里使用docker镜像,并且挂载了主机的 /home/yangmuhan/key 目录
# 挂载 docker sock 文件,用来实现执行docker命令;
docker.image('docker').inside('-v /home/yangmuhan/key:/key -v /var/run/docker.sock:/var/run/docker.sock' ) {
sh """
cp /key/subject-pro.json etek-srv-func-gateway-access/src/main/resources/
# sleep 3600
echo ${DOCKERFILE} ${IMAGE_URL}
docker build -t ${IMAGE_URL} --no-cache -f ${DOCKERFILE} .
docker login -uuser -ppassword harbor.xxx.com # 这里可以使用jenkins的密钥管理方式
docker push ${IMAGE_URL}
"""
//docker.withRegistry("https://${IMAGE_URL}", "${ALI_RREGISTRY_CREDENTIAL_ID}") {
// def myImage = null
// myImage = docker.build("${IMAGE_URL}", "--no-cache -f ${DOCKERFILE} .")
// //sh """
// //sleep 3600
// //"""
// myImage.push()
//}
}
container_id = sh (
script: "docker create ${IMAGE_URL} ",
returnStdout: true
).trim()
echo "Container Id: ${container_id}"
sh """
docker cp $container_id:/apps/app.jar ./app.jar
mv app.jar ${TAG_NAME}.jar
docker rm -v $container_id
"""
}
}
}
stage('Deploy'){
when {
expression {
NEED_DEPLOY
}
}
steps {
script{
build job: '../redeploy-pipeline',
parameters:[
string(name: 'service_name', value: "${service_name}"),
],
wait:false
}
}
}
}
post {
always {
archiveArtifacts artifacts: '*.jar', onlyIfSuccessful: true
}
success {
echo '恭喜您,构建成功!!!'
mail subject: "'${env.JOB_NAME} [${env.BUILD_NUMBER}]' 执行成功",
body: """
<font color="red">(本邮件是程序自动下发的,请勿回复!)</font><br/>
项目名称:${env.JOB_NAME}<br/>
构建编号:${env.BUILD_NUMBER}<br/>
构建结果:${currentBuild.result} <br/>
构建时长:${currentBuild.duration}ms <br/>
构建地址:<a href="${env.BUILD_URL}">${env.BUILD_URL}${env.BUILD_NUMBER}/</a><br/>
""",
charset: 'utf-8',
from: 'ops@ce-service.com.cn',
mimeType: 'text/html',
to: "$EMAIL"
}
failure {
echo '抱歉,构建失败!!!'
mail subject: "'${env.JOB_NAME} [${env.BUILD_NUMBER}]' 执行失败",
body: """
<font color="red">(本邮件是程序自动下发的,请勿回复!)</font><br/>
项目名称:${env.JOB_NAME}<br/>
构建编号:${env.BUILD_NUMBER}<br/>
构建结果:${currentBuild.result} <br/>
构建时长:${currentBuild.duration}ms <br/>
构建地址:<a href="${env.BUILD_URL}">${env.BUILD_URL}${env.BUILD_NUMBER}/</a><br/>
""",
charset: 'utf-8',
from: 'ops@ce-service.com.cn',
mimeType: 'text/html',
to: "$EMAIL"
}
unstable {
echo '该任务已经被标记为不稳定任务....'
}
changed {
echo ''
}
}
}
链接
https://www.jenkins.io/doc/book/pipeline/docker/
https://docs.cloudbees.com/docs/admin-resources/latest/plugins/docker-workflow
https://cloud.tencent.com/developer/article/1830662
自动化集成:Pipeline整合Docker容器 - 腾讯云开发者社区-腾讯云 (tencent.com)
(97条消息) Pipeline 中使用 Docker 构建示例_归-尘的博客-CSDN博客_docker pipeline