System

Connect to the application

jenkins.Jenkins.connect()

Test the connection to the Jenkins instance.

Returns:

Object containing connection information.

Return type:

jenkins_pysdk.objects.JenkinsConnectObject

Raises:
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.connect())

The above code will output:

request=<Request object at 1718212996176> content='[200] Successfully connected to JenkinsDNS.' status_code=200

Get the Jenkins version

jenkins.Jenkins.version()

Get the version information of the Jenkins instance.

Returns:

Version information of the Jenkins instance

Return type:

str

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.version)

The above code will output:

2.448

Restart the application

jenkins.Jenkins.restart()

Restart the Jenkins instance.

Parameters:

graceful (bool) – (optional) If True, restart after all jobs have finished, defaults to False

Returns:

Restart status

Return type:

jenkins_pysdk.objects.JenkinsActionObject

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.restart(graceful=True))

The above code will output:

request=<Request object at 2603665289872> content='[200] Restarting the Jenkins instance... please wait...' status_code=200

Enable Quiet Mode

jenkins.Jenkins.quiet_mode()

Enable or disable Quiet Mode on the Jenkins instance.

Parameters:
  • duration (int) – (optional) Enable Quiet Mode for X seconds

  • disable (bool) – (optional) If True, disable Quiet Mode, defaults to False

Returns:

Result of the request to enable or disable Quiet Mode

Return type:

jenkins_pysdk.objects.JenkinsActionObject

Raises:

JenkinsGeneralException – If a general exception occurs.

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.quiet_mode(duration=30))

The above code will output:

request=<Request object at 1938732315280> content='[200] Successfully enabled Quiet Mode for 30 seconds.' status_code=200

Shutdown the application

jenkins.Jenkins.shutdown()

Terminate the user’s session.

Parameters:

graceful (bool) – (Default: False) If True, pause new jobs and wait for all jobs to complete.

Returns:

Result of the shutdown request

Return type:

jenkins_pysdk.objects.JenkinsActionObject

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.shutdown())

The above code will output:

request=<Request object at 2613641997152> content='[200] Shutting down...' status_code=200

Logout

jenkins.Jenkins.logout()

Terminate the user’s session.

Parameters:

boot (bool) – (Default: False) If True, terminate all the users’ sessions. (Caution if it’s a service account!)

Returns:

Result of the logout request

Return type:

jenkins_pysdk.objects.JenkinsActionObject

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.logout())

The above code will output:

request=<Request object at 2453417290592> content='[200] Successfully logged out.' status_code=200

Reload config from disk

jenkins.Jenkins.reload()

Reload configuration from disk.

Returns:

Result of request

Return type:

jenkins_pysdk.objects.JenkinsActionObject

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.reload())

The above code will output:

request=<Request object at 2741687462992> content='[200] Successfully reloaded configuration.' status_code=200

Get the available executors

jenkins.Jenkins.available_executors()

View the number of available executors on the instance.

Returns:

Number of available executors

Return type:

int

Raises:

JenkinsGeneralException – If a general exception occurs.

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.available_executors)

The above code will output:

No executors are available.

Execute commands in the script console

jenkins.Jenkins.script_console()
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS",
                  username="admin", token="11e8e294cee85ee88b60d99328284d7608")
import pathlib
file = pathlib.Path("C:\\Users\\UnknownUser\\Desktop\\commands.groovy")
commands = file.read_text()
print(f"Running commands:\n{commands}")

print(jenkins.script_console(commands))

The above code will output:

Running commands:
println("Hello from Jenkins Script Console!")

Hello from Jenkins Script Console!