从Jenkinsfile构建到k8s部署

def registry = "harbor.uat.testtest.com.cn" def project = "uat" def app_name = "springcloud-eureka-api" def jenkins_slave_image = "${registry}/uat/jenkins-slave-maven-centos7:v1" def image_name = "${registry}/${project}/${app_name}:${BUILD_NUMBER}" def git_address = "http://gitlab.gic.testtest.com.cn/springcloud-framework/springcloud-eureka.git" def docker_registry_auth = "harbor" def git_auth = "gitlab-gic" def k8s_auth = "kube-config-rancher" def k8s_cloud_name = "rancher" def eureka_access = 'defaultZone: http://${eureka.host1}:8010/eureka/,http://${eureka.host2}:8010/eureka/' def jar_name = 'springcloud-eureka.jar' def label_name = "jenkins-slave-${app_name}" podTemplate(label: "$label_name", cloud: "$k8s_cloud_name", containers: [ containerTemplate( name: 'jnlp', image: "${jenkins_slave_image}" ), ], volumes: [ hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'), hostPathVolume(mountPath: '/usr/bin/docker', hostPath: '/usr/bin/docker'), hostPathVolume(mountPath: '/home/jenkins/.m2', hostPath: '/root/.m2'), hostPathVolume(mountPath: '/home/jenkins/dockerfile-yaml', hostPath: '/dockerfile_yaml') ], ) { node("$label_name"){ stage('拉取代码'){ //checkout([$class: 'GitSCM', branches: [[name: '${Branch}']], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_address}"]]]) git branch: "$Branch",credentialsId: "$git_auth", url: "$git_address" //sh """ //rm -rf Jenkinsfile //mv Dockerfile /home/jenkins/ //mv deploy.yaml /home/jenkins/ //""" } stage('代码编译'){ //sh "/opt/rh/rh-maven33/root/usr/bin/mvn clean package -Dmaven.test.skip=true" sh """ echo "root@123" |su root -c "chown default.root /home/jenkins -R" sed -i 's#defaultZone.*#${eureka_access}#g' src/main/resources/application.yml sed -i 's#file: logs#file: /logs#g' src/main/resources/application.yml /opt/rh/rh-maven33/root/usr/bin/mvn clean install -DskipTests echo build """ } stage('构建镜像'){ //harbor登录 withCredentials([usernamePassword(credentialsId: "${docker_registry_auth}", passwordVariable: 'password', usernameVariable: 'username')]) { sh """ echo "root@123" |su root -c "chmod 666 /var/run/docker.sock" mv target/$jar_name ./ cp /home/jenkins/dockerfile-yaml/Dockerfile ./ sed -i 's#springcloud.jar#${jar_name}#g' Dockerfile docker build -t ${image_name} ./ docker login -u ${username} -p '${password}' '${registry}' docker push ${image_name} """ } } stage('部署到K8S平台'){ //sh "sleep 3600" //kubernetesDeploy configs: 'deploy.yaml', kubeconfigId: "${k8s_auth}" //sh "mkdir /home/jenkins/.kube/" //sh "echo '${k8s_auth}' >/home/jenkins/.kube/config" sh """ cp /home/jenkins/dockerfile-yaml/${app_name}/${project}/${app_name}1.yaml ./ sed -i 's#\$IMAGE_NAME#${image_name}#g' ${app_name}1.yaml sed -i 's#\$APP_NAME#${app_name}#g' ${app_name}1.yaml sed -i 's#namespace: sit#namespace: ${project}#g' ${app_name}1.yaml cp ${app_name}1.yaml /home/jenkins/dockerfile-yaml/nacos/${project}/ cp /home/jenkins/dockerfile-yaml/${app_name}/${project}/${app_name}2.yaml ./ sed -i 's#\$IMAGE_NAME#${image_name}#g' ${app_name}2.yaml sed -i 's#\$APP_NAME#${app_name}#g' ${app_name}2.yaml sed -i 's#namespace: sit#namespace: ${project}#g' ${app_name}2.yaml cp ${app_name}2.yaml /home/jenkins/dockerfile-yaml/nacos/${project}/ """ //sh "kubectl apply -f /home/jenkins/dockerfile-yaml/${app_name}/${app_name}.yaml" kubernetesDeploy configs: "${app_name}1.yaml", kubeconfigId: "${k8s_auth}" kubernetesDeploy configs: "${app_name}2.yaml", kubeconfigId: "${k8s_auth}" } } }
上一篇:深入浅出 -- 系统架构之微服务架构


下一篇:python学习24:python中的列表(list)的遍历