jenkins插件集成api使用

jenkins配置插件&api使用

jenkins

https://mirrors.jenkins.io/war-stable/2.222.1/ 包下载地址

jenkins镜像源修改

sed -i 's/https:\/\/www.google.com/https:\/\/www.baidu.com/g'  default.json
sed -i 's/https:\/\/updates.jenkins.io\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g'  default.jsonhttp://mirror.xmission.com/jenkins/plugins/

java

https://www.oracle.com/cn/java/technologies/javase/javase8-archive-downloads.html java下载

tar xf jdk-8u144-linux-x64.tar.gz
mv jdk-8u144-linux-x64 /usr/local/jdk
vim ~/.bashrc
export JAVA_HOME=/usr/local/jdk     #建议使用自带的java,不然会出现版本兼容各种问题
export PATH=${JAVA_HOME}/bin:$PATH

启动jenkins

nohup java -Dhudson.model.DownloadService.noSignatureChecke=true \
-DJENKINS_HOME=/app/jenkins -jar /app/jenkins/jenkins.war &

maven

https://maven.apache.org/download.cgi

https://dlcdn.apache.org/maven/maven-3/3.9.0/binaries/apache-maven-3.9.0-bin.tar.gz

[root@vm ~]# tar zxf apache-maven-3.9.0-bin.tar.gz -C /usr/local/
[root@vm ~]# vim /etc/profile
...
export MAVEN_HOME=/usr/local/apache-maven-3.6.0
export PATH=$PATH:$MAVEN_HOME/bin
[root@vm ~]# source /etc/profile
[root@vm ~]# mvn -v
Apache Maven 3.9.0 maven clean 清理构建目录
maven clean package 打包
maven clean install 打包部署
maven clean test 单元测试

系统设置->全局工具配置->Maven->Maven 安装
在这里插入图片描述

创建一条pipeline任务

pipeline {agent anytools { 		maven 'maven'    // maven 与全局配置里的名字保持一致  }stages {stage('Example'){steps {echo 'Hello World'script {mavenHome = tool 'maven'    //  工具名println(mavenHome)sh "${mavenHome}/bin/mvn -v"}}}}      
}
mvn clean install -DskipTests
mvn clean package 

ant

https://archive.apache.org/dist/ant/binaries/apache-ant-1.10.10-bin.tar.gz

[root@vm ~]# tar zxf apache-ant-1.10.10-bin.tar.gz -C /usr/local/
[root@vm ~]# vim /etc/profile
...
export ANT_HOME=/usr/local/apache-ant-1.10.10
export PATH=$PATH:$MAVEN_HOME/bin:$ANT_HOME/bin
source /etc/profile
[root@vm ~]# source /etc/profile
pipeline {agent anytools { 		ant 'ANT'    // ANT 与全局配置里的名字保持一致  }stages {stage('Example'){steps {script {antHome = tool 'ANT'    //  工具名sh "${antHome}/bin/ant -version"}}}}      
}

gradle

https://downloads.gradle-dn.com/distributions/gradle-7.0-bin.zip

[root@vm ~]# unzip gradle-7.0-bin.zip  -d /usr/local/
[root@vm ~]# vim /etc/profile
...
export GRADLE_HOME=/usr/local/gradle-7.0
export PATH=$PATH:$GRADLE_HOME/bin
source /etc/profile
[root@vm ~]# source /etc/profile

Gradle常用命令

gradlew -v 	#版本号,首次运行,没有gradle的要下载的哦。
gradlew clean 	#删除HelloWord/app目录下的build文件夹
gradlew build 	#检查依赖并编译打包
gradlew assembleDebug 	#编译并打Debug包
gradlew assembleRelease 	#编译并打Release的包
gradlew installRelease Release	#模式打包并安装
gradlew uninstallRelease 	#卸载Release模式包

node

https://nodejs.org/download/release/v14.10.1/node-v14.10.1-linux-x64.tar.xz

[root@vm ~]# tar xf node-v14.10.1-linux-x64.tar.xz -C /usr/local/
[root@vm ~]# vim /etc/profile
...
export NODE_HOME=node-v14.10.1-linux-x64
export PATH=$PATH:$NODE_HOME/bin
[root@vm ~]# source /etc/profile
node {stage ("npmbuild"){sh """export npmHome=/usr/local/node-v14.10.1-linux-x64export PATH=\$PATH:\$npmHome/binnpm -v"""}    
}	
tools 固定命名,Valid tool types: [ant, hudson.tasks.Ant$AntInstallation, git, hudson.plugins.git.GitTool, gradle, hudson.plugins.gradle.GradleInstallation, jdk, hudson.model.JDK, jgit, org.jenkinsci.plugins.gitclient.JGitTool, jgitapache, org.jenkinsci.plugins.gitclient.JGitApacheTool, maven, hudson.tasks.Maven$MavenInstallation]    }

try-catch异常抛出

// 不做抛出的话,流水线报错是执行失败的状态, 加上try,部分步骤没执行,但流水线是绿色OK的
pipeline {agent anytools { 		jdk 'jdk17'    // maven 与全局配置里的名字保持一致  }stages {stage('Example'){steps {echo 'Hello World'script {try {mavenHome = tool 'jdk17'    //  工具名println(mavenHome)sh "${mavenHome}/bin/mvn -v"} catch(e){println(e)}}}}}      
}

分支标签参数选择

pipeline {agent anytools { 		jdk 'jdk17'    // maven 与全局配置里的名字保持一致  }parameters {gitParameter name: 'branch', type: 'PT_BRANCH_TAG',branchFilter: 'origin/(.*)',defaultValue: 'master',selectedValue: 'DEFAULT',sortMode: 'DESCENDING_SMART',description: 'Select your branch or tag.'           }stages {stage('checkout') {steps {git branch: "${params.branch}", url: "https://gitee.com/wg_pub_test/pub_test.git"sh "cat jenkinsfile"}}}      
}

选项参数

pipeline {agent anyparameters {choice(name: 'branch',choices: ['develop', 'feature'],description: '选择分支')}stages {stage('checkout') {steps {sh "echo ${params.branch}"}}}      
}

saltstack

https://mirrors.tuna.tsinghua.edu.cn/saltstack/#rhel
yum install https://repo.saltstack.com/py3/redhat/salt-py3-repo-latest.el8.noarch.rpm
String deployHosts = "host1,host02"
pipeline{agent anystages{stage("Deploy"){steps{script{hosts = deployHosts.split(",").toList()for(host in hosts){//sh "salt ${host}cmd.run ls"println(host)}}}}}
}

ansible

yum install epel-release ansbile

LDAP认证集成

phpldapadmin、openldap  两个容器服务 
LDAP 插件

Jenkins API

http://192.168.1.11:8080/api/
Python API
Web REST API
jenkins Core API

Python API

库名称:python-jenkins
仓库Pypi : https://pypi.org/project/python-jenkins/
在线文档: http://python-jenkins.readthedocs.org/en/latest/jenkinsapi库也能使用,暂未研究
Create newjobs 创建新项目
Copy existingjobs 复制已存在的项目
Delete jobs 删除项目
Update jobs 更新项目
Get a job's build information 获取一个项目的构建信息
Get Jenkins master version information 获取Jenkins master的版本信息
Get Jenkins plugin information 过去jenkins插件信息
Start a build on ajob 构建一个项目Create nodes 创建一个节点
Create nodes 创建一个节点
Enable/Disable nodes 启用/禁用节点
Getinformation on nodes 获取节点信
息Create/delete/reconfig views 创建/删除/更新视图
Put server in shutdown mode (quiet down)关机
List running builds 列出构建中的项目
Delete builds 删除构建
Wipeout job workspaceCreate/delete/update folders 创建/删除/更新文件夹
Set the next build number 设置下次构建ID
Install plugins 安装插件
# pip install python-jenkins==1.6.0import jenkins
server=jenkins.Jenkins("http://192.168.1.11:8080",username="admin" ,password="123456")
import jenkinsserver = jenkins.Jenkins("http://192.168.1.11:8080", username="admin", password="123456")print(server.get_whoami())
print(dir(server))   #查看 所有的⽅法

do操作方法
在这里插入图片描述

get获取方法

在这里插入图片描述

其他方法

在这里插入图片描述

每个接口的使用方法:https://python-jenkins.readthedocs.io/en/latest/api.html

import jenkinsserver = jenkins.Jenkins("http://192.168.1.11:8080", username="admin", password="123456")print(server.job_exists("newpipe"))		# 判断job存在
server.copy_job("pipe","newpipe")  # 复制一个项目
server.create_job(name, config_xml)	# 需要准备xml文件
print(server.get_job_config('pipe'))	# 打印匹配的config_xml
<?xml version='1.1' encoding='UTF-8'?>
<flow-definition plugin="workflow-job@1326.ve643e00e9220"><actions><org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobAction plugin="pipeline-model-definition@2.2150.v4cfd8916915c"/><org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobPropertyTrackerAction plugin="pipeline-model-definition@2.2150.v4cfd8916915c"><jobProperties/><triggers/><parameters/><options/></org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobPropertyTrackerAction></actions><description></description><keepDependencies>false</keepDependencies><properties><jenkins.model.BuildDiscarderProperty><strategy class="hudson.tasks.LogRotator"><daysToKeep>3</daysToKeep><numToKeep>5</numToKeep><artifactDaysToKeep>-1</artifactDaysToKeep><artifactNumToKeep>-1</artifactNumToKeep></strategy></jenkins.model.BuildDiscarderProperty></properties><definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps@3837.v305192405b_c0"><script>String deployHosts = &quot;host1,host02&quot;
pipeline{agent anystages{stage(&quot;Deploy&quot;){steps{script{hosts = deployHosts.split(&quot;,&quot;).toList()for(host in hosts){//sh &quot;salt ${host}cmd.run ls&quot;println(host)}}}}}
}</script><sandbox>true</sandbox></definition><triggers/><disabled>false</disabled>
</flow-definition>

修改config_xml文件,创建新的任务

import jenkins
# import re  #配合正则做配置更新
serverUrl="http://192.168.1.11:8080"
username = "admin"
password ="123456"
server = jenkins.Jenkins(serverUrl,username ,password)
defProjectName ="pipe"
newProjectName ="my_new_pipe"if server.job_exists(newProjectName)!= True :print("项目不存在开始新建项目")config_xml=server.get_job_config(defProjectName)	# 获取config_xmlnewconfig_xml = config_xml.replace("println(host)","println(host)\nprintln(\"hello\")")print(newconfig_xml)server.create_job(newProjectName,newconfig_xml)
else:print("项目已存在!")

更新配置

import jenkinsserverUrl = "http://192.168.1.11:8080"
username = "admin"
password = "123456"
server = jenkins.Jenkins(serverUrl, username, password)
defProjectName = "pipe"
newProjectName = "free_style"if server.job_exists(newProjectName) != True:print("项目不存在开始新建项目")config_xml = server.get_job_config(defProjectName)newconfig_xml = config_xml.replace("println(host)", "println(host)\nprintln(\"hello\")")server.create_job(newProjectName, newconfig_xml)
else:config_xml = server.get_job_config(defProjectName)new_config_xml = config_xml.replace("$J_branch", "$H_branch")   # 分支参数$J_branch 改成$H_branchserver.upsert_job("free_style", new_config_xml)server.build_job("free_style",parameters=None,token=None)  #job命名不要用中文

批量创建job

import jenkinsserverUrl = "http://192.168.1.11:8080"
username = "admin"
password = "123456"
server = jenkins.Jenkins(serverUrl, username, password)
defProjectName = "temp_back"  # temp_web    需要先创建好,使用xml文件
job_list = ["free_job2", "free_job3", "free_job4", "free_job5","free_job6"]for ProjectName in job_list:newProjectName = ProjectName + "_back"    # 前端可以 + "_web"if server.job_exists(newProjectName) != True:print(newProjectName + "项目不存在,开始新建项目")config_xml = server.get_job_config(defProjectName)newconfig_xml = config_xml.replace("appname", newProjectName)  # 替换部分关键信息,如代码仓名server.create_job(newProjectName, newconfig_xml)print(newProjectName + "已创建")else:print(newProjectName + "已存在")

Web REST API

API首页:http://127.0.0.1:8080/api/
http://192.168.1.11:8080/job/my_new_pipe/api/json
# get
获取项目信息接口:http://127.0.0.1:8080/job/{projectName}/api/json获取项目构建信息接口:http://127.0.0.1:8080/job/{projectName}/{buildNumber}/api/json获取项目配置接口:http://127.0.0.1:8080/job/{projectName}/config.xml
# post
创建项目接口: http://127.0.0.1:8080/createltem?name={projectName)参数: --data-binary @config.xml 头部: -H "Content-Type:text/xml"禁用项目接口:http://127.0.0.1:8080/job/{projectName}/disable启用项目接口:http://127.0.0.1:8080/job/{projectName}/enable删除项目接口:http://127.0.0.1:8080/job/{projectName}/delete构建项目接口:http://127.0.0.1:8080/job/{projectName}/build
。 方式:POST
参数化构建接口:http://127.0.0.1:8080/job/{projectName}/buildWithParameters[root@vm ~]# ls config.xml
[root@vm ~]# curl -X POST -u admin:123456 http://127.0.0.1:8080/createltem?name={projectName)  --data-binary @config.xml -H "Content-Type:text/xml"

如果没有做认证集成:用户名+密码 如果做了集成:用户名+token

pipeline{agent anystages{stage("test"){steps{script{httpRequest authentication: 'pipeline',httpMode: 'POST',responseHandle: 'NONE',url: 'http://192.168.1.11:8080/job/newpipe/disable'}}}}
}

jenkins Core API

需java基础

jira创建需求(也可关联版本) --> jenkins 插件创建分支–> 触发 gitlab 创建分支 gitlab提交代码触发 jenkins 构建

Jira(需求)+ GitLab(代码)+Jenkins(CI)+Spinnaker(CD一套微服务架构)

插件了解

Jenkins自带推荐插件:Git,Folders,BuildTimeout,Credentials Binding,Timestamper,Workspace Cleanup,Ant,Gradle,Pipeline,GitHub Branch Source等等

Git parameter git参数插件

Role-Based Strategy 角色管理

Generic Webhook Trigger 触发构建

gitlab plugin 触发构建

ssh Publisher 上传执行shell

saltstack plugin 批量自动化发布

log-parser-plugin 增加过滤条件显示日志

生成Jenkinfile方法(项目->Pipeline Syntax->片段生成器)选择salt: Send a message to Salt API补充信息

rancher插件 更新镜像

Nexus Artifact Uploader上传制品

Maven Artifact ChoiceListProvider (Nexus) 选择构建包

jenkins发版

前端打包

yarn install  && npm run build:uat  
#方法一
rsync -az --delete  dist/  /app/nginx/static/test/#方法二
cp -rf dist/*   test/ && zip -r static.zip test/*
unzip -od /app/nginx/static static.zip方法三
zip -r $BUILD_TAG.zip dist/*
mv *.zip /app/
ansible -i /root/ansible/hostip proxy -m copy -a \
'src=/app/$BUILD_TAG-admin.zip dest=/usr/share/nginx/html/'
ansible -i /root/ansible/hostip proxy -m shell -a \
'unzip -o /usr/share/nginx/html/$BUILD_TAG-admin.zip -d /usr/share/nginx/html/"

后端打包

mvn -T 8 clean install -Dmaven.test.skip=true
mvn clean package -s ./settings.xml -Dmaven.test.skip=true

打镜像

FROM openjdk:17.0.2
ADD ./api-0.0.1-SNAPSHOT.jar /app.jar
ENV JAVA_OPTS "-Xms3072m -Xmx3072m -XX:NewRatio=3 -XX:SurvivorRatio=4 -XX:HeapDumpPath=/logs/dump/front/document_service.dump"
ENTRYPOINT exec java $JAVA_OPTS -jar /app.jartag=`date -d today +"%Y%m%d%H%M"
docker build -t kw.com:80/app/admin:$tag .
docker push kw.com:80/app/admin:$tag
docker rmi kw.com:80/app/admin:$tag
#打通k8s
export KUBECONFIG=$HOME/.kube/config:$HOME/kube/config_perf
kubectl config use-context dev
kubectl set image deployment/admin  container-1=kw.com:80/app/admin:$tag

jar包运行

tag=`date -d today +"%Y%m%d%H%M"
mv ./target/admin.jar /app/admin-$tag.jar
ansible dev -m copy -a "src=/app/admin-$tag.jar dest=/app/dev/"
ansible dev -m shell -a "sed -i 's/admin-[0-9]*.jar/admin-$tag.jar/g' \
/etc/supervisor/conf.d/admin.conf"
ansible dev -m shell -a "supervisorctl update && supervisorctl restart admin"
ansible dev -m shell -a "bash /etc/supervisor/conf.bak/rm.sh"
rm /app/admin-$tag.jar

supervisor服务

cat admin.conf
[program:admin]
command=bash -c "/usr/bin/java -jar -Dhwenvironment=uat /app/dev/admin-282204291446.jar"
redirect stderr= true
autorestart=true
startretries=999
stopwaitsecs=300
stdout_logfile=//app/dev/log/admin-stdout-supervisor.log
stdout_logfile_maxbytes=50MB
stdout_logfile_backups=20

jar包清理脚本

cat rm.sh
for i in  cheat common home admin
do
num=`ls /app/dev/.jar|grep $i |wc -l`
jar=`ls -lrt /app/dev/*.jar |grep $i|awk '{print $NF}' |head -1`
if [ $num -gt 5 ]thenrm -f $jar & echo "delete $jar"
fi
done

“supervisorctl update && supervisorctl restart admin”
ansible dev -m shell -a “bash /etc/supervisor/conf.bak/rm.sh”
rm /app/admin-$tag.jar


supervisor服务```sh
cat admin.conf
[program:admin]
command=bash -c "/usr/bin/java -jar -Dhwenvironment=uat /app/dev/admin-282204291446.jar"
redirect stderr= true
autorestart=true
startretries=999
stopwaitsecs=300
stdout_logfile=//app/dev/log/admin-stdout-supervisor.log
stdout_logfile_maxbytes=50MB
stdout_logfile_backups=20

jar包清理脚本

cat rm.sh
for i in  cheat common home admin
do
num=`ls /app/dev/.jar|grep $i |wc -l`
jar=`ls -lrt /app/dev/*.jar |grep $i|awk '{print $NF}' |head -1`
if [ $num -gt 5 ]thenrm -f $jar & echo "delete $jar"
fi
done

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/596191.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

slam数学补充

协方差矩阵的计算 M(p̃ − μ)(p̃ − μ) T / (n1) μ Σ p̃ / (n1) 协方差矩阵的意义 以及 特征向量和特征值的关系 从数据表面看&#xff1a; 当M(n,n)过大意味着在数据均值的n维上变化较大&#xff0c;反之M(n,n)过小意味着数据在均值 的n维上变化不大。 当M(n,m)过…

教你快速认识Java中的抽象类和接口

目录 引言 抽象类&#xff08;Abstract Class&#xff09; 抽象类的概念 抽象类的图标 抽象类的语法 抽象类的特点 接口&#xff08;Interface&#xff09; 接口的概念 接口的图标 接口的语法 接口的特点 接口的使用 接口的意义 抽象类与接口的区别 Object类 结…

VMware配置环境(安装运行问题)及系列dns端口网络类型IP远程连接学习之(详谈8000字)

安装vmware快速配置步骤 下载VMware安装包 在下载好VMware安装包之后双击运行 接受条款 关闭VMware自动更新 勾选快捷键方式 安装VMware安装 输入许可证&#xff08;有需要私信小编&#xff09; 安装完成 重启电脑即可 最终成功界面: 安装Linux系统 创建虚拟机 选择…

递归实现排列型枚举(acwing)

题目描述&#xff1a; 把 1∼n 这 n 个整数排成一行后随机打乱顺序&#xff0c;输出所有可能的次序。 输入格式&#xff1a; 一个整数 n。 输出格式&#xff1a; 按照从小到大的顺序输出所有方案&#xff0c;每行 1 个。 首先&#xff0c;同一行相邻两个数用一个空格隔开…

在同一个局域网如何共享打印机和文件

1.在连接了打印机的主机上设置 1.1启用windows共享 打开网络与共享中心&#xff0c;点击“更改高级共享设置” 选择&#xff1a; “启用网络发现”“启用文件和打印机共享”“启用共享以便可以访问网络的用户可以读取和写入公用文件夹中的文件” 打开控制面板&#xff0c;选…

使用idea 调试HashMap时出现ExpiringCache

问题背景&#xff1a; 调试HashMap源码验证是在第一次put才创建table时&#xff0c;发现刚new出来HashMap表的size已经有值了&#xff1f;&#xff01;&#xff01;而且都是关于ExpiringCache之类的 解决办法&#xff1a; 开启调试前只在main方法里打断点&#xff0c;程序启动…

防抖节流面试

1、防抖 1.1、条件 1、高频 2、耗时&#xff08;比如console不算&#xff09; 3、以最后一次调用为准 刷到个神评论&#xff0c;回城是防抖&#xff0c;技能cd是节流 1.2、手写 传参版本 function debounce(fn,delay){let timerreturn function(...args){//返回函数必须是普…

之前翻硬币问题胡思乱想的完善

题目背景 小明正在玩一个“翻硬币”的游戏。 题目描述 桌上放着排成一排的若干硬币。我们用 * 表示正面&#xff0c;用 o 表示反面&#xff08;是小写字母&#xff0c;不是零&#xff09;&#xff0c;比如可能情形是 **oo***oooo&#xff0c;如果同时翻转左边的两个硬币&#x…

秋招刷题4(动态规划)

1.购物单 import java.util.Scanner;public class Main {public static void main(String[] args){Scanner sc new Scanner(System.in);int N sc.nextInt();int m sc.nextInt();Goods[] goods new Goods[m];for(int i 0; i < m; i){goods[i] new Goods();}for(int i …

【cpp】快速排序优化

标题&#xff1a;【cpp】快速排序 水墨不写bug 正文开始&#xff1a; 快速排序的局限性&#xff1a; 虽然快速排序是一种高效的排序算法&#xff0c;但也存在一些局限性&#xff1a; 最坏情况下的时间复杂度&#xff1a;如果选择的基准元素不合适&#xff0c;或者数组中存在大…

ubuntu-server部署hive-part1-安装jdk

参照 https://blog.csdn.net/qq_41946216/article/details/134345137 操作系统版本&#xff1a;ubuntu-server-22.04.3 虚拟机&#xff1a;virtualbox7.0 安装jdk 上传解压 以root用户&#xff0c;将jdk上传至/opt目录下 tar zxvf jdk-8u271-linux-x64.tar.gz 配置环境变量…

YOLOv5实战记录05 Pyside6可视化界面

个人打卡&#xff0c;慎看。 指路大佬&#xff1a;【手把手带你实战YOLOv5-入门篇】YOLOv5 Pyside6可视化界面_哔哩哔哩_bilibili 零、虚拟环境迁移路径后pip报错解决 yolov5-master文件夹我换位置后&#xff0c;无法pip install了。解决如下&#xff1a; activate.bat中修改…