一 说明
前面所有配置都跟jenkins部署k8s项目-CICD这篇配置一致,唯一不同的是发布脚本不一样
// 公共
def registry = "huningfei"
// 项目
def project = "demo-test"
def app_name = "demo"
def image_name = "${registry}/${project}:${BUILD_NUMBER}"
def git_address = "https://gitee.com/huningfei/demo-test.git"
// 认证
def secret_name = "registry-pull-secret"
def docker_registry_auth = "450fb8b4-62c9-43fe-8a69-ec30705e724e"
def git_auth = "b363af0f-a96c-465c-a14e-60f7127727eb"
def k8s_auth = "a60b4b7f-2aef-4622-94a5-6efef67c4ac9"
node(){
// 第一步
stage('拉取代码'){
checkout([$class: 'GitSCM', branches: [[name: '${Branch}']], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_address}"]]])
}
// 第二步
stage('代码编译'){
sh 'sudo su root -c "mvn clean package -Dmaven.test.skip=true"'
sh 'sudo su root -c "chown -R tomcat:tomcat /home/tomcat/.jenkins/workspace/k8s-demo-test"'
}
// 第三步
stage('构建镜像'){
withCredentials([usernamePassword(credentialsId: "${docker_registry_auth}", passwordVariable: 'password', usernameVariable: 'username')]) {
sh """
echo '
FROM java:8
WORKDIR /home/tomcat/.jenkins/workspace/k8s-demo-test
COPY target/demo-0.0.1-SNAPSHOT.jar /demo-0.0.1-SNAPSHOT.jar
CMD java -jar /demo-0.0.1-SNAPSHOT.jar
'> Dockerfile
docker build -t ${image_name} .
docker login -u ${username} -p '${password}'
docker push ${image_name}
"""
}
}
// 第四步
stage('部署到K8S平台'){
sh """
sed -i 's#$IMAGE_NAME#${image_name}#' deploy.yml
sed -i 's#$SECRET_NAME#${secret_name}#' deploy.yml
"""
kubernetesDeploy configs: 'deploy.yml', kubeconfigId: "${k8s_auth}"
}
}