pipeline in k8

1.环境

# Jenkins/github in k8s,hub是阿里云 

插件:
kubernets
pipeline
docker pipeline
docker

Jenkins源:https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

2.配置

Dashboard > 系统管理 > 节点管理 > configureClouds

3.配置k8s

4.输入集群地址

5.连接测试

6.配置Jenkins地址

7.配置slave的信息

8.配置slave的镜像(包含了docker跟kubectl镜像)

9.配置maven

10.挂载docker文件,可以给maven下载的包做持久化

pipeline {
  
    agent 
        {
        label 'jnlp-slave'
        }
    
    stages {
        stage('Source') {
            steps {
                sh 'git clone http://192.168.90.170:32187/root/demo-java.git'
            }
        }
        stage('Build package') {
            steps {
                container('maven') {
                    sh ' mvn clean  package  -Dmaven.test.skip=true'
                    //打包跳过测试
                }
            }
        }
        stage('build  push  image') {
            steps {
                container('jnlp-slave') {
                    script {

                        //git commit的tag前七位
                        git_commit = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim()
                        
                        //第一个是hub地址,aliyun-docker-registry是hub的账号密码
                        docker.withRegistry('https://registry.cn-hangzhou.aliyuncs.com','aliyun-docker-registry') {

                        //build镜像  tag镜像
                        def customImage = docker.build("s-ops/my-image:${env.BUILD_NUMBER}-${git_commit}")
                        //push镜像
                        customImage.push()
                         
                        //删除宿主机的镜像
                        sh "docker rmi s-ops/my-image:${env.BUILD_NUMBER}-${git_commit}"
                        }
                    }
                }
            }
        }
    }
}
# docker pipeline使用: https://www.jenkins.io/doc/book/pipeline/docker/
posted @ 2022-04-05 15:39  等等马上就好  阅读(65)  评论(0编辑  收藏  举报