Credentials

Credentials

Search for a system domain

credentials.Credentials.search_domains()

Search for domains on the Jenkins instance.

Parameters:

domain (str, optional) – The name of the domain to search for. If None, returns all domains.

Returns:

The Domain object representing the found domain.

Return type:

jenkins_pysdk.credentials.Domain

Raises:

JenkinsGeneralException – If a general exception occurs.

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

The above code will output:

<jenkins_pysdk.credentials.Domain object at 0x0000023E2488CCD0>

Iterate all domains

credentials.Credentials.iter_domains()

Iterate over domains on the Jenkins instance.

Returns:

A generator yielding Domain objects.

Return type:

Generator[jenkins_pysdk.credentials.Domain]

Raises:

JenkinsGeneralException – If a general exception occurs.

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
for domain in jenkins.credentials.iter_domains():
    print(domain.name)

The above code will output:

Global credentials (unrestricted)
domain2
test_domain

Iterate all domains

credentials.Credentials.list_domains()

List all domains on the Jenkins instance.

Returns:

List of Domain objects.

Return type:

List[jenkins_pysdk.credentials.Domain]

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print([domain.name for domain in jenkins.credentials.list_domains()])

The above code will output:

['Global credentials (unrestricted)', 'domain2', 'test_domain']

Create a domain

credentials.Credentials.create_domain()

Create a new domain.

Parameters:
  • name (str) – The name of the domain

  • cred (Builder.Credentials.Domain) – The credentials to associate with the domain.

Returns:

Outcome of the domain creation request.

Return type:

jenkins_pysdk.objects.JenkinsActionObject

Raises:
jenkins = Jenkins(host="https://JenkinsDNS", username="admin", passw="11e8e294cee85ee88b60d99328284d7608")
from jenkins_pysdk.builders import Builder
new_user = Builder.Credentials.Domain(name="global2", description="new global domain")
print(jenkins.credentials.create_domain("global2", new_user))

The above code will output:

request=<HTTPSessionRequestObject object at 1593952704992> content='[200] Successfully created domain (global2).' status_code=200

Domain

Get the domain name

credentials.Domain.name()

The name of the domain.

Returns:

The name of the domain.

Return type:

str

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
domain = jenkins.credentials.search_domains()
print(domain.name)

The above code will output:

Global credentials (unrestricted)

Get the domain URL

credentials.Domain.url()

The domain URL.

Returns:

The domain URL in str format.

Return type:

str

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
domain = jenkins.credentials.search_domains()
print(domain.url)

The above code will output:

https://JenkinsDNS/manage/credentials/store/system/domain/_

Search for a credential in the domain

credentials.Domain.search()

Search for a credential within the domain.

Parameters:

cred_id (str) – The ID of the credential to search for.

Returns:

The credential matching the provided ID.

Return type:

jenkins_pysdk.credentials.Credential

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
github_pat = jenkins.credentials.search_domains("domain2").search("Github_PAT")
   print(github_pat)

The above code will output:

<jenkins_pysdk.credentials.Credential object at 0x000002B2AD79BD60>

Iterate credentials in the domain

credentials.Domain.iter()

Iterate over credentials within the domain.

Returns:

A generator yielding credentials within the specified domain.

Return type:

Generator[jenkins_pysdk.credentials.Credential]

Raises:

JenkinsGeneralException – If a general exception occurs.

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
domain = jenkins.credentials.search_domains()  # Defaults to Global domain
for cred in domain.iter():
    print(cred.id)

The above code will output:

3f2ba384-a1bc-4785-86d0-1c82d4e8be03
95b47fec-c078-4821-b05b-c7149f549429

List credentials in the domain

credentials.Domain.list()

List credentials within the domain.

Returns:

A list of credentials within the specified domain.

Return type:

List[jenkins_pysdk.credentials.Credential]

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
domain = jenkins.credentials.search_domains()  # Defaults to Global domain
print([cred.id for cred in domain.list()])

The above code will output:

['3f2ba384-a1bc-4785-86d0-1c82d4e8be03', '95b47fec-c078-4821-b05b-c7149f549429']

Create a credential in the domain

credentials.Domain.create()

Create a new credential.

Parameters:
Returns:

Result of the deletion operation.

Return type:

jenkins_pysdk.objects.JenkinsActionObject

jenkins = Jenkins(host="https://JenkinsDNS", username="admin", passw="11e8e294cee85ee88b60d99328284d7608")
from jenkins_pysdk.builders import Builder

new_cred = Builder.Credentials.UsernamePassword(cred_id="gitlab_login", username="new_username", password="new_pasw")
print(new_cred)
print(jenkins.credentials.search_domains("global2").create("new_cred", new_cred))

The above code will output:

<com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl>
    <scope>GLOBAL</scope>
    <id>gitlab_login</id>
    <username>new_username</username>
    <password>new_pasw</password>
</com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl>
request=<HTTPSessionRequestObject object at 2307040044480> content='[200] Successfully created credential (new_cred).' status_code=200

Credential

Get the credential ID

credentials.Credential.id()

Get the ID of the credential.

Returns:

The ID of the credential.

Return type:

str

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
github_pat = jenkins.credentials.search_domains("domain2").search("Github_PAT")
print(github_pat.id)

The above code will output:

Github_PAT

Get the credential config

credentials.Credential.config()

Get the configuration of the credential.

Returns:

The configuration of the credential.

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")
github_pat = jenkins.credentials.search_domains("domain2").search("Github_PAT")
print(github_pat.config)

The above code will output:

<org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl plugin="plain-credentials@179.vc5cb_98f6db_38">
  <scope>GLOBAL</scope>
  <id>Github_PAT</id>
  <description></description>
  <secret>
    <secret-redacted/>
  </secret>
</org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl>

Reconfigure the credential

credentials.Credential.reconfig()

Reconfigure the credential with new XML content or using a Credentials builder.

Parameters:

xml (str or Builder.Credentials) – The new XML content or a Credentials builder.

Returns:

Result of the reconfiguration operation.

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")
github_pat = jenkins.credentials.search_domains("domain2").search("Github_PAT")
new_password = """<org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl plugin="plain-credentials@179.vc5cb_98f6db_38">
                    <scope>GLOBAL</scope>
                    <id>Github_PAT</id>
                    <description/>
                   <secret>
                    new_password
                    </secret>
                </org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl>"""

The above code will output:

request=<Request object at 2336431155744> content='[200] Successfully reconfigured Github_PAT.' status_code=200

Move a credential to another domain (You can’t move from System to User/Local)

credentials.Credential.move()

Move the credential to another domain.

Returns:

Result of the move operation.

Return type:

jenkins_pysdk.objects.JenkinsActionObject

jenkins = Jenkins(host="https://JenkinsDNS", username="admin",
                  passw="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.credentials.search_domains("global2").search("gitlab_login").move("system/test_domain"))

The above code will output:

request=<HTTPSessionRequestObject object at 1999299504192> content='[200] Successfully moved gitlab_login to system/test_domain.' status_code=200

Delete the credential

credentials.Credential.delete()

Delete the credential.

Returns:

Result of the deletion operation.

Return type:

jenkins_pysdk.objects.JenkinsActionObject

jenkins = Jenkins(host="https://JenkinsDNS", username="admin",
                  passw="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.credentials.search_domains("test_domain").search("gitlab_login").delete())

The above code will output:

request=<Request object at 2398624629840> content='[200] Failed to delete credential.' status_code=200