实现代码打包编译
容器镜像构建
jenkins编译采用docker构建。
遇到问题:
1.需要限制docker 容器的内存和cpu
docker {
image 'ccr.ccs.tencentyun.com/libary/maven:3.6.3-jdk-8'
args "-v ${WORKSPACE}:/workspace --memory='2048m' --cpus='1'"
}
2.jenkins构建需要限制构建时间。避免job任务构建时间太长
timeout(time: 30, unit: 'MINUTES') { //限制30分钟的构建时间。超过30分钟自动断开
bash
pipeline {
agent any
parameters {
choice(
name: 'GIT_BUILD_REF',
choices: ['test', 'master'],
description: 'Select the Git branch or commit to build'
)
}
environment {
GIT_REPO_URL = "http://192.168.1.2/djb/djb-cloud.git"
HARBOR_REGISTRY = "192.168.2.1:5000"
HARBOR_HOST= "example.com"
DOCKER_BUILD_CONTEXT = './module-system/djb-module-system-biz'
CODING_DOCKER_IMAGE_NAME = 'ops/djb-module-system'
DOCKERFILE_PATH = './module-system/djb-module-system-biz/Dockerfile'
HARBOR_CREDENTIALS_ID = "fd3362aa-xx" //凭据id
CREDENTIALS_ID = "0ee6d913-xxx"
DEPLOYMENT_NAME = "djb-module-system-deploy"
CONTAINER_NAME = "djb-module-system-deploy"
WEBHOOK_URL ="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxx123"
}
stages {
stage('检出') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: "${params.GIT_BUILD_REF}"]],
userRemoteConfigs: [[
url: "${env.GIT_REPO_URL}",
credentialsId: "${env.CREDENTIALS_ID}"
]]])
script {
// 获取当前的Git提交ID
env.GIT_COMMIT_ID = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
// 获取当前时间
env.CURRENT_TIME = sh(script: 'date +%Y%m%d%H%M%S', returnStdout: true).trim()
// 生成版本号
env.DOCKER_IMAGE_VERSION = "${params.GIT_BUILD_REF}-${env.GIT_COMMIT_ID}-${env.CURRENT_TIME}"
}
}
}
stage('build') {
agent {
docker {
image 'ccr.ccs.tencentyun.com/libary/maven:3.6.3-jdk-8'
args "-v ${WORKSPACE}:/workspace --memory='2048m' --cpus='1'"
}
}
steps {
script {
timeout(time: 30, unit: 'MINUTES') {
sh "cd /workspace && ls -l"
sh 'pwd'
sh "ls -l && mvn -v"
sh 'cd /workspace && pwd && mvn clean package -Dmaven.test.skip=true'
}
}
}
}
stage('构建镜像并推送到 Harbor Docker 制品库') {
steps {
script {
// 使用 withRegistry 来处理 HTTP 登录
docker.withRegistry("https://${env.HARBOR_REGISTRY}", "${env.HARBOR_CREDENTIALS_ID}") {
def dockerImage = docker.build("${env.CODING_DOCKER_IMAGE_NAME}:${env.DOCKER_IMAGE_VERSION}", "-f ${env.DOCKERFILE_PATH} ${env.DOCKER_BUILD_CONTEXT}")
dockerImage.push()
env.IMAGE_NAME = "${env.HARBOR_HOST}/${env.CODING_DOCKER_IMAGE_NAME}:${env.DOCKER_IMAGE_VERSION}"
}
}
}
}
stage('部署到 Kubernetes') {
when {
expression { return params.GIT_BUILD_REF == 'test' }
}
steps {
script {
// 使用 withKubeConfig 来处理 Kubernetes 配置
withKubeConfig(credentialsId: 'tke-cluster-credentials') {
sh "kubectl set image deployment/${env.DEPLOYMENT_NAME} ${env.CONTAINER_NAME}=${env.IMAGE_NAME} --record"
}
}
}
}
}
post {
success {
script {
def message = """{
"msgtype": "text",
"text": {
"content": "Jenkins 部署成功!\\n部署镜像号: ${env.IMAGE_NAME}\\n部署状态:\\n${env.DEPLOYMENT_NAME}:成功"
}
}"""
sh """
curl -X POST ${WEBHOOK_URL} \
-H 'Content-Type: application/json' \
-d '${message}'
"""
}
}
failure {
script {
def message = """{
"msgtype": "text",
"text": {
"content": "Jenkins 部署失败!\\n部署镜像号: ${env.IMAGE_NAME}\\n部署状态:\\n${env.DEPLOYMENT_NAME}:失败"
}
}"""
sh """
curl -X POST ${WEBHOOK_URL} \
-H 'Content-Type: application/json' \
-d '${message}'
"""
}
}
}
}