imirsh

导航

Jenkins Pipeline 部署 Kubernetes 应用

项目说明

部署思路

  • 克隆代码
  • 编译打包
  • 构建镜像
  • 将镜像推送到本地镜像仓库
  • 修改 k8s.yaml 文件的 tag
  • 应用资源清单 k8s.yaml
podTemplate(cloud: 'kubernetes',containers: [
    containerTemplate(name: 'tools', image: 'registry.cn-beijing.aliyuncs.com/dengyou/kube-ops:tools', ttyEnabled: true, command: 'cat'), 
    containerTemplate(name: 'maven3', image: 'registry.cn-beijing.aliyuncs.com/dengyou/kube-ops:maven3', command: 'cat', ttyEnabled: true),
    ],
    volumes: [
        hostPathVolume(hostPath: '/var/run/docker.sock', mountPath: '/var/run/docker.sock'),
        hostPathVolume(hostPath: '/root/.kube/', mountPath: '/root/.kube/'),
        hostPathVolume(hostPath: '/root/.docker', mountPath: '/root/.docker'),
        hostPathVolume(hostPath: '/data/repo', mountPath: '/usr/local/apache-maven/repo')
        // /data/repo 要手动创建出来,并给与相应的权限
        // hostPathVolume(hostPath: '/etc/hosts',mountPath: '/etc/hosts'),
        // hostPathVolume(hostPath: '/etc/docker//etc/docker/certs.d/',mountPath: '/etc/docker//etc/docker/certs.d/')  
    ]
)
{
   node(POD_LABEL) {
        stage('Clone'){
            container('tools') {
                echo "1. Clone Stage"
                git url: "https://github.com/imirsh/mvnweb-demo.git"
                script {
                    build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
                }
            }
        }
        stage('Package') {
            container('maven3') {
                echo "3. Package From Source Code"
                sh "mvn install"
            }
        }
        stage('Build') {
            container('tools') {
                echo "3. Build Docker Image Stage"
                sh "docker build -t registry.cn-beijing.aliyuncs.com/dengyou/mvnweb-demo:${build_tag} ."
                 // sh "docker build -t reg.linux.io/mvnweb/mvnweb-demo:${build_tag}  ."
            }
        }
        stage('Push') {
            container('tools') {
                echo "4. Push Docker Image Stage"
                // withCredentials([usernamePassword(credentialsId: 'registry.cn-beijing.aliyuncs.com_Auth', usernameVariable: 'aliUser',  passwordVariable: 'aliPassword')]) {
                //     sh "docker login -u ${aliUser} -p ${aliPassword} registry.cn-beijing.aliyuncs.com"
                //     sh "docker push registry.cn-beijing.aliyuncs.com/dengyou/jenkins-demo:${build_tag}"
                // }
                sh "docker push registry.cn-beijing.aliyuncs.com/dengyou/mvnweb-demo:${build_tag}"
                // sh "docker push  reg.linux.io/mvnweb/mvnweb-demo:${build_tag} "
        }
      }
        stage('YAML') {
            container('tools'){
                sh "sed -i 's@<BUILD_TAG>@${build_tag}@g' k8s.yaml"
                sh '/usr/local/bin/kubectl apply -f k8s.yaml --record'
            }
        }
    }
}

posted on 2020-07-17 17:54  imirsh  阅读(807)  评论(0编辑  收藏  举报