Jenkins file one line of code to deploy. Net program to k8s

chester·chen 2021-11-25 17:26:30
jenkins file line code deploy.

What is? Jenkins Shared library

With the increase of micro Services , Every project needs pipline file , In this case Pipeline High code redundancy , also pipeline It's getting more and more complex .

jenkins have access to Shared Lib, Put some public pipeline Abstract into module code , In various projects pipeline Reuse between , To reduce redundancy .

Shared library directory structure

 Shared library root directory
|-- vars
|-- test1.groovy
|-- src
|-- test2.groovy
|-- resources

vars: Depend on Jenkins Of the operating environment Groovy Script . Among them Groovy The script is called Global variables .
src: The standard Java Source directory structure , Among them Groovy Scripts are called class libraries (Library class).
resources: Directories allow you to use... From external libraries libraryResource Step to load the relevant non Groovy file .

How to reference a shared library

#!/usr/bin/env groovy
// Reference the shared library configured by default 
@Library('demo-shared-library') _
// Reference the specified branch 、tag Shared library code 
@Library('[email protected]') _
// References more than one specified branch tag The shared library of 
@Library('[email protected]$Branch/Tag','[email protected]$Branch/Tag') _
@Library('utils') import org.foo.Utilities
@Library('utils') import static org.foo.Utilities.*

vars Global variables under

/vars The global variable under must be in All lowercase or hump (camelCased)
/vars/*.groovy If it comes true call() Method , When referencing directly, the method is executed by default

Implement one line of code deployment .NET Program to K8S

install Jenkins Master

# master
docker run --name jenkins-blueocean -u root --privileged -d -p 8080:8080 -p 50000:50000 -v D:/architecture/jenkins/data:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock jenkinsci/blueocean
# visit :http://ip:8080/
# jenkins password , View container log : 7285ced325a24483bfdaab227415fdac
# Install recommended plug-ins 

install Jenkins Agent

 
Manage Jenkins -> Manage Nodes and Clouds -> New Node
  • Name: agent2/agent3
  • Labels: agentnode
  • Launch method: Launch agent by connecting it to the master

Build Agent Docker Image

# slave
# Dockerfile
FROM jenkins/inbound-agent:latest
USER root
RUN apt-get update
RUN apt-get -y install ca-certificates curl gnupg lsb-release
RUN curl -fsSL https://get.docker.com -o get-docker.sh
RUN sh get-docker.sh
RUN curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
RUN install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
RUN kubectl version --client
ENV JMETER_VERSION=5.4.1
ENV JMETER_HOME=/jmeter/apache-jmeter-${JMETER_VERSION}
ENV JMETER_PATH=${JMETER_HOME}/bin:${PATH}
ENV PATH=${JMETER_HOME}/bin:${PATH}
RUN mkdir /jmeter
COPY apache-jmeter-${JMETER_VERSION}.tgz /jmeter
RUN cd /jmeter && tar xvf apache-jmeter-${JMETER_VERSION}.tgz # among tgz Please go to the official website to download
RUN sed -i 's/#jmeter.save.saveservice.output_format=csv/jmeter.save.saveservice.output_format=xml/g' /jmeter/apache-jmeter-5.4.1/bin/jmeter.properties

 

docker build -t chesterjenkinsagent .
docker tag chesterjenkinsagent:latest 192.168.43.95/jenkins/chesterjenkinsagent:v1
docker login --username=admin --password=123456 192.168.43.95
docker push 192.168.43.95/jenkins/chesterjenkinsagent:v1

function Agent, The key passes through jenkins Of agent information acquisition

# agent4
docker run -v /var/run/docker.sock:/var/run/docker.sock --name agent4 -d --init 192.168.43.95/jenkins/chesterjenkinsagent:v1 -url http://192.168.43.94:8080 1e84c896dbffc0c325587eedb6301ab0ae66d3f4b49c4628dbb05714e382d7a2 agent4

 

newly added K8S The credentials

  • take k8s Export the configuration file of the cluster ~/.kube/config
  •  Mange Jenkins -> Manage Credentials -> Add Credentials -> Secret File
  • Select the exported kubeconfig, Set up id by kubeconfig

newly added Harbor The credentials

  • Mange Jenkins -> Manage Credentials -> Add Credentials -> Username with password
  • Input Harbor The username and password of

newly added Gitee The credentials

  • Mange Jenkins -> Manage Credentials -> Add Credentials -> Username with password
  • Input Gitee The username and password of

Installing a plug-in

Manage Jenkins -> Manage Plugins -> Search "Performance" -> install

Manage shared libraries

Manage Jenkins -> Configure System -> Global Pipeline Libraries, Among them git The address is the address of the shared library

Add the following code to the shared library

vars/run.groovy

#!/usr/bin/env groovy
def call(String nameSpaceName, String serviceName, String servicePath, String servicePort, String nodePort, Map envInfo) {
def devBranch = envInfo['dev']
def prodBranch = envInfo['prod']
pipeline {
agent {
label 'agentnode'
}
environment {
DEV_MY_KUBECONFIG = credentials('kubeconfig')
PROD_MY_KUBECONFIG = credentials('kubeconfig')
HARBOR = credentials('harbor')
}
stages {
stage('Dev - GitPull') {
steps {
deleteDir()
gitCheckOut devBranch, env.GIT_URL
}
post {
success {
script {
echo 'pull done'
}
}
}
}
stage('Dev - DockerBuild') {
steps {
dockerImageBuild serviceName, "${servicePath}Dockerfile"
}
}
stage('Dev - DockerTag') {
steps {
dockerTag serviceName, 'dev'
}
}
stage('Dev - DockerLogin') {
steps {
dockerLogin 'dev'
}
}
stage('Dev - DockerPush') {
steps {
dockerPush serviceName, 'dev'
}
}
stage('Dev - GenerateHarborSecretYAML') {
steps {
harborSecret nameSpaceName, serviceName, 'dev'
}
}
stage('Dev - GenerateK8SYAML') {
steps {
k8sGenerateYaml nameSpaceName, serviceName, servicePath, 'dev', servicePort, nodePort
}
}
stage('Dev - DeployToK8S') {
steps {
k8sDeploy servicePath, 'dev'
}
}
stage('Dev - CheckDeployStatus') {
steps {
k8sCheckDeployStatus nameSpaceName, serviceName, 'dev'
}
}
stage('Dev - Jmeter Test') {
steps {
jmeterTest servicePath
}
}
stage('DeployToProd?') {
steps {
input ' Deploy production ?'
}
}
stage('Prod - GitPull') {
steps {
gitCheckOut prodBranch, env.GIT_URL
}
}
stage('Prod - DockerBuild') {
steps {
dockerImageBuild serviceName, "${servicePath}Dockerfile"
}
}
stage('Prod - DockerTag') {
steps {
dockerTag serviceName, 'prod'
}
}
stage('Prod - DockerLogin') {
steps {
dockerLogin 'prod'
}
}
stage('Prod - DockerPush') {
steps {
dockerPush serviceName, 'prod'
}
}
stage('Prod - GenerateHarborSecretYAML') {
steps {
harborSecret nameSpaceName, serviceName, 'prod'
}
}
stage('Prod - GenerateK8SYAML') {
steps {
k8sGenerateYaml nameSpaceName, serviceName, servicePath, 'prod', servicePort, nodePort
}
}
stage('Prod - DeployToK8S') {
steps {
k8sDeploy servicePath, 'prod'
}
}
stage('Prod - CheckDeployStatus') {
steps {
k8sCheckDeployStatus nameSpaceName, serviceName, 'prod'
}
}
}
}
}

 

vars/dockerImageBuild.groovy

#!/usr/bin/env groovy
def call(String serviceName, String dockerfilePath) {
echo "serviceName:${serviceName} dockerfilePath:${dockerfilePath}"
sh "docker build -t ${serviceName} -f ${dockerfilePath} ."
}

 

vars/dockerLogin.groovy

#!/usr/bin/env groovy
def call(String envName) {
sh 'docker login --username=$HARBOR_USR --password=$HARBOR_PSW 192.168.43.95'
}

 

vars/dockerPush.groovy

#!/usr/bin/env groovy
def call(String serviceName,String envName) {
sh "docker push 192.168.43.95/dapr/${serviceName}:${envName}-${BUILD_NUMBER}"
}

 

vars/dockerTag.groovy

#!/usr/bin/env groovy
def call(String serviceName, String envName) {
sh "docker tag ${serviceName}:latest 192.168.43.95/dapr/${serviceName}:${envName}-${BUILD_NUMBER}"
}

 

vars/gitCheckOut.groovy

#!/usr/bin/env groovy
def call(String branchName, String gitUrl) {
echo "branchName:${branchName} gitUrl:${gitUrl}"
checkout([$class: 'GitSCM', branches: [[name: branchName]], extensions: [], userRemoteConfigs: [[credentialsId: 'gitee', url: gitUrl]]])
}

 

vars/harborSecret.groovy

def call(String namespaceName, String serviceName, String envName) {
dir('harborsecret') {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'gitee', url: 'https://gitee.com/chesterdotchen/jenkins-demo-secrets.git']]])
sh """sed -i 's/{{ServiceName}}/${serviceName}/g' secrets.yaml"""
sh """sed -i 's/{{NameSpaceName}}/${namespaceName}/g' secrets.yaml"""
if (envName == 'dev') {
sh("kubectl --kubeconfig ${DEV_MY_KUBECONFIG} apply -f secrets.yaml")
}
if (envName == 'prod') {
sh("kubectl --kubeconfig ${PROD_MY_KUBECONFIG} apply -f secrets.yaml")
}
}
}

 

vars/jmeterTest.groovy

#!/usr/bin/env groovy
def call(String servicePath) {
sh "jmeter -j jmeter.save.saveservice.output_format=xml -n -t ${servicePath}jmeter.jmx -l ${servicePath}jmeter.report.jtl"
sh "cp ${servicePath}jmeter.report.jtl ${servicePath}jmeter.report.${BUILD_NUMBER}.jtl"
perfReport errorFailedThreshold:5, sourceDataFiles:"${servicePath}jmeter.report.jtl"
sh "cat ${servicePath}jmeter.report.${BUILD_NUMBER}.jtl"
sh """#!/bin/sh
grep '<failure>true</failure>' ${servicePath}jmeter.report.${BUILD_NUMBER}.jtl
if [ \$? = 0 ]
then
exit 1
else
exit 0
fi
"""
}

 

vars/k8sCheckDeployStatus.groovy

#!/usr/bin/env groovy
def call(String nameSpaceName, String serviceName, String envName) {
if (envName == 'dev') {
sh("""
 ATTEMPTS=0
ROLLOUT_STATUS_CMD='kubectl --kubeconfig ${DEV_MY_KUBECONFIG} rollout status deployment/${serviceName} -n ${nameSpaceName}-ns'
until \$ROLLOUT_STATUS_CMD || [ \$ATTEMPTS -eq 60 ]; do
\$ROLLOUT_STATUS_CMD
ATTEMPTS=\$((attempts + 1))
sleep 10
done
""")
 }
if (envName == 'prod') {
sh("""
 ATTEMPTS=0
ROLLOUT_STATUS_CMD='kubectl --kubeconfig ${PROD_MY_KUBECONFIG} rollout status deployment/${serviceName} -n ${nameSpaceName}-ns'
until \$ROLLOUT_STATUS_CMD || [ \$ATTEMPTS -eq 60 ]; do
\$ROLLOUT_STATUS_CMD
ATTEMPTS=\$((attempts + 1))
sleep 10
done
""")
 }
}

 

vars/k8sDeploy.groovy

#!/usr/bin/env groovy
def call(String servicePath, String envName) {
if (envName == 'dev') {
sh("kubectl --kubeconfig ${DEV_MY_KUBECONFIG} apply -f ${servicePath}deployment.yaml")
}
if (envName == 'prod') {
sh("kubectl --kubeconfig ${PROD_MY_KUBECONFIG} apply -f ${servicePath}deployment.yaml")
}
}

 

vars/k8sGenerateYaml.groovy

#!/usr/bin/env groovy
def call(String namespaceName, String serviceName, String servicePath, String envName, String servicePort, String nodePort) {
sh """sed "s/{{tagversion}}/${envName}-${BUILD_NUMBER}/g" ${servicePath}deployment.yaml.tpl > ${servicePath}deployment.yaml """
sh """sed -i 's/{{ServiceName}}/${serviceName}/g' ${servicePath}deployment.yaml"""
sh """sed -i 's/{{ServicePort}}/${servicePort}/g' ${servicePath}deployment.yaml"""
sh """sed -i 's/{{NodePort}}/${nodePort}/g' ${servicePath}deployment.yaml"""
sh """sed -i 's/{{NameSpaceName}}/${namespaceName}/g' ${servicePath}deployment.yaml"""
}

 

jenkins-demo-secrets The code in is as follows

apiVersion: v1
kind: Namespace
metadata:
name: {{NameSpaceName}}-ns
---
apiVersion: v1
kind: Secret
metadata:
name: harbor-key
namespace: {{NameSpaceName}}-ns
type: kubernetes.io/dockerconfigjson
data:
.dockerconfigjson: ewoJImF1dGhzIjogewoJCSIxOTIuMTY4LjQzLjk1IjogewoJCQkiYXV0aCI6ICJZV1J0YVc0Nk1USXpORFUyIgoJCX0sCgkJInJlZ2lzdHJ5LmNuLWJlaWppbmcuYWxpeXVuY3MuY29tIjogewoJCQkiYXV0aCI6ICI2Wm1JNUxpQTU0dXVPbU5vWlc1NWFYTm9hVEV5TXc9PSIKCQl9Cgl9Cn0=

 dockerconfigjson Can be obtained by

docker login --username=admin --password=123456 192.168.43.95
cat ~/.docker/config.json | base64

Jenkinsfile Reference shared libraries in

The project library needs to be written in advance Dockerfile,Jenkinsfile,deployment.yaml.tpl,jmeter.jmx

 

Dockerfile

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 5001
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["FrontEnd/FrontEnd.csproj", "FrontEnd/"]
COPY ["Common/Common.csproj", "Common/"]
RUN dotnet restore "FrontEnd/FrontEnd.csproj"
COPY . .
WORKDIR "/src/FrontEnd"
RUN dotnet build "FrontEnd.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "FrontEnd.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "FrontEnd.dll"]

Jenkinsfile

#!/usr/bin/env groovy
@Library('[email protected]') _
run 'daprtest', 'frontend', './FrontEnd/', '5001', '31111', ['dev':'*/master', 'prod':'*/master']

deployment.yaml.tpl

apiVersion: v1
kind: Namespace
metadata:
name: {{NameSpaceName}}-ns
---
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: statestore
namespace: {{NameSpaceName}}-ns
spec:
type: state.redis
version: v1
metadata:
- name: redisHost
value: 192.168.43.102:6379
- name: redisPassword
value: "123456"
- name: actorStateStore
value: "true"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ServiceName}}
namespace: {{NameSpaceName}}-ns
labels:
app: {{ServiceName}}
spec:
replicas: 1
selector:
matchLabels:
app: {{ServiceName}}
template:
metadata:
namespace: {{NameSpaceName}}-ns
labels:
app: {{ServiceName}}
annotations:
dapr.io/enabled: "true"
dapr.io/app-id: "{{ServiceName}}"
dapr.io/app-port: "{{ServicePort}}"
spec:
imagePullSecrets:
- name: harbor-key
containers:
- name: {{ServiceName}}
image: 192.168.43.95/dapr/{{ServiceName}}:{{tagversion}}
ports:
- containerPort: {{ServicePort}}
imagePullPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
namespace: {{NameSpaceName}}-ns
name: {{ServiceName}}-svc
spec:
type: NodePort
selector:
app: {{ServiceName}}
ports:
- port: {{ServicePort}}
targetPort: {{ServicePort}}
nodePort: {{NodePort}}

jmter.jmx Write your own

newly build pipline

Among them URL Point to your project git Address

structure pipline

thus , We're done Jenkinsfile Deploy our project in one line of code , If there's a new one service, In the process of writing Dockerfile,deployment.yaml.tpl( bring into correspondence with ),jmeter.jmx Under the premise of , We just need Jenkinsfile Through a shared library run The method can

版权声明
本文为[chester·chen]所创,转载请带上原文链接,感谢
https://javamana.com/2021/11/20211109084136000e.html

  1. JavaScript高级程序设计读后感(一)之零碎知识点查漏补缺
  2. 先到先学!Alibaba甩出第四次更新的JDK源码高级笔记(终极版)
  3. Java File类
  4. How To Install MariaDB on linux
  5. #yyds干货盘点# Mybatis 的 XML 配置
  6. Spring认证中国教育管理中心-Spring Data MongoDB教程七
  7. Linux进程和任务管理
  8. Linux文件系统日志分析
  9. Redis-客户端-重点知识
  10. Redis-事件-重点知识
  11. Redis-AOF持久化-重点知识
  12. Redis-RDB持久化-重点知识
  13. http://lx.gongxuanwang.com/sszt/32.htm
  14. 回顾我两个月面试阿里,携程,小红书,美团,网易等等(Java岗)
  15. JavaScript高级程序设计读后感(一)之零碎知识点查漏补缺
  16. Rocketmq source code analysis: message sending process
  17. Rocketmq source code analysis: how does rocketmq store messages?
  18. RocketMQ source analysis: how to debug the RocketMQ source in IDEA
  19. How To Install MariaDB on linux
  20. Comment installer mariadb sur Linux
  21. http://lx.gongxuanwang.com/sszt/7.htm
  22. Classe de fichiers Java
  23. Premier arrivé, premier servi! Alibaba lance la quatrième mise à jour de JDK source Advanced notes (Ultimate)
  24. #yyds干货盘点#设计模式之【工厂模式】
  25. Java * SpringBoot实现万能文件在线预览,已开源,真香
  26. Redis | 第4章 Redis中的数据库《Redis设计与实现》
  27. Liang Tingwei's first variety show of "director, please give advice" reshapes the classic work "spring of a new town"
  28. Redis | 第4章 Redis中的数据库《Redis设计与实现》
  29. 关于centos docker版本过低导致 is not a valid repository/tag: invalid reference format
  30. Redis 源码简洁剖析 02 - SDS 字符串
  31. 回顧我兩個月面試阿裏,攜程,小紅書,美團,網易等等(Java崗)
  32. Rétrospectivement, j'ai passé deux mois à interviewer Ali, ctrip, Little Red Book, meituan, NetEase, etc. (Java post)
  33. Docker + webhook Automation Deployment Front End Project
  34. Java技术之Spring、Hibernate框架整合方法
  35. http://lx.gongxuanwang.com/sszt/32.htm
  36. 亚马逊自己的 Linux 发行版现在完全基于 Fedora 了
  37. Redis 源码简洁剖析 02 - SDS 字符串
  38. Java技術之Spring、Hibernate框架整合方法
  39. Méthode d'intégration des cadres de printemps et d'hibernation de la technologie Java
  40. Redis source Concise Analysis 02 - SDS String
  41. La distribution Linux d'Amazon est maintenant entièrement basée sur Fedora
  42. org.springframework.web.bind.MissingServletRequestParameterException
  43. Built in constraints and functions of MySQL Foundation (2)
  44. Basic operation of MySQL Foundation (I)
  45. Introduction to Java zero foundation 3: Java data types
  46. 从零开始搭建EasyDarwin环境——Linux系统开发环境搭建Golang
  47. Redis source Concise Analysis 02 - SDS String
  48. Construire l'environnement easydarwin à partir de zéro - - construire l'environnement de développement du système Linux golang
  49. javaweb代码是正确的,但是第一行代码就报错了
  50. **** | Java | 后端开挂:3行代码写出8个接口
  51. Java || 看了大二学长写的代码,我竟开始默默的模仿了。。。
  52. Java | 手把手教你实现一个抽奖系统(Java版)
  53. Java | Manuel pour vous apprendre à mettre en œuvre un système de loterie (version Java)
  54. Java | | après avoir lu le Code que j'ai écrit en deuxième année, j'ai commencé à imiter silencieusement...
  55. Java | back - end Pending: 3 - line Code write 8 Interfaces
  56. Le Code Web Java est correct, mais la première ligne de code est incorrecte
  57. Android网络编程之Http通信
  58. Android網絡編程之Http通信
  59. Http communication for Android Network Programming
  60. The story of spring