| 1 |
# Copyright 2012 Hewlett-Packard Development Company, L.P. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
|
| 16 |
"""These are job parameters that are common to every type of Jenkins job. |
| 17 |
|
| 18 |
Example: |
| 19 |
|
| 20 |
.. literalinclude:: /../../tests/yamlparser/job_fixtures/general-example-001.yaml |
| 21 |
|
| 22 |
:Job Parameters: |
| 23 |
* **project-type**: |
| 24 |
Defaults to "freestyle", but "maven" as well as "multijob", "flow", |
| 25 |
"pipeline" or "externaljob" can also be specified. |
| 26 |
|
| 27 |
* **defaults**: |
| 28 |
Specifies a set of :ref:`defaults` to use for this job, defaults to |
| 29 |
''global''. If you have values that are common to all of your jobs, |
| 30 |
create a ``global`` :ref:`defaults` object to hold them, and no further |
| 31 |
configuration of individual jobs is necessary. If some jobs |
| 32 |
should not use the ``global`` defaults, use this field to specify a |
| 33 |
different set of defaults. |
| 34 |
|
| 35 |
* **description**: |
| 36 |
The description for the job. By default, the description |
| 37 |
"!-- Managed by Jenkins Job Builder" is applied. |
| 38 |
|
| 39 |
* **disabled**: |
| 40 |
Boolean value to set whether or not this job should be disabled in |
| 41 |
Jenkins. Defaults to ``false`` (job will be enabled). |
| 42 |
|
| 43 |
* **display-name**: |
| 44 |
Optional name shown for the project throughout the Jenkins web GUI in |
| 45 |
place of the actual job name. The jenkins_jobs tool cannot fully remove |
| 46 |
this trait once it is set, so use caution when setting it. Setting it to |
| 47 |
the same string as the job's name is an effective un-set workaround. |
| 48 |
Alternately, the field can be cleared manually using the Jenkins web |
| 49 |
interface. |
| 50 |
|
| 51 |
* **concurrent**: |
| 52 |
Boolean value to set whether or not Jenkins can run this job |
| 53 |
concurrently. Defaults to ``false``. |
| 54 |
|
| 55 |
* **workspace**: |
| 56 |
Path for a custom workspace. Defaults to Jenkins default |
| 57 |
configuration. |
| 58 |
|
| 59 |
* **folder**: |
| 60 |
The folder attribute provides an alternative to using '<path>/<name>' as |
| 61 |
the job name to specify which Jenkins folder to upload the job to. |
| 62 |
|
| 63 |
Requires the Jenkins :jenkins-plugins:`CloudBees Folders Plugin |
| 64 |
<cloudbees-folder>`. |
| 65 |
|
| 66 |
* **child-workspace**: |
| 67 |
Path for a child custom workspace. Defaults to Jenkins default |
| 68 |
configuration. This parameter is only valid for matrix type jobs. |
| 69 |
|
| 70 |
* **quiet-period**: |
| 71 |
Number of seconds to wait between consecutive runs of this job. |
| 72 |
Defaults to ``0``. |
| 73 |
|
| 74 |
* **block-downstream**: |
| 75 |
Boolean value to set whether or not this job must block while |
| 76 |
downstream jobs are running. Downstream jobs are determined |
| 77 |
transitively. Defaults to ``false``. |
| 78 |
|
| 79 |
* **block-upstream**: |
| 80 |
Boolean value to set whether or not this job must block while |
| 81 |
upstream jobs are running. Upstream jobs are determined |
| 82 |
transitively. Defaults to ``false``. |
| 83 |
|
| 84 |
* **auth-token**: |
| 85 |
Specifies an authentication token that allows new builds to be |
| 86 |
triggered by accessing a special predefined URL. Only those who |
| 87 |
know the token will be able to trigger builds remotely. |
| 88 |
|
| 89 |
* **retry-count**: |
| 90 |
If a build fails to checkout from the repository, Jenkins will |
| 91 |
retry the specified number of times before giving up. |
| 92 |
|
| 93 |
* **node**: |
| 94 |
Restrict where this job can be run. If there is a group of |
| 95 |
machines that the job can be built on, you can specify that |
| 96 |
label as the node to tie on, which will cause Jenkins to build the job on |
| 97 |
any of the machines with that label. For matrix projects, this parameter |
| 98 |
will only restrict where the parent job will run. |
| 99 |
|
| 100 |
* **logrotate**: |
| 101 |
The Logrotate section allows you to automatically remove old build |
| 102 |
history. It adds the ``logrotate`` attribute to the :ref:`Job` |
| 103 |
definition. All logrotate attributes default to "-1" (keep forever). |
| 104 |
**Deprecated on jenkins >=1.637**: use the ``build-discarder`` |
| 105 |
property instead |
| 106 |
|
| 107 |
* **jdk**: |
| 108 |
The name of the jdk to use |
| 109 |
|
| 110 |
* **raw**: |
| 111 |
If present, this section should contain a single **xml** entry. This XML |
| 112 |
will be inserted at the top-level of the :ref:`Job` definition. |
| 113 |
""" |
| 114 |
|
| 115 |
import logging |
| 116 |
import xml.etree.ElementTree as XML |
| 117 |
|
| 118 |
import jenkins_jobs.modules.base |
| 119 |
from jenkins_jobs.xml_config import remove_ignorable_whitespace |
| 120 |
|
| 121 |
|
| 122 |
class General(jenkins_jobs.modules.base.Base): |
| 123 |
sequence = 10 |
| 124 |
logrotate_warn_issued = False |
| 125 |
|
| 126 |
def gen_xml(self, xml, data): |
| 127 |
jdk = data.get("jdk", None) |
| 128 |
if jdk: |
| 129 |
XML.SubElement(xml, "jdk").text = jdk |
| 130 |
XML.SubElement(xml, "actions") |
| 131 |
desc_text = data.get("description", None) |
| 132 |
if desc_text is not None: |
| 133 |
description = XML.SubElement(xml, "description") |
| 134 |
description.text = desc_text |
| 135 |
XML.SubElement(xml, "keepDependencies").text = "false" |
| 136 |
|
| 137 |
# Need to ensure we support the None parameter to allow disabled to |
| 138 |
# remain the last setting if the user purposely adds and then removes |
| 139 |
# the disabled parameter. |
| 140 |
# See: http://lists.openstack.org/pipermail/openstack-infra/2016-March/003980.html # noqa |
| 141 |
disabled = data.get("disabled", None) |
| 142 |
if disabled is not None: |
| 143 |
XML.SubElement(xml, "disabled").text = str(disabled).lower() |
| 144 |
|
| 145 |
if "display-name" in data: |
| 146 |
XML.SubElement(xml, "displayName").text = data["display-name"] |
| 147 |
if data.get("project-type", "freestyle") != "pipeline": |
| 148 |
if data.get("block-downstream"): |
| 149 |
XML.SubElement(xml, "blockBuildWhenDownstreamBuilding").text = "true" |
| 150 |
else: |
| 151 |
XML.SubElement(xml, "blockBuildWhenDownstreamBuilding").text = "false" |
| 152 |
if data.get("block-upstream"): |
| 153 |
XML.SubElement(xml, "blockBuildWhenUpstreamBuilding").text = "true" |
| 154 |
else: |
| 155 |
XML.SubElement(xml, "blockBuildWhenUpstreamBuilding").text = "false" |
| 156 |
authtoken = data.get("auth-token", None) |
| 157 |
if authtoken is not None: |
| 158 |
XML.SubElement(xml, "authToken").text = authtoken |
| 159 |
if data.get("project-type", "freestyle") != "pipeline": |
| 160 |
if data.get("concurrent"): |
| 161 |
XML.SubElement(xml, "concurrentBuild").text = "true" |
| 162 |
else: |
| 163 |
XML.SubElement(xml, "concurrentBuild").text = "false" |
| 164 |
else: |
| 165 |
if not data.get("concurrent"): |
| 166 |
properties = xml.find("properties") |
| 167 |
if properties is None: |
| 168 |
properties = XML.SubElement(xml, "properties") |
| 169 |
XML.SubElement( |
| 170 |
properties, |
| 171 |
"org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty", |
| 172 |
) |
| 173 |
if data.get("project-type", "freestyle") != "pipeline": |
| 174 |
if "workspace" in data: |
| 175 |
XML.SubElement(xml, "customWorkspace").text = str(data["workspace"]) |
| 176 |
if (xml.tag == "matrix-project") and ("child-workspace" in data): |
| 177 |
XML.SubElement(xml, "childCustomWorkspace").text = str( |
| 178 |
data["child-workspace"] |
| 179 |
) |
| 180 |
if "quiet-period" in data: |
| 181 |
XML.SubElement(xml, "quietPeriod").text = str(data["quiet-period"]) |
| 182 |
node = data.get("node", None) |
| 183 |
if data.get("project-type", "freestyle") != "pipeline": |
| 184 |
if node: |
| 185 |
XML.SubElement(xml, "assignedNode").text = node |
| 186 |
XML.SubElement(xml, "canRoam").text = "false" |
| 187 |
else: |
| 188 |
XML.SubElement(xml, "canRoam").text = "true" |
| 189 |
if "retry-count" in data: |
| 190 |
XML.SubElement(xml, "scmCheckoutRetryCount").text = str(data["retry-count"]) |
| 191 |
|
| 192 |
if "logrotate" in data: |
| 193 |
if not self.logrotate_warn_issued: |
| 194 |
logging.warning( |
| 195 |
"logrotate is deprecated on jenkins>=1.637," |
| 196 |
" the property build-discarder on newer" |
| 197 |
" jenkins instead" |
| 198 |
) |
| 199 |
self.logrotate_warn_issued = True |
| 200 |
|
| 201 |
lr_xml = XML.SubElement(xml, "logRotator") |
| 202 |
logrotate = data["logrotate"] |
| 203 |
lr_days = XML.SubElement(lr_xml, "daysToKeep") |
| 204 |
lr_days.text = str(logrotate.get("daysToKeep", -1)) |
| 205 |
lr_num = XML.SubElement(lr_xml, "numToKeep") |
| 206 |
lr_num.text = str(logrotate.get("numToKeep", -1)) |
| 207 |
lr_adays = XML.SubElement(lr_xml, "artifactDaysToKeep") |
| 208 |
lr_adays.text = str(logrotate.get("artifactDaysToKeep", -1)) |
| 209 |
lr_anum = XML.SubElement(lr_xml, "artifactNumToKeep") |
| 210 |
lr_anum.text = str(logrotate.get("artifactNumToKeep", -1)) |
| 211 |
|
| 212 |
if "raw" in data: |
| 213 |
raw(self.registry, xml, data["raw"]) |
| 214 |
|
| 215 |
|
| 216 |
def raw(registry, xml_parent, data): |
| 217 |
# documented in definition.rst since includes and docs is not working well |
| 218 |
# For cross cutting method like this |
| 219 |
root = XML.fromstring(data.get("xml")) |
| 220 |
remove_ignorable_whitespace(root) |
| 221 |
xml_parent.append(root) |