什么是Jenkins共享库

随着微服务的增多,每个项目的都需要pipline文件,这样的话Pipeline代码冗余度高,并且pipeline的功能越来越复杂。

jenkins可以使用Shared Lib,将一些公共的pipeline抽象做成模块代码,在各种项目pipeline之间复用,以减少冗余。

共享库目录结构

共享库根目录
|-- vars
|-- test1.groovy
|-- src
|-- test2.groovy
|-- resources

vars: 依赖于Jenkins运行环境的Groovy脚本。其中的Groovy脚本被称之为全局变量
src: 标准的Java源码目录结构,其中的Groovy脚本被称为类库(Library class)。
resources: 目录允许从外部库中使用 libraryResource 步骤来加载有关的非 Groovy 文件。

引用共享库的方式

#!/usr/bin/env groovy

// 引用默认配置的共享库
@Library('demo-shared-library') _ // 引用指定分支、tag的共享库代码
@Library('demo-shared-library@1.0') _ // 引用多个指定分支tag的共享库
@Library('demo-shared-library@$Branch/Tag','demo-shared-library-test@$Branch/Tag') _ @Library('utils') import org.foo.Utilities @Library('utils') import static org.foo.Utilities.*

vars下的全局变量

/vars下的全局变量必须以全小写或驼峰(camelCased)
/vars/*.groovy若实现call()方法,直接引用时默认执行其中的方法

实现一行代码部署.NET程序到K8S

安装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 # 访问:http://ip:8080/
# jenkins密码,查看容器日志: 7285ced325a24483bfdaab227415fdac
# 安装推荐插件

安装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 #其中tgz文件请去官网下载
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

运行Agent,其中的密钥通过jenkins的agent信息获取

# 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

新增K8S凭据

  • 将k8s集群的配置文件导出 ~/.kube/config
  • Mange Jenkins -> Manage Credentials -> Add Credentials -> Secret File
  • 选择导出的kubeconfig,设置id为kubeconfig

新增Harbor凭据

  • Mange Jenkins -> Manage Credentials -> Add Credentials -> Username with password
  • 输入Harbor的用户名密码

新增Gitee凭据

  • Mange Jenkins -> Manage Credentials -> Add Credentials -> Username with password
  • 输入Gitee的用户名密码

安装插件

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

管理共享库

Manage Jenkins -> Configure System -> Global Pipeline Libraries,其中的git地址是共享库的地址

共享库中新增以下代码

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 '部署生产?'
}
} 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中代码如下

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可通过以下方式获取

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

Jenkinsfile中引用共享库

项目库中需要提前编写好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('share@master') _

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则根据需要自行编写

新建pipline

其中的URL指向自己项目的git地址

构建pipline

至此,我们已经完成了Jenkinsfile中一行代码部署我们的项目,如果有新的service,在编写好Dockerfile,deployment.yaml.tpl(保持一致),jmeter.jmx的前提下,我们只需要Jenkinsfile中通过共享库引用run方法即可

Jenkins file一行代码部署.NET程序到K8S的更多相关文章

  1. 一行代码让微信小程序支持 cookie

    weapp-cookie 一行代码让微信小程序支持 cookie,传送门:github Intro 微信原生的 wx.request 网络请求接口并不支持传统的 Cookie,但有时候我们现有的后端接 ...

  2. 写在最前面 - 《看懂每一行代码 - kubernetes》

    我要写什么 <看懂每一行代码 - kubernetes>会包含k8s整个项目的源码解析,考虑到门槛问题,在开始分析k8s之前我会通过一些更低难度的golang开源项目讲解来帮助大家提升go ...

  3. 【转】jenkins自动化部署项目7 -- 新建job(将服务代码部署在windows上)

    关于构建结束后jenkins会kill所有衍生子进程的官方解决方案:https://wiki.jenkins.io/display/JENKINS/Spawning+processes+from+bu ...

  4. jenkins自动化部署项目7 -- 新建job(将服务代码部署在windows上)

    关于构建结束后jenkins会kill所有衍生子进程的官方解决方案:https://wiki.jenkins.io/display/JENKINS/Spawning+processes+from+bu ...

  5. Centos7.4简单安装使用gitlab+maven+jenkins实现java代码的持续集成部署

    1.工具的简单介绍 gitlab--源代码版本管理控制工具 maven--java代码编译构建工具 jenkins--基于java开发的自动化持续集成部署工具 sonar--代码质量管理工具 2.gi ...

  6. Docker搭建Jenkins+Maven/Gradle——代码自动化运维部署平台(二)

    一.简介 1.Jenkins 概述: Jenkins是一个功能强大的应用程序,允许持续集成和持续交付项目,无论用的是什么平台.这是一个免费的源代码,可以处理任何类型的构建或持续集成.集成Jenkins ...

  7. Jenkins与网站代码上线解决方案

    1.1 前言 Jenkins是一个用Java编写的开源的持续集成工具.在与Oracle发生争执后,项目从Hudson项目独立. Jenkins提供了软件开发的持续集成服务.它运行在Servlet容器中 ...

  8. Java核心技术卷一基础知识-第10章-部署应用程序和applet-读书笔记

    第10章 部署应用程序和applet 本章内容: * JAR文件 * Java Web Start * applet * 应用程序首选项存储 10.1 JAR文件 一个JAR文件既可以包含类文件,也可 ...

  9. Jenkins与网站代码上线解决方案【转】

    转自 Jenkins与网站代码上线解决方案 - 惨绿少年 https://www.nmtui.com/clsn/lx524.html 1.1 前言 Jenkins是一个用Java编写的开源的持续集成工 ...

随机推荐

  1. 重磅来袭!!!Elasticsearch7.14.1(ES 7.14.1)与Springboot2.5.4的整合

    1. 概述 前面我们聊了 Elasticsearch(ES)集群的搭建,今天我们来聊一下,Elasticsearch(ES)集群如何与 Springboot 进行整合. Elasticsearch(E ...

  2. javascript 中介者模式 mediator

    * player.js /** * 中介者模式 * @param {*} name 角色名称 * @param {*} teamColor 队伍颜色 */ function Player(name, ...

  3. linux环境下,使用Navicat连接mysql时,提示本地IP无法连接虚拟环境下的mysql解决方案

    在Linux环境下,使用Navicat连接mysql时,提示本地IP无法连接虚拟环境下的mysql,提示如下: 而导致连接错误的原因是MYSQL没有开启远程登录权限. 解决方案: 在mysql命令中执 ...

  4. 鸿蒙内核源码分析(定时器篇) | 哪个任务的优先级最高 | 百篇博客分析OpenHarmony源码 | v31.02

    百篇博客系列篇.本篇为: v31.xx 鸿蒙内核源码分析(定时器篇) | 哪个任务的优先级最高 | 51.c.h .o 本篇说清楚定时器的实现 读本篇之前建议先读鸿蒙内核源码分析(总目录)其余篇. 运 ...

  5. 项目配置shiro原缓存注解失效

    项目用springboot + shiro + ehcache @cacheable 注解不起作用原因 Shiro框架初始化比Spring框架的某些部件早,导致使用@Autowire注入Shiro框架 ...

  6. cybersploit

    靶机准备 将ova文件导入虚拟机中,设置网络模式为NAT 从靶机描述得知包含3个flag kali扫描其ip netdiscover -r 192.168.164.0/24 渗透测试 namp扫描端口 ...

  7. Java(一)——基础知识

    引言 之前一直对 Java 怀有固执的偏见,以为 Java 是编写前端的语言,作为一个机械生,非常抗拒去学它. 但是最近接触一点以后,完全改观了先前的看法,于是开启了对 Java 的大学习. 一.数据 ...

  8. JavaEE & Tomcat 介绍

    目录 企业开发介绍 JavaEE 规范 Web 概述 系统结构简介 C/S 结构 B/S 结构 两种结构的区别及优略 Tomcat Tomcat 介绍 关于服务器 Tomcat下载与安装 Tomcat ...

  9. 纯代码实现wordpress图片本地化【5.2.2版本可用】

    最近用wordpress做了个网站,想复制些新闻过来用,但图片是别人的,就想怎么本地化,在网找到了方法,那就保存一下方法. 复制下面的代码,然后粘贴到你当前WordPress主题的模版函数(funct ...

  10. IDEA破解方法:重新刷新到30天【支持正版】

    IDEA破解方法:重新刷新到30天[支持正版] 步骤: 导入plugins.zhile.io 进入File-->Settings-->Plugins 点设置(齿轮符号)-->Mana ...