Click on the blue word aboveFocus on “DevOps Cloud School” to receive the latest technology practice
Today is the 26 day of “DevOps Cloud School” progressing with you
If this article is helpful to you, please forward it, like it and share it. Your attention is the motivation for me to continue sharing!
Introduction
This time we are going to learn the Jenkins API interface, we first use the Python-jenkins library to complete it.
Warehouse Pypi: https://pypi.org/project/python-jenkins/
Online documentation: http://python-jenkins.readthedocs.org/en/latest/
Current environment Python version v3.7.0
Function
-
Create new jobs Create new projects
-
Copy existing jobs Copy existing projects
-
Delete jobs Delete projects
-
Update jobs Update projects
-
Get a job’s build information Get a project’s build information
-
Get Jenkins master version information Get the version information of Jenkins master
-
Get Jenkins plugin information past jenkins plugin information
-
Start a build on a job Build a project
-
Create nodes Create a node
-
Enable/Disable nodes Enable/Disable nodes
-
Get information on nodes Get node information
-
Create/delete/reconfig views Create/delete/update views
-
Put server in shutdown mode (quiet down) shutdown
-
List running builds lists the projects in the build
-
Delete builds Delete builds
-
Wipeout job workspace
-
Create/delete/update folders Create/delete/update folders
-
Set the next build number Set the next build ID
-
Install plugins install plugins
Use ideas
For example I want to create a project
-
To find a way to create a project first
-
Then check how to use each interface according to the API documentation
-
Open a python interpreter for debugging
Initialization configuration
Install python-jenkins
pip install python-jenkins==1.6.0
test
ZeyangdeMacBook-Pro:~ zeyang$ python3 Python 3.7.4 (v3.7.4:e09359112e, Jul 8 2019, 14:54:52) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import jenkins >>> server = jenkins.Jenkins("http://127.0.0.1:8080",username="admin",password="admin") >>> server. get_whoami() {'_class': 'hudson.model.User', 'absoluteUrl': 'http://127.0.0.1:8080/user/admin', 'description': '\ ', 'fullName': 'admin', 'id': 'admin', 'property': [{'_class': 'jenkins.security.ApiTokenProperty'} , {'_class': 'jenkins.security.LastGrantedAuthoritiesProperty'}, {'_class': 'hudson.model.MyViewsProperty'}, {'_class': 'hudson.model. PaneStatusProperties'}, {'_class': 'hudson.security.HudsonPrivateSecurityRealm$Details'}, {'_class': 'org.jenkinsci.main.modules.cli.auth.ssh.UserPropertyImpl\ '}, {'_class': 'jenkins.security.seed.UserSeedProperty'}, {'_class': 'hudson.search.UserSearchProperty', 'insensitiveSearch': True}, { '_class': 'hudson.model.TimeZoneProperty'}]}
view all methods
>>> dir(server) ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', \ '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__ ', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__' , '__weakref__', '_add_missing_builds', '_auth_resolved', '_auths', '_build_url', '_get_encoded_params', '_get_job_folder', '_get_tag_text', \ '_get_view_jobs', '_maybe_add_auth', '_request', '_response_handler', '_session', '_timeout_warning_issued', 'assert_credential_exists', 'assert_folder', 'assert_job_exists ', 'assert_node_exists', 'assert_promotion_exists', 'assert_view_exists', 'auth', 'build_job', 'build_job_url', 'cancel_queue', 'check_jenkinsfile_syntax' , 'copy_job', 'create_credential', 'create_folder', 'create_job', 'create_node', 'create_promotion', 'create_view', 'credential_exists', \ 'crumb', 'debug_job_info', 'delete_build', 'delete_credential', 'delete_job', 'delete_node', 'delete_promotion', 'delete_view', 'disable_job ', 'disable_node', 'enable_job', 'enable_node', 'get_all_jobs', 'get_build_console_output', 'get_build_env_vars', 'get_build_info', 'get_build_test_report' , 'get_credential_config', 'get_credential_info', 'get_info', 'get_job_config', 'get_job_info', 'get_job_info_regex', 'get_job_name', 'get_jobs', \ 'get_node_config', 'get_node_info', 'get_nodes', 'get_plugin_info', 'get_plugins', 'get_plugins_info', 'get_promotion_config', 'get_promotion_name', 'get_promotions ', 'get_promotions_info', 'get_queue_info', 'get_queue_item', 'get_running_builds', 'get_version', 'get_view_config', 'get_view_name', 'get_views' , 'get_whoami', 'install_plugin', 'is_folder', 'jenkins_open', 'jenkins_request', 'job_exists', 'jobs_count', 'list_credentials', \ 'maybe_add_crumb', 'node_exists', 'promotion_exists', 'quiet_down', 'reconfig_credential', 'reconfig_job', 'reconfig_node', 'reconfig_promotion', 'reconfig_view ', 'rename_job', 'run_script', 'server', 'set_next_build_number', 'stop_build', 'timeout', 'upsert_job', 'view_exists' , 'wait_for_normal_op', 'wipeout_job_workspace']
how to do it
function | python method |
---|---|
Project Operation | create_job disable_job delete_job copy_job enable_job job_exists jobs_count upsert_job |
Build Operation | build_job delete_build build_job_url stop_build |
Credential operations | create_credential delete_credentialcredential_exists |
View operations | create_view delete_view view_exists |
Node operation | create_node disable_node delete_node enable_node node_exists |
Promotion operation | create_promotion delete_promotion promotion_exists |
Cancel queue | cancel_queue |
Check jenkinsfile syntax | check_jenkinsfile_syntax |
Check project information | debug_job_info |
Folder operation | is_folder create_folder |
Install plugin | install_plugin |
get method
function | python method |
---|---|
Get project info | get_all_jobs get_job_config get_job_info get_job_info_regex get_job_name get_jobs |
Get build info | get_build_console_output get_build_env_vars get_build_info get _build_test_report |
Get Credentials Info | get_credential_config get_credential_info list_credentials |
get_info | |
Get node information | get_node_config get_node_info get_nodes |
Get Plugin information | get_plugin_info get_plugins get_plugins_info |
Get promotion information | get_promotion_config get_promotion_name get_promotions get_promotions_info |
Get queue information | get_queue_info get_queue_item |
Get running builds | get_running_builds |
Get Version | get_version |
Get View Information | get_view_config get_view_name get_views |
Get current user | get_whoami |
jenkins_open | |
jenkins_request |
Update Action
function | python method |
---|---|
Shutdown | quiet_down |
Renew Credentials | reconfig_credential |
Renew Items | reconfig_job |
Update node | reconfig_node |
Update promotion | reconfig_promotion |
Update view | reconfig_view |
Rename item | rename_job |
Run script | run_script |
Set the next build id |
set_next_build_number |
Demo example
How to use each interface: https://python-jenkins.readthedocs.io/en/latest/api.html
Project Operation
We need to get acquainted with the config.xml of the Jenkins project. Most APIs use xml files when creating projects. First create a project (any type is fine) and then we enter the $JENKINS_HOME/jobs/ directory to view the config.xml file generated by our project.
builds stores the build information of the project.
ZeyangdeMacBook-Pro:jobs zeyang$ ls demo-test ZeyangdeMacBook-Pro:jobs zeyang$ ls demo-test/ buildsconfig.xml
config.xml
You can see that the file storage of jenkins is stored in xml. (Later we need to use this xml to create a new project)
<?xml version='1.1' encoding='UTF-8'?> <project> <description>test</description> <keepDependencies>false</keepDependencies> <properties/> <scm class="hudson.scm.NullSCM"/> <canRoam>true</canRoam> <disabled>false</disabled> <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding> <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding> <triggers/> <concurrentBuild>false</concurrentBuild> <builders/> <publishers/> <buildWrappers/> </project>
Using methods to get project configuration information
server.get_job_config("demo-test") '<?xml version=\'1.1\' encoding=\'UTF-8\'?>\\ <project>\\ <description>test</description>\ n <keepDependencies>false</keepDependencies>\\ <properties/>\\ <scm class="hudson.scm.NullSCM"/>\\ <canRoam>true</canRoam>\\ <disabled >false</disabled>\\ <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>\\ <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>\\ <triggers/>\\ <concurrentBuild>false</concurrentBuild>\\ < builders/>\\ <publishers/>\\ <buildWrappers/>\\ </project>'
Master how to create projects
create_job
(name, config_xml)
-
name project name string type
-
config_xml configuration file string type
New project demo-test-02
config_xml = server.get_job_config("demo-test") server.create_job("demo-test-02",config_xml)
Verify that the project was successfully created
server.job_exists("demo-test-02") True
Copy a new project demo-test-03
copy_job
(from_name, to_name)
-
from_name source item string type
-
to_name target item string type
An error will be reported when the source project name is the same as the target project name. JenkinsException
>>> server.copy_job("demo-test-02","demo-test-03") >>> server.job_exists("demo-test-03") True
Write a script for project creation
Create a new project and add a parameter srcType
to distinguish the type of version control system used by the project. Arguments can be (svn, git).
What we want to accomplish is to create a new project based on a project template and replace the relevant parameters. Template project name demo-devops-service
.
import jenkins #login serverUrl = "http://127.0.0.1:8080" username = "admin" password = "admin" server = jenkins. Jenkins(serverUrl, username, password) defProjectName = "demo-devops-service" newProjectName = "demo-test-service" if server.job_exists(newProjectName) != True : print("The project does not exist and start to create a new project") config_xml=server.get_job_config(defProjectName) newconfig_xml = config_xml.replace("<defaultValue>svn</defaultValue>","<defaultValue>git</defaultValue>") print(newconfig_xml) server.create_job(newProjectName, newconfig_xml) else: print("Item already exists!")
run output
ZeyangdeMacBook-Pro:codes zeyang$ python3 josbtest.py The project does not exist Start a new project <?xml version='1.1' encoding='UTF-8'?> <project> <actions/> <description></description> <keepDependencies>false</keepDependencies> <properties> <hudson.model.ParametersDefinitionProperty> <parameterDefinitions> <hudson.model.StringParameterDefinition> <name>srcType</name> <description></description> <defaultValue>git</defaultValue> <trim>false</trim> </hudson.model.StringParameterDefinition> </parameterDefinitions> </hudson.model.ParametersDefinitionProperty> </properties> <scm class="hudson.scm.NullSCM"/> <canRoam>true</canRoam> <disabled>false</disabled> <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding> <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding> <triggers/> <concurrentBuild>false</concurrentBuild> <builders/> <publishers/> <buildWrappers/> </project>
Effect