程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python管理jenkins持續集成

編輯:Python

一,jenkins安裝部署

Linux下Jenkins 安裝方式很多,源碼包(WAR 文件)安裝,鏡像服務安裝,yum命令安裝等
命令安裝最簡單,其他方式可以自己baidu
安裝步驟如下

1, 安裝命令

sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
sudo yum install jenkins

2, 修改配置

# 修改端口
vim /etc/sysconfig/jenkins

3,啟動命令

service jenkins start 啟動
service jenkins stop 停止
service jenkins restart 重啟

4,首次登錄

第一次登錄需要密碼,根據提示:

cat /var/lib/jenkins/secrets/initialAdminPassword


首次登錄,需要下載一些插件,可以使推薦的插件,也可以自己選擇下載


安裝完成後,自動進入管理賬戶注冊頁面,注冊好了,進入登錄頁面

5,jenkins一般和項目交互,需要一些用戶憑證(eg:git)

二,python-jenkins

工作中我們會想通過http方式運行Jenkins job,這樣就不需要每次都進入jenkins的頁面操作了,而且可以統一集成。
在Jenkins官網上的Python-Jenkins API。
Python-Jenkins官網:https://pypi.python.org/pypi/python-jenkins/
Python-Jenkins Doc:http://python-jenkins.readthedocs.io/en/latest/index.html

1,安裝

pip install python-jenkins

2,基本使用

import jenkins
# 登錄jenkins
server = jenkins.Jenkins('http://192.168.59.149:8080', username='user', password='password')
# 構建參數化job
server.build_job('jobname', {'param1': 'aaa', 'param2': 'bbb'})

3,項目封裝

import jenkins
import logging
# 日志格式設置
logger = logging.getLogger('jenkins')
class JenkinsDevOps:
def __init__(self, jenkins_info_dict):
self.url = jenkins_info_dict["url"]
self.username = jenkins_info_dict["username"]
self.password = jenkins_info_dict["password"]
self.jenkins_server = jenkins.Jenkins(url=self.url, username=self.username, password=self.password)
def determine_job(self, job_name):
"""
檢測項目是否存在
:param job_name: 項目名稱
:return:
"""
return self.jenkins_server.get_job_name(job_name)
def build_job(self, job_name, parameters=None):
"""
:param job_name: name of job
:param parameters: parameters for job, or ``None``, ``dict``
:return:
"""
self.jenkins_server.build_job(job_name, parameters=parameters)
def get_last_success_build(self, job_name):
"""
獲取上次成功的構建
:param job_name:
:return:
"""
number = self.jenkins_server.get_job_info(job_name)["lastSuccessfulBuild"]["number"]
return number
def stop_job(self, job_name, number):
"""
獲取構建結果的日志
:param job_name: 項目名稱
:param number: 構建號
"""
self.jenkins_server.stop_build(job_name, number)
def create_job(self, data):
"""
新建項目job
:param data: 包含job新建格式文本config.xml
:return:
"""
res = self.jenkins_server.create_job('app-run', open(data['config_xml'], encoding='utf-8').read())
return res
if __name__ == '__main__':
my_parameters = {"branch": "test", "item": "my_test_item"}
my_jenkins_dict = {"url": "https://jenkins.xxx.cn/",
"username": "admin", "password": "xxxxxxxx"}
item_name = "test-slave"
# jenkins_main(my_jenkins_dict, item_name, my_parameters)

4,job新建格式文本config.xml

<?xml version='1.1' encoding='UTF-8'?>
<project>
<actions/>
<description></description>
<keepDependencies>false</keepDependencies>
<properties>
<hudson.model.ParametersDefinitionProperty>
<parameterDefinitions>
<hudson.model.StringParameterDefinition>
<name>Branch</name>
<description>分支</description>
<defaultValue>dev</defaultValue>
<trim>false</trim>
</hudson.model.StringParameterDefinition>
<hudson.model.StringParameterDefinition>
<name>Version</name>
<description>版本號</description>
<defaultValue>7.0.0-devops</defaultValue>
<trim>false</trim>
</hudson.model.StringParameterDefinition>
</parameterDefinitions>
</hudson.model.ParametersDefinitionProperty>
</properties>
<scm class="hudson.plugins.git.GitSCM" plugin="[email protected]">
<configVersion>2</configVersion>
<userRemoteConfigs>
<hudson.plugins.git.UserRemoteConfig>
<url>http://gitlab.com/hermes/system/app-job.git</url>
<credentialsId>e11f0f10-d1a9-4d36-86b5-9184051c8cc5</credentialsId>
</hudson.plugins.git.UserRemoteConfig>
</userRemoteConfigs>
<branches>
<hudson.plugins.git.BranchSpec>
<name>$Branch</name>
</hudson.plugins.git.BranchSpec>
</branches>
<doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
<submoduleCfg class="empty-list"/>
<extensions/>
</scm>
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers/>
<concurrentBuild>false</concurrentBuild>
<builders>
<hudson.tasks.Shell>
<command>bash /opt/newcicd/scripts/java_build_job.sh</command>
<configuredLocalRules/>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
</project>[

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved