#### 准备工作
```sh
jenkins 安装pipeline相关插件
```
#### 工作流程
##### springboot微服务打包工作流程
```sh
1.jenkins拉取gitlab代码
2.maven编译微服务打包生成jar包
3.docker build通过DockerFile文件把jar包打成镜像
4.docker push推送镜像到harbor仓库
5.helm部署会拉取harbor的服务镜像以及chart部署K8S集群
6.配置ingress暴露前端服务和API接口服务、网关服务
```
pipeline配置
```Groovy
pipeline {
agent any
environment {
REGISTRY_HOST = 'harbor.example.com' #私有仓库的域名
REGISTRY_USERNAME = 'shudoon'
REGISTRY_PASSWORD = 'xxxxxxx'
REGISTRY_NAMESPACE = 'shudoon' #私有仓库的命名空间
REGISTRY_SECRET = 'shudoon-harbor-secret' #k8s存储私有仓库的secret
FILEBEAT_IMAGE="filebeat" #指定filebeat镜像名
SKYWALKING_IMAGE="skywalking-agent-sidecar" #指定skywalking镜像名
HELM_LOCAL_REPO = 'shudoon-local' #helm本地仓库名
HELM_TEMPLATE_NAME = 'springboot-demo' #helm的模板名
HELM_TEMPLATE_VERSION = '0.2.24-filebeat-skywalking-javaheap' #helm模板的版本
}
parameters {
string defaultValue: 'shudoon-data-service', description: '服务名', name: 'SERVICE_NAME', trim: false
gitParameter branchFilter: '.*', defaultValue: 'master', name: 'branch_or_tag', type: 'PT_BRANCH_TAG'
string defaultValue: '1', description: '实例数', name: 'REPLICAS', trim: false
string defaultValue: '18004', description: '服务端口', name: 'SERVER_PORT', trim: false
string defaultValue: 'test2', description: '环境', name: 'BUILD_ENV', trim: false
string defaultValue: 'http://git.example.com/shudoon-cloud-new/shudoon-cloud-service/shudoon-data.git', description: 'git地址', name: 'GIT_URL', trim: false
}
stages {
stage('拉取代码') {
steps {
sh 'pwd'
checkout([$class: 'GitSCM', branches: [[name: '${branch_or_tag}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '7ace3f3e-4ef7-4005-aa6c-ae5aef462fac', url: '${GIT_URL}']]]) //jenkins配置的用户账号密码的对应的credentialsId
}
}
stage('获取版本'){
steps{
script{
TAG_BRANCH = sh(returnStdout: true, script: 'echo $branch_or_tag|awk -F/ \'{print $NF}\'').trim()
GIT_COMMITID = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
BUILD_IMAGE_TAG = "${TAG_BRANCH}-${GIT_COMMITID}-${BUILD_ID}"
}
}
}
stage('构建MAVEN项目') {
steps {
sh 'pwd'
sh "echo ${TAG_BRANCH}-${GIT_COMMITID}"
sh "echo $BUILD_IMAGE_TAG"
sh '/usr/local/maven3.6.2/bin/mvn -f src/pom.xml clean install -Dmaven.test.skip=true -P${BUILD_ENV}'
}
}
stage('docker构建镜像') {
steps {
sh "cd src/${SERVICE_NAME};pwd;docker build --build-arg server_port=${SERVER_PORT} -t ${REGISTRY_HOST}/${REGISTRY_NAMESPACE}/${SERVICE_NAME}:${BUILD_IMAGE_TAG} ."
}
}
stage('推送构建的镜像') {
steps {
sh "docker login -u ${REGISTRY_USERNAME} -p ${REGISTRY_PASSWORD} ${REGISTRY_HOST}"
sh "docker push ${REGISTRY_HOST}/${REGISTRY_NAMESPACE}/${SERVICE_NAME}:${BUILD_IMAGE_TAG}"
sh "docker image rm ${REGISTRY_HOST}/${REGISTRY_NAMESPACE}/${SERVICE_NAME}:${BUILD_IMAGE_TAG}"
}
}
stage('部署镜像到k8s') {
steps {
sh "/usr/local/bin/helm upgrade -i --kubeconfig /root/.kube/config --set image.repository=${REGISTRY_HOST}/${REGISTRY_NAMESPACE}/${SERVICE_NAME},image.tag=${BUILD_IMAGE_TAG},image2.repository=${REGISTRY_HOST}/${REGISTRY_NAMESPACE}/${FILEBEAT_IMAGE},image3.repository=${REGISTRY_HOST}/${REGISTRY_NAMESPACE}/${SKYWALKING_IMAGE},imagePullSecrets[0].name=${REGISTRY_SECRET},replicaCount=${REPLICAS},service.targetPort=${SERVER_PORT} --version ${HELM_TEMPLATE_VERSION} ${SERVICE_NAME} ${HELM_LOCAL_REPO}/${HELM_TEMPLATE_NAME}"
}
}
}
}
```
##### 前端服务打包工作流程
```sh
1.jenkins拉取gitlab代码
2.npm build构建前端代码生成dist静态文件
2.docker build通过DockerFile文件把dist静态文件包打成镜像
3.docker push推送镜像到harbor仓库
4.helm部署会拉取harbor的服务镜像以及chart部署K8S集群
5.配置ingress暴露前端服务和API接口服务、网关服务
```
pipeline配置
```Groovy
pipeline {
agent any
environment {
VUE_APP_BASE_API = 'https://tgw2.example.com/' #后台的API地址
REGISTRY_HOST = 'harbor.example.com'
REGISTRY_USERNAME = 'shudoon'
REGISTRY_PASSWORD = 'xxxxx'
REGISTRY_NAMESPACE = 'shudoon'
REGISTRY_SECRET = 'shudoon-harbor-secret'
FILEBEAT_IMAGE="filebeat"
SKYWALKING_IMAGE="skywalking-agent-sidecar"
HELM_LOCAL_REPO = 'shudoon-local'
HELM_TEMPLATE_NAME = 'web-demo'
HELM_TEMPLATE_VERSION = '0.2.1-filebeat'
}
parameters {
string defaultValue: 'dc-web', description: '服务名', name: 'SERVICE_NAME', trim: false
gitParameter branchFilter: '.*', defaultValue: 'master', name: 'tag_branch', type: 'PT_BRANCH_TAG'
string defaultValue: '1', description: '实例数', name: 'REPLICAS', trim: false
string defaultValue: '80', description: '服务端口', name: 'SERVER_PORT', trim: false
string defaultValue: 'http://git.example.com/webapp/report-pc.git', description: 'git地址', name: 'GIT_URL', trim: false
}
stages {
stage('拉取代码') {
steps {
sh 'pwd'
sh "echo $VUE_APP_BASE_API"
checkout([$class: 'GitSCM', branches: [[name: '${tag_branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '7ace3f3e-4ef7-4005-aa6c-ae5aef462fac', url: '${GIT_URL}']]])
}
}
stage('获取版本'){
steps{
script{
TAG_BRANCH = sh(returnStdout: true, script: 'echo $tag_branch|awk -F/ \'{print $NF}\'').trim()
GIT_COMMITID = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
BUILD_IMAGE_TAG = "${TAG_BRANCH}-${GIT_COMMITID}-${BUILD_ID}"
}
}
}
stage('构建前端node项目') {
tools {
nodejs 'nodejs_12.18.1'
}
steps {
sh 'node --version'
sh 'npm --version'
sh 'npm config set registry https://registry.npm.taobao.org'
sh 'npm config get registry'
sh 'npm install --unsafe-perm'
sh 'npm run build:stage'
}
}
stage('构建镜像') {
steps {
script {
def image = docker.build("${REGISTRY_HOST}/${REGISTRY_NAMESPACE}/${SERVICE_NAME}:${BUILD_IMAGE_TAG}")
}
}
}
stage('推送构建的镜像') {
steps {
sh "docker login -u ${REGISTRY_USERNAME} -p ${REGISTRY_PASSWORD} ${REGISTRY_HOST}"
sh "docker push ${REGISTRY_HOST}/${REGISTRY_NAMESPACE}/${SERVICE_NAME}:${BUILD_IMAGE_TAG}"
sh "docker image rm ${REGISTRY_HOST}/${REGISTRY_NAMESPACE}/${SERVICE_NAME}:${BUILD_IMAGE_TAG}"
}
}
stage('部署镜像到k8s') {
steps {
sh "/usr/local/bin/helm upgrade -i --set image.repository=${REGISTRY_HOST}/${REGISTRY_NAMESPACE}/${SERVICE_NAME},image.tag=${BUILD_IMAGE_TAG},image2.repository=${REGISTRY_HOST}/${REGISTRY_NAMESPACE}/${FILEBEAT_IMAGE},image3.repository=${REGISTRY_HOST}/${REGISTRY_NAMESPACE}/${SKYWALKING_IMAGE},imagePullSecrets[0].name=${REGISTRY_SECRET},replicaCount=${REPLICAS},service.targetPort=${SERVER_PORT} --version ${HELM_TEMPLATE_VERSION} ${SERVICE_NAME} ${HELM_LOCAL_REPO}/${HELM_TEMPLATE_NAME}"
}
}
}
}
```