diff --git a/doc/source/execution.rst b/doc/source/execution.rst index 7434be72b..eb292e116 100644 --- a/doc/source/execution.rst +++ b/doc/source/execution.rst @@ -66,6 +66,11 @@ job_builder section they will be accessible in `qux.yml` but not in `bar.yml`. They will also be accessible in `mydir/bar.yml` and `mydir/qux.yml`. False by default. +**update** + (Optional) If set, allows the user to specify if only "jobs" or "views" + (or "all") are updated. Users can override the setting here by passing + ``--jobs-only`` or ``--views-only`` CLI options. + (Valid options: jobs, views, all) jenkins section ^^^^^^^^^^^^^^^ diff --git a/jenkins_jobs/cli/entry.py b/jenkins_jobs/cli/entry.py index 888c21c13..30373b490 100644 --- a/jenkins_jobs/cli/entry.py +++ b/jenkins_jobs/cli/entry.py @@ -86,11 +86,17 @@ class JenkinsJobs(object): self._set_config(self.jjb_config.builder, 'ignore_cache') self._set_config(self.jjb_config.builder, 'flush_cache') + self._set_config(self.jjb_config.builder, 'update') self._set_config(self.jjb_config.yamlparser, 'allow_empty_variables') self._set_config(self.jjb_config.jenkins, 'section') self._set_config(self.jjb_config.jenkins, 'user') self._set_config(self.jjb_config.jenkins, 'password') + # Note: CLI options override config file options. + if getattr(self.options, 'update', None) is None: + self.options.update = str(self.jjb_config.builder.get('update', + 'all')) + if getattr(self.options, 'plugins_info_path', None) is not None: with io.open(self.options.plugins_info_path, 'r', encoding='utf-8') as yaml_file: diff --git a/jenkins_jobs/cli/subcommand/update.py b/jenkins_jobs/cli/subcommand/update.py index 18ce70bfc..5884648e6 100644 --- a/jenkins_jobs/cli/subcommand/update.py +++ b/jenkins_jobs/cli/subcommand/update.py @@ -72,25 +72,26 @@ class UpdateSubCommand(base.BaseSubCommand): dest='n_workers', help="number of workers to use, 0 for autodetection and 1 " "for just one worker.") - update.add_argument( - '-j', '--jobs-only', - action='store_true', dest='add_jobs', - default=False, - help='update only jobs' - ) - update.add_argument( - '-v', '--views-only', - action='store_true', dest='add_views', - default=False, - help='update only views' - ) update.add_argument( '--existing-only', action='store_true', default=False, dest='existing_only', - help='update existing jobs only' - ) + help='update existing jobs only') + + update_type = update.add_mutually_exclusive_group() + update_type.add_argument( + '-j', '--jobs-only', + action='store_const', + dest='update', + const='jobs', + help='update only jobs') + update_type.add_argument( + '-v', '--views-only', + action='store_const', + dest='update', + const='views', + help='update only views') def _generate_xmljobs(self, options, jjb_config=None): builder = JenkinsManager(jjb_config) @@ -122,11 +123,6 @@ class UpdateSubCommand(base.BaseSubCommand): return builder, xml_jobs, xml_views def execute(self, options, jjb_config): - - if options.add_jobs and options.add_views: - raise JenkinsJobsException( - '"--views-only" and "--jobs-only" cannot be used together.') - if options.n_workers < 0: raise JenkinsJobsException( 'Number of workers must be equal or greater than 0') @@ -134,12 +130,12 @@ class UpdateSubCommand(base.BaseSubCommand): builder, xml_jobs, xml_views = self._generate_xmljobs( options, jjb_config) - if options.add_jobs: + if options.update == 'jobs': jobs, num_updated_jobs = builder.update_jobs( xml_jobs, n_workers=options.n_workers, existing_only=options.existing_only) logger.info("Number of jobs updated: %d", num_updated_jobs) - elif options.add_views: + elif options.update == 'views': views, num_updated_views = builder.update_views( xml_views, n_workers=options.n_workers, existing_only=options.existing_only) diff --git a/jenkins_jobs/config.py b/jenkins_jobs/config.py index 535a19105..031bb922d 100644 --- a/jenkins_jobs/config.py +++ b/jenkins_jobs/config.py @@ -313,6 +313,12 @@ class JJBConfig(object): 'retain_anchors') self.yamlparser['retain_anchors'] = retain_anchors + update = None + if (config and config.has_section('job_builder') and + config.has_option('job_builder', 'update')): + update = config.get('job_builder', 'update') + self.builder['update'] = update + def validate(self): # Inform the user as to what is likely to happen, as they may specify # a real jenkins instance in test mode to get the plugin info to check diff --git a/tests/cmd/fixtures/settings_from_config.ini b/tests/cmd/fixtures/settings_from_config.ini index 2001a81a7..2b6a96b1d 100644 --- a/tests/cmd/fixtures/settings_from_config.ini +++ b/tests/cmd/fixtures/settings_from_config.ini @@ -6,3 +6,4 @@ password=jenkins_password allow_empty_variables=True ignore_cache=True flush_cache=True +update=all diff --git a/tests/cmd/test_config.py b/tests/cmd/test_config.py index ef399b604..8b2eee7d8 100644 --- a/tests/cmd/test_config.py +++ b/tests/cmd/test_config.py @@ -118,6 +118,7 @@ class TestConfigs(CmdTestsBase): self.assertEqual(jjb_config.jenkins['password'], "jenkins_password") self.assertEqual(jjb_config.builder['ignore_cache'], True) self.assertEqual(jjb_config.builder['flush_cache'], True) + self.assertEqual(jjb_config.builder['update'], "all") self.assertEqual( jjb_config.yamlparser['allow_empty_variables'], True)