from jenkinsapi.jenkins import Jenkins
JOB_NAME = 'xxx'
SERVER = 'http://192.168.0.1:8080'
jenkins = Jenkins('http://192.168.0.1:8080', 'user', 'token')
params = {
'Branch': 'master'
}
jenkins.build_job(JOB_NAME, params)
job = jenkins[JOB_NAME]
qi = job.invoke(build_params=params)
if qi.is_queued() or qi.is_running():
print('等待任务构建完成...')
qi.block_until_complete()
build = qi.get_build()
ifnot build.is_good():
raise RuntimeError(f'Build failed, check {server}/job/{JOB_NAME}/{build.buildno}/pipeline-graph/ for more details.')
python
工具类
压缩文件夹
import zipfile
import os
import sys
defzip_dir(directory_path: str, output_path=None):
# Get the base name of the directory to include in the zip
base_name = os.path.basename(directory_path)
# Create a zip file at the specified output pathwith zipfile.ZipFile(output_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
# Walk through the directory and add each file or directory to the zip filefor root, dirs, files in os.walk(directory_path):
# Create an archive name with the top-level directory included
arcname_root = os.path.join(base_name, os.path.relpath(root, start=directory_path))
# Add directory entriesifnot files andnot dirs:
# Handle the case of empty directories
zipf.write(root, arcname=arcname_root + '/')
for file in files:
# Get the full path of the file
full_path = os.path.join(root, file)
# Create the relative path for the file in the zip
arcname = os.path.join(arcname_root, file)
zipf.write(full_path, arcname)
python
Y/N 输入确认
definput_bool(text: str, default: bool = False):
tip: str
exp: strif default:
tip = '(Y/n)'else:
tip = '(y/N)'
out = input(f'{text}{tip}')
if out == '':
return default
elif out == 'Y'or out == 'y':
returnTrueelif out == 'N'or out == 'n':
returnFalseelse:
return input_bool(text, default)