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.json

http://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

sh 复制代码
[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任务

groovy 复制代码
pipeline {
    agent any
    
    tools { 		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

sh 复制代码
[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
groovy 复制代码
pipeline {
    agent any
    
    tools { 		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

shell 复制代码
[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常用命令

sh 复制代码
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

sh 复制代码
[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
groovy 复制代码
node {
    stage ("npmbuild"){
        sh """
           export npmHome=/usr/local/node-v14.10.1-linux-x64
           export PATH=\$PATH:\$npmHome/bin
           npm -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异常抛出

groovy 复制代码
// 不做抛出的话,流水线报错是执行失败的状态, 加上try,部分步骤没执行,但流水线是绿色OK的
pipeline {
    agent any
    
    tools { 		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)
                    }
                }
            }
        }
    }      
}

分支标签参数选择

groovy 复制代码
pipeline {
    agent any
    
    tools { 		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"
            }
        }
    }      
}

选项参数

groovy 复制代码
pipeline {
    agent any

    parameters {
        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
groovy 复制代码
String deployHosts = "host1,host02"
pipeline{
    agent any
    
    stages{
        
        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 安装插件
python 复制代码
# pip install python-jenkins==1.6.0

import jenkins
server=jenkins.Jenkins("http://192.168.1.11:8080",username="admin" ,password="123456")
import jenkins

server = 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

python 复制代码
import jenkins

server = 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 复制代码
<?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 any
    
    stages{
        
        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文件,创建新的任务

python 复制代码
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_xml
    newconfig_xml = config_xml.replace("println(host)","println(host)\nprintln(\"hello\")")
    print(newconfig_xml)
    server.create_job(newProjectName,newconfig_xml)
else:
    print("项目已存在!")

更新配置

python 复制代码
import jenkins

serverUrl = "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_branch
    server.upsert_job("free_style", new_config_xml)
    server.build_job("free_style",parameters=None,token=None)  #job命名不要用中文

批量创建job

python 复制代码
import jenkins

serverUrl = "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

sh 复制代码
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
sh 复制代码
# 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

groovy 复制代码
pipeline{
    agent any

    stages{
        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发版

前端打包

sh 复制代码
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/"

后端打包

sh 复制代码
mvn -T 8 clean install -Dmaven.test.skip=true
mvn clean package -s ./settings.xml -Dmaven.test.skip=true

打镜像

sh 复制代码
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.jar


tag=`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
sh 复制代码
#打通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包运行

sh 复制代码
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服务

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包清理脚本

sh 复制代码
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 ]
	then
	rm -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包清理脚本

sh 复制代码
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 ]
	then
	rm -f $jar & echo "delete $jar"
fi
done
相关推荐
。puppy13 分钟前
HCIP--3实验- 链路聚合,VLAN间通讯,Super VLAN,MSTP,VRRPip配置,OSPF(静态路由,环回,缺省,空接口),NAT
运维·服务器
颇有几分姿色22 分钟前
深入理解 Linux 内存管理:free 命令详解
linux·运维·服务器
光芒再现dev39 分钟前
已解决,部署GPTSoVITS报错‘AsyncRequest‘ object has no attribute ‘_json_response_data‘
运维·python·gpt·语言模型·自然语言处理
AndyFrank1 小时前
mac crontab 不能使用问题简记
linux·运维·macos
成都古河云2 小时前
智慧场馆:安全、节能与智能化管理的未来
大数据·运维·人工智能·安全·智慧城市
算法与编程之美2 小时前
文件的写入与读取
linux·运维·服务器
Amelio_Ming2 小时前
Permissions 0755 for ‘/etc/ssh/ssh_host_rsa_key‘ are too open.问题解决
linux·运维·ssh
心灵彼岸-诗和远方2 小时前
Devops业务价值流:软件研发最佳实践
运维·产品经理·devops
JuiceFS3 小时前
好未来:多云环境下基于 JuiceFS 建设低运维模型仓库
运维·云原生
Ven%3 小时前
centos查看硬盘资源使用情况命令大全
linux·运维·centos