-
Notifications
You must be signed in to change notification settings - Fork 16.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[community] Add Jenkins tool support.
This PR are used to support Jenkins API to cover below feature, * Create Jenkin Job. * Delete Jenkin Job. * Run the Jenkin job. * Get Job Status.
- Loading branch information
Showing
5 changed files
with
398 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Jenkins\n", | ||
"\n", | ||
"This notebook, go over how to use Jenkins. \n", | ||
"First make sure that you have installed python-jenkins with the command below: " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"vscode": { | ||
"languageId": "shellscript" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"%pip install --upgrade --quiet python-jenkins" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Before start using Jenkins, first setup or get authorization to access Jenkins server." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"vscode": { | ||
"languageId": "shellscript" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import getpass\n", | ||
"import os\n", | ||
"\n", | ||
"\n", | ||
"def _set_env(var: str):\n", | ||
" if not os.environ.get(var):\n", | ||
" os.environ[var] = getpass.getpass(f\"{var}: \")\n", | ||
"\n", | ||
"_set_env(\"PASSWORD\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"To disable the SSL Verify, set `os.environ[\"PYTHONHTTPSVERIFY\"] = \"0\"`" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from langchain_community.tools.jenkins.tool import JenkinsJobRun\n", | ||
"from langchain_community.utilities.jenkins import JenkinsAPIWrapper\n", | ||
"\n", | ||
"\n", | ||
"tools = [JenkinsJobRun(\n", | ||
" api_wrapper=JenkinsAPIWrapper(\n", | ||
" jenkins_server=\"https://example.com\",\n", | ||
" username=\"admin\",\n", | ||
" password=os.environ[\"PASSWORD\"]\n", | ||
" )\n", | ||
")]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"You can now call invoke and pass arguments." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"1. Create the Jenkins job" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"jenkins_job_content = \"\"\n", | ||
"src_file = \"job1.xml\"\n", | ||
"with open(src_file) as fread:\n", | ||
" jenkins_job_content = fread.read()\n", | ||
"tools[0].invoke({'job': \"job01\", \"config_xml\": jenkins_job_content, \"action\": \"create\"})" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"2. Run the Jenkins Job" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"tools[0].invoke({'job': \"job01\", \"parameters\": {}, \"action\": \"run\"})" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"3. Get job info" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"resp = tools[0].invoke({'job': \"job01\", \"number\": 1, \"action\": \"status\"})\n", | ||
"if not resp[\"inProgress\"]:\n", | ||
" print(resp[\"result\"])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"4. Delete the jenkins job" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"tools[0].invoke({'job': \"job01\", \"action\": \"delete\"})" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.1" | ||
}, | ||
"vscode": { | ||
"interpreter": { | ||
"hash": "3929050b09828356c9f5ebaf862d05c053d8228eddbc70f990c168e54dd824ba" | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"""Jenkins Tool.""" | ||
|
||
from langchain_community.tools.jenkins.tool import JenkinsJobRun | ||
|
||
__all__ = ["JenkinsJobRun"] |
114 changes: 114 additions & 0 deletions
114
libs/community/langchain_community/tools/jenkins/tool.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
"""Tool for the Jenkins API""" | ||
|
||
from typing import Optional, Type | ||
|
||
from langchain_core.callbacks import CallbackManagerForToolRun | ||
from langchain_core.tools import BaseTool | ||
from pydantic import BaseModel, Field | ||
|
||
from langchain_community.utilities.jenkins import JenkinsAPIWrapper | ||
|
||
class JenkinsSchema(BaseModel): | ||
Check failure on line 11 in libs/community/langchain_community/tools/jenkins/tool.py GitHub Actions / cd libs/community / make lint #3.13Ruff (I001)
|
||
"""Input for the Jenkins tool. | ||
Instantiate: | ||
.. code-block:: python | ||
from tools.jenkins.tool import JenkinsJobRun | ||
from tools.jenkins.utility import JenkinsAPIWrapper | ||
tools = [JenkinsJobRun( | ||
api_wrapper=JenkinsAPIWrapper( | ||
jenkins_server="https://jenkinsserver.com", | ||
username="admin", | ||
password=os.environ["PASSWORD"] | ||
) | ||
)] | ||
Invoke directly with args: | ||
.. code-block:: python | ||
# delete jenkins job | ||
tools[0].invoke({'job': "job01", "action": "delete"}) | ||
# create jenkins job | ||
jenkins_job_content = "" | ||
src_file = "job1.xml" | ||
with open(src_file) as fread: | ||
jenkins_job_content = fread.read() | ||
tools[0].invoke({'job': "job01", "config_xml": jenkins_job_content, "action": "create"}) | ||
Check failure on line 42 in libs/community/langchain_community/tools/jenkins/tool.py GitHub Actions / cd libs/community / make lint #3.13Ruff (E501)
|
||
# run the jenkins job | ||
tools[0].invoke({'job': "job01", "parameters": {}, "action": "run"}) | ||
# get jenkins job info by passing job number | ||
resp = tools[0].invoke({'job': "job01", "number": 1, "action": "status"}) | ||
Check failure on line 48 in libs/community/langchain_community/tools/jenkins/tool.py GitHub Actions / cd libs/community / make lint #3.13Ruff (E501)
|
||
if not resp["inProgress"]: | ||
print(resp["result"]) | ||
""" | ||
|
||
job: str = Field( | ||
description="name of the job" | ||
) | ||
action: str = Field( | ||
description="action of the job, like, create, run, delete" | ||
) | ||
number: int = Field( | ||
default=1, | ||
description="job number" | ||
) | ||
config_xml: str = Field( | ||
default='', | ||
description="job xml content" | ||
) | ||
parameters: dict = Field( | ||
default={}, | ||
description="job parameters" | ||
) | ||
|
||
|
||
class JenkinsJobRun(BaseTool): # type: ignore[override, override] | ||
"""Tool that execute the job""" | ||
name: str = "jenkins" | ||
description: str = ( | ||
"A tool that is used to create, trigger and delete Jenkins jobs with specified parameters." | ||
Check failure on line 78 in libs/community/langchain_community/tools/jenkins/tool.py GitHub Actions / cd libs/community / make lint #3.13Ruff (E501)
|
||
) | ||
api_wrapper: JenkinsAPIWrapper = Field(default_factory=JenkinsAPIWrapper) # type: ignore[arg-type] | ||
args_schema: Type[BaseModel] = JenkinsSchema | ||
|
||
|
||
def _run( | ||
self, | ||
job: str, | ||
action: str, | ||
number: Optional[int] = 1, | ||
config_xml: Optional[str] = "", | ||
parameters: Optional[dict] = {}, | ||
run_manager: Optional[CallbackManagerForToolRun] = None, | ||
) -> any: | ||
"""Use the tool.""" | ||
if action == "create": | ||
self.api_wrapper.create_job( | ||
job=job, | ||
config_xml=config_xml, | ||
) | ||
elif action == "run": | ||
return self.api_wrapper.run_job( | ||
job=job, | ||
parameters=parameters, | ||
) | ||
elif action == "delete": | ||
self.api_wrapper.delete_job( | ||
job=job, | ||
) | ||
elif action == "status": | ||
return self.api_wrapper.status_job( | ||
job=job, | ||
number=number | ||
) | ||
else: | ||
raise ValueError("'action' not matched") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.