Merge "Added config options to not overwrite jobs desc"

This commit is contained in:
Jenkins 2014-04-21 18:12:34 +00:00 committed by Gerrit Code Review
commit 90af7a8fc6
12 changed files with 147 additions and 22 deletions

View File

@ -46,6 +46,21 @@ the following format:
.. literalinclude:: ../../etc/jenkins_jobs.ini-sample .. literalinclude:: ../../etc/jenkins_jobs.ini-sample
:language: ini :language: ini
job_builder section
^^^^^^^^^^^^^^^^^^^
**ignore_cache**
(Optional) If set to True, Jenkins Job Builder won't use any cache.
**keep_descriptions**
By default `jenkins-jobs` will overwrite the jobs descriptions even if no
description has been defined explicitly.
When this option is set to True, that behavior changes and it will only
overwrite the description if you specified it in the yaml. False by default.
jenkins section
^^^^^^^^^^^^^^^
**user** **user**
This should be the name of a user previously defined in Jenkins. This should be the name of a user previously defined in Jenkins.
Appropriate user permissions must be set under the Jenkins security Appropriate user permissions must be set under the Jenkins security
@ -61,9 +76,6 @@ the following format:
**url** **url**
The base URL for your Jenkins installation. The base URL for your Jenkins installation.
**ignore_cache**
(Optional) If set to True, Jenkins Job Builder won't use any cache.
Running Running
------- -------

View File

@ -1,5 +1,10 @@
[job_builder]
ignore_cache=True
keep_descriptions=False
[jenkins] [jenkins]
user=jenkins user=jenkins
password=1234567890abcdef1234567890abcdef password=1234567890abcdef1234567890abcdef
url=https://jenkins.example.com url=https://jenkins.example.com
ignore_cache=True ##### This is deprecated, use job_builder section instead
#ignore_cache=True

View File

@ -117,9 +117,10 @@ def matches(what, where):
class YamlParser(object): class YamlParser(object):
def __init__(self, config=None): def __init__(self, config=None):
self.registry = ModuleRegistry(config)
self.data = {} self.data = {}
self.jobs = [] self.jobs = []
self.config = config
self.registry = ModuleRegistry(self.config)
def parse_fp(self, fp): def parse_fp(self, fp):
data = yaml.load(fp) data = yaml.load(fp)
@ -288,8 +289,18 @@ class YamlParser(object):
def getXMLForJob(self, data): def getXMLForJob(self, data):
kind = data.get('project-type', 'freestyle') kind = data.get('project-type', 'freestyle')
data["description"] = (data.get("description", "") + if self.config:
self.get_managed_string()).lstrip() keep_desc = self.config.getboolean('job_builder',
'keep_descriptions')
else:
keep_desc = False
if keep_desc:
description = data.get("description", None)
else:
description = data.get("description", '')
if description is not None:
data["description"] = description + \
self.get_managed_string().lstrip()
for ep in pkg_resources.iter_entry_points( for ep in pkg_resources.iter_entry_points(
group='jenkins_jobs.projects', name=kind): group='jenkins_jobs.projects', name=kind):
Mod = ep.load() Mod = ep.load()

View File

@ -19,6 +19,19 @@ import logging
import os import os
import platform import platform
import sys import sys
import cStringIO
DEFAULT_CONF = """
[job_builder]
keep_descriptions=False
ignore_cache=False
[jenkins]
url=http://localhost:8080/
user=
password=
"""
def confirm(question): def confirm(question):
@ -80,20 +93,15 @@ def main():
'jenkins_jobs.ini') 'jenkins_jobs.ini')
if os.path.isfile(localconf): if os.path.isfile(localconf):
conf = localconf conf = localconf
config = ConfigParser.ConfigParser() config = ConfigParser.ConfigParser()
if os.path.isfile(conf): ## Load default config always
config.readfp(cStringIO.StringIO(DEFAULT_CONF))
if options.command == 'test':
logger.debug("Not reading config for test output generation")
elif os.path.isfile(conf):
logger.debug("Reading config from {0}".format(conf)) logger.debug("Reading config from {0}".format(conf))
conffp = open(conf, 'r') conffp = open(conf, 'r')
config.readfp(conffp) config.readfp(conffp)
elif options.command == 'test':
## to avoid the 'no section' and 'no option' errors when testing
config.add_section("jenkins")
config.set("jenkins", "url", "http://localhost:8080")
config.set("jenkins", "user", None)
config.set("jenkins", "password", None)
config.set("jenkins", "ignore_cache", False)
logger.debug("Not reading config for test output generation")
else: else:
raise jenkins_jobs.errors.JenkinsJobsException( raise jenkins_jobs.errors.JenkinsJobsException(
"A valid configuration file is required when not run as a test" "A valid configuration file is required when not run as a test"
@ -107,7 +115,12 @@ def main():
if options.ignore_cache: if options.ignore_cache:
ignore_cache = options.ignore_cache ignore_cache = options.ignore_cache
elif config.has_option('jenkins', 'ignore_cache'): elif config.has_option('jenkins', 'ignore_cache'):
logging.warn('ignore_cache option should be moved to the [job_builder]'
' section in the config file, the one specified in the '
'[jenkins] section will be ignored in the future')
ignore_cache = config.get('jenkins', 'ignore_cache') ignore_cache = config.get('jenkins', 'ignore_cache')
elif config.has_option('job_builder', 'ignore_cache'):
ignore_cache = config.get('job_builder', 'ignore_cache')
# workaround for python 2.6 interpolation error # workaround for python 2.6 interpolation error
# https://bugs.launchpad.net/openstack-ci/+bug/1259631 # https://bugs.launchpad.net/openstack-ci/+bug/1259631

View File

@ -48,9 +48,15 @@ def get_scenarios(fixtures_path):
raise Exception( raise Exception(
"No XML file named '%s' to match " "No XML file named '%s' to match "
"YAML file '%s'" % (xml_candidate, yaml_filename)) "YAML file '%s'" % (xml_candidate, yaml_filename))
conf_candidate = re.sub(r'\.yaml$', '.conf', yaml_filename)
# If present, add the configuration file
if conf_candidate not in files:
conf_candidate = None
scenarios.append((yaml_filename, { scenarios.append((yaml_filename, {
'yaml_filename': yaml_filename, 'xml_filename': xml_candidate 'yaml_filename': yaml_filename,
'xml_filename': xml_candidate,
'conf_filename': conf_candidate,
})) }))
return scenarios return scenarios

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<actions/>
<description>My nice description&lt;!-- Managed by Jenkins Job Builder --&gt;</description>
<keepDependencies>false</keepDependencies>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<concurrentBuild>false</concurrentBuild>
<canRoam>true</canRoam>
<properties/>
<scm class="hudson.scm.NullSCM"/>
<builders>
<hudson.tasks.Shell>
<command>#!/usr/bin/env python
#
print(&quot;Doing something cool with python&quot;)
</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
</project>

View File

@ -0,0 +1,13 @@
- project:
name: add_managed_to_desc
jobs:
- 'add_managed_to_desc'
- job-template:
name: 'add_managed_to_desc'
description: 'My nice description'
builders:
- shell: |
#!/usr/bin/env python
#
print("Doing something cool with python")

View File

@ -66,14 +66,14 @@
choosing-strategy: gerrit choosing-strategy: gerrit
- project: - project:
name: test name: complete001
version: version:
- 1.2 - 1.2
jobs: jobs:
- 'build_myproject_{version}' - 'complete001_{version}'
- job-template: - job-template:
name: 'build_myproject_{version}' name: 'complete001_{version}'
scm: scm:
- gerrit-scm: - gerrit-scm:
project_pattern: openstack-infra/jenkins-job-builder project_pattern: openstack-infra/jenkins-job-builder

View File

@ -0,0 +1,2 @@
[job_builder]
keep_descriptions = True

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<actions/>
<keepDependencies>false</keepDependencies>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<concurrentBuild>false</concurrentBuild>
<canRoam>true</canRoam>
<properties/>
<scm class="hudson.scm.NullSCM"/>
<builders>
<hudson.tasks.Shell>
<command>#!/usr/bin/env python
#
print(&quot;Doing something cool with python&quot;)
</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
</project>

View File

@ -0,0 +1,12 @@
- project:
name: dont_overwrite_desc
jobs:
- 'dont_overwrite_desc'
- job-template:
name: 'dont_overwrite_desc'
builders:
- shell: |
#!/usr/bin/env python
#
print("Doing something cool with python")

View File

@ -16,6 +16,7 @@
# under the License. # under the License.
import os import os
from ConfigParser import ConfigParser
from testtools import TestCase from testtools import TestCase
from testscenarios.testcase import TestWithScenarios from testscenarios.testcase import TestWithScenarios
from tests.base import get_scenarios, BaseTestCase from tests.base import get_scenarios, BaseTestCase
@ -37,7 +38,14 @@ class TestCaseModuleYamlInclude(TestWithScenarios, TestCase, BaseTestCase):
yaml_filepath = os.path.join(self.fixtures_path, self.yaml_filename) yaml_filepath = os.path.join(self.fixtures_path, self.yaml_filename)
parser = YamlParser() if self.conf_filename:
config = ConfigParser()
conf_filepath = os.path.join(self.fixtures_path,
self.conf_filename)
config.readfp(open(conf_filepath))
else:
config = None
parser = YamlParser(config)
parser.parse(yaml_filepath) parser.parse(yaml_filepath)
# Generate the XML tree # Generate the XML tree