Jenkins on Kubernetes的一个例子
文章目录
效果图
pipeline:
def label = "k8s-slave-${UUID.randomUUID().toString()}"
podTemplate(label: 'devops', cloud: "kubernetes", containers: [
containerTemplate(
name: 'jnlp',
image: '192.168.233.214/jenkins/jenkins-mvn:v3.0',
alwaysPullImage: false,
privileged: false,
args: '${computer.jnlpmac} ${computer.name}')
],
namespace: 'devops',serviceAccount: 'jenkins',automountServiceAccountToken: 'true',
volumes: [
hostPathVolume(hostPath: '/var/run/docker.sock', mountPath: '/var/run/docker.sock'),
hostPathVolume(hostPath: '/usr/share/zoneinfo/Asia/Shanghai',mountPath: '/etc/localtime'),
persistentVolumeClaim(claimName: 'jnlp-mvn', mountPath: '/home/jenkins/.m2repo')
]) {
node('devops') {
//Define all variables
def project = "${项目}"
def appName = "${应用名}"
def serviceName = "${应用名}"
def imageVersion = "${版本}"
def namespace = "${命名空间}"
def feSvcName = "${应用名}"
def httptype = "${链接类型}"
def repourl = "${代码链接}"
def kind = "${服务类型}"
def imageTag = "${镜像仓库}/${项目}/${应用名}:${版本}"
def image = "${镜像仓库}/${项目}/${应用名}"
def env = "${环境}"
def branch = "${分支}"
stage('Get Date') {
container('jnlp') {
sh 'date'
sh 'git --version'
}
}
stage('Checkout') {
container('jnlp') {
switch (httptype) {
case "Subversion":
script {
//从SVN拉取代码
def scmVars = checkout ([$class: 'SubversionSCM',
additionalCredentials: [],
excludedCommitMessages: '',
excludedRegions: '',
excludedRevprop: '',
excludedUsers: '',
filterChangelog: false,
ignoreDirPropChanges: false,
includedRegions: '',
locations: [[credentialsId: '948fad5d-f9ed-460c-974e-65d016300c5f',
depthOption: 'infinity',
ignoreExternalsOption: true,
local: '.',
remote: "${repourl}"]],
workspaceUpdater: [$class: 'UpdateUpdater']])
svnversion = scmVars.SVN_REVISION
}
break
case "Gitlab":
script {
checkout([$class: 'GitSCM',
branches: [[name: "${branch}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [[
credentialsId: "Gitlab",
url: "${repourl}"
]]
])
}
sh "ls"
break
default:
println "none"
break
}
}
}
stage('Check Secret') {
container('jnlp') {
sh "echo secret"
sh "ls"
}
}
stage('Package Maven') {
container('jnlp') {
sh "echo ${branch} && ls"
sh "mvn clean package -U -P${env}"
sh "ls"
}
}
stage('Docker Build Push'){//自定义仓库配置编译镜像推送到仓库
//sh 'mv ${WORKSPACE}/target/helloworld-0.0.1-SNAPSHOT.jar .'
withDockerRegistry(credentialsId: 'harbor-cert', url: 'http://192.168.233.214'){
def customImage = docker.build("${imageTag}", '.')
customImage.push()
}
}
stage('Deploy Kubernetes') {
container('jnlp') {
switch (env) {
case "dev":
// Create namespace if it doesn't exist
withKubeConfig(caCertificate: '', clusterName: '', contextName: '', credentialsId: 'f85e5cfd-225c-4484-9c41-8494739cec99', namespace: 'devops', serverUrl: 'https://192.168.233.253:8443') {
sh("""kubectl get ${kind} ${appName} -o yaml -n ${namespace} | sed "s%${image}:.*\$%${image}:${imageVersion}%" | kubectl replace -f -""")
//Update the imagetag to the latest version
//Create or update resources
sh("kubectl get pod -n ${namespace}")
//Grab the external Ip address of the service
}
break
case "pro":
withKubeConfig(caCertificate: '', clusterName: '', contextName: '', credentialsId: 'f85e5cfd-225c-4484-9c41-8494739cec99', namespace: 'devops', serverUrl: 'https://192.168.233.253:8443') {
sh("""kubectl get ${kind} ${appName} -o yaml -n ${namespace} | sed "s%${image}:.*\$%${image}:${imageVersion}%" | kubectl replace -f -""")
//Update the imagetag to the latest version
//Create or update resources
sh("kubectl get pod -n ${namespace}")
//Grab the external Ip address of the service
}
break
default:
withKubeConfig(caCertificate: '', clusterName: '', contextName: '', credentialsId: 'f85e5cfd-225c-4484-9c41-8494739cec99', namespace: 'devops', serverUrl: 'https://192.168.233.253:8443') {
sh("echo Value N/A")
}
}
stage('post') {
container('jnlp') {
script {
title = "[Jenkins CI]"
text = """
|\t[Jenkins CI]
|项目名称:${project}
|构建状态:${currentBuild.currentResult}
|工程名称:${appName}
|镜像标签:${image:imageTag}
""".stripMargin().replaceAll("\n", "\\\\n")
}
sh """echo ${text}"""
sh """
curl -s -X POST -H Content-Type:application/json -d '{"msgtype": "text", "title": "${title}", "text": {"content": "${text}"}}' 'https://oapi.dingtalk.com/robot/send?access_token=**********************'
"""
}
}
}
}
}
}
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论