使用 Jenkins Pipeline实现持续集成与交付(CI/CD)
文章目录前言环境配置在Jenkins的服务器上安装好jdk在Jenkins的服务器上安装好maven在Jenkins的服务器上安装好dockerGlobal Tool Configuration(全局工具配置)配置pod template(kubectl)Jenkins 创建Pipeline编写Pipeline Script前言参考此教程时,需要提前准备好或了解以下内容:准备好k8s集群,...
·
文章目录
前言
参考此教程时,需要提前准备好或了解以下内容:
- 准备好k8s集群,请参考《使用kubeadm搭建(kubernetes)k8s集群》
- 准备好harbor仓库,请参考《Harbor 安装》
- 了解maven构建相关知识,请参考《Maven新手入门》
- 了解docker镜像构建相关知识,请参考《Dockerfile构建springboot Image》
- 了解k8s如何部署应用,请参考《在k8s上部署Springboot》
- 了解jenkins pipeline的基本语法《Jenkins高级篇之Pipeline代码自动化》
环境配置
在Jenkins的服务器上安装好jdk
在Jenkins的服务器上安装好maven
在Jenkins的服务器上安装好docker
Global Tool Configuration(全局工具配置)
- 找到并打开Jenkins/Global Tool Configuration
- 找到jdk和maven,点击Add JDK 和点击 Add Maven,并根据实际安装情况填入信息
配置pod template(kubectl)
如果还没有集成kubernetes的,请参考《Jenkins 集成k8s 运行Jenkins slave(Jenkins 弹性slave)》里面的在Jenkins上集成Kubernetes
- 找到Jenkins/configuration,再找到里面的Cloud,点击Add Pod Template并填入以下信息:
注意:点击 Advance(高级),配置Service Account,根据自己实际配置的Service Account填入,最后Save
Jenkins 创建Pipeline
在Jenkins里面创建Pipeline job
- 点击New Item
- 输入item name: ,并点击Pipeline,最后点OK
- 在Pipeline里面选择Pipeline script,并在Script里面写入pipeline script,script参考下面的编写Pipeline Script部分,点击保存
编写Pipeline Script
以下是我准备好的声明式Pipeline,可以直接复制到上面的Pipeline job,点击保存
pipeline {
agent any
tools {
maven 'maven'
jdk 'jdk'
}
stages {
stage('Init') {
steps{
script{
deleteDir()
println "welcome to Nick learn"
sh 'mkdir workplace'
}
}
}
stage('git source') {
steps{
script{
dir("${env.WORKSPACE}/workplace"){
git branch: 'env/dev', credentialsId: '11111',url: 'git@192.168.108.132:root/jenkins_slave_k8s.git'
}
}
}
}
stage('maven package') {
steps{
script{
dir("${env.WORKSPACE}/workplace"){
sh 'mvn clean package'
}
}
}
}
stage('docker build') {
steps{
script{
dir("${env.WORKSPACE}/workplace"){
sh 'docker login -u ' + HARBOR_CREDS_USR + ' -p ' + HARBOR_CREDS_PSW + ' ' + HARBOR_HOST
sh "docker build -t " + HARBOR_HOST + "/" + DOCKER_IMAGE + ":" + GIT_TAG + " --no-cache ."
sh "docker push " + HARBOR_HOST + "/" + DOCKER_IMAGE + ":" + GIT_TAG
sh "docker rmi " + HARBOR_HOST + "/" + DOCKER_IMAGE + ":" + GIT_TAG
stash(includes: '*.tpl', name: 'workplace')
}
}
}
}
stage('K8s Deploy') {
agent {
node {
label 'k8s_deploy'
}
}
steps {
script {
container('kubectl') {
unstash 'workplace'
sh 'ls'
sh "sed -e 's#{IMAGE_URL}#" + HARBOR_HOST + "/" + DOCKER_IMAGE + "#g;s#{IMAGE_TAG}#" + GIT_TAG + "#g;s#{APP_NAME}#" + APP_NAME + "#g;s#{SPRING_PROFILE}#k8s-test#g' k8s-deployment.tpl > k8s-deployment.yml"
sh "sed -e 's#{APP_NAME}#" + APP_NAME + "#g;s#{NODE_PORT}#" + NODE_PORT + "#g' k8s-deployment-svc.tpl > k8s-deployment-svc.yml"
try{
sh "kubectl delete rc " + APP_NAME + " --namespace=" + K8S_NAMESPACE
}catch (Exception e) {
println e
}
try{
sh "kubectl delete svc " + APP_NAME + " --namespace=" + K8S_NAMESPACE
}catch (Exception e) {
println e
}
sh "kubectl apply -f k8s-deployment.yml --namespace=" + K8S_NAMESPACE
sh "kubectl apply -f k8s-deployment-svc.yml --namespace=" + K8S_NAMESPACE
}
}
}
}
}
environment {
HARBOR_CREDS = credentials('jenkins-harbor-creds')
GIT_TAG = sh(returnStdout: true,script: 'echo ' + BUILD_NUMBER ).trim()
}
parameters {
string(name: 'HARBOR_HOST', defaultValue: '192.168.108.131', description: 'harbor仓库地址')
string(name: 'DOCKER_IMAGE', defaultValue: 'practice/jenkins_demo_practice', description: 'docker镜像名')
string(name: 'APP_NAME', defaultValue: 'jenkinsDemoPractice', description: 'k8s中标签名')
string(name: 'NODE_PORT', defaultValue: '30000', description: 'Service端口')
string(name: 'K8S_NAMESPACE', defaultValue: 'devops', description: 'k8s的namespace名称')
}
}
更多推荐
已为社区贡献5条内容
所有评论(0)