93
93
- A list of URL segment(s) to retrieve the update center JSON file from.
94
94
default: ['update-center.json', 'updates/update-center.json']
95
95
version_added: 3.3.0
96
+ plugin_versions_url_segment:
97
+ type: list
98
+ elements: str
99
+ description:
100
+ - A list of URL segment(s) to retrieve the plugin versions JSON file from.
101
+ default: ['plugin-versions.json', 'current/plugin-versions.json']
102
+ version_added: 11.1.0
96
103
latest_plugins_url_segments:
97
104
type: list
98
105
elements: str
137
144
- Pinning works only if the plugin is installed and Jenkins service was successfully restarted after the plugin installation.
138
145
- It is not possible to run the module remotely by changing the O(url) parameter to point to the Jenkins server. The module
139
146
must be used on the host where Jenkins runs as it needs direct access to the plugin files.
147
+ - If using a custom O(updates_url), ensure that the URL provides a C(plugin-versions.json) file.
148
+ This file must include metadata for all available plugin versions to support version compatibility resolution.
149
+ The file should be in the same format as the one provided by Jenkins update center (https://updates.jenkins.io/current/plugin-versions.json).
140
150
extends_documentation_fragment:
141
151
- ansible.builtin.url
142
152
- ansible.builtin.files
@@ -688,6 +698,10 @@ def _get_latest_compatible_plugin_version(self, plugin_name=None):
688
698
self .jenkins_version = self .parse_version (raw_version )
689
699
name = plugin_name or self .params ['name' ]
690
700
cache_path = "{}/ansible_jenkins_plugin_cache.json" .format (self .params ['jenkins_home' ])
701
+ plugin_version_urls = []
702
+ for base_url in self .params ['updates_url' ]:
703
+ for update_json in self .params ['plugin_versions_url_segment' ]:
704
+ plugin_version_urls .append ("{}/{}" .format (base_url , update_json ))
691
705
692
706
try : # Check if file is saved localy
693
707
if os .path .exists (cache_path ):
@@ -697,10 +711,19 @@ def _get_latest_compatible_plugin_version(self, plugin_name=None):
697
711
698
712
now = time .time ()
699
713
if now - file_mtime >= 86400 :
700
- response , info = fetch_url (self .module , "https://updates.jenkins.io/current/plugin-versions.json" )
701
- if info ['status' ] != 200 :
702
- self .module .fail_json (msg = "Failed to fetch plugin-versions.json" , details = info )
703
- plugin_data = json .loads (to_native (response .read ()), object_pairs_hook = OrderedDict )
714
+ try :
715
+ response = self ._get_urls_data (plugin_version_urls , what = "plugin-versions.json" )
716
+ plugin_data = json .loads (to_native (response .read ()), object_pairs_hook = OrderedDict )
717
+ if "plugins" not in plugin_data :
718
+ raise ValueError ("Missing 'plugins' key" )
719
+ except Exception as e :
720
+ self .module .warn (
721
+ "Could not fetch plugin-versions.json from custom updates_url(s). Falling back to updates.jenkins.io. Details: {}" .format (to_native (e ))
722
+ )
723
+ response , info = fetch_url (self .module , "https://updates.jenkins.io/current/plugin-versions.json" )
724
+ if info ['status' ] != 200 :
725
+ self .module .fail_json (msg = "Failed to fetch fallback plugin-versions.json" , details = info )
726
+ plugin_data = json .loads (to_native (response .read ()), object_pairs_hook = OrderedDict )
704
727
705
728
# Save it to file for next time
706
729
with open (cache_path , "w" ) as f :
@@ -710,6 +733,8 @@ def _get_latest_compatible_plugin_version(self, plugin_name=None):
710
733
plugin_data = json .load (f )
711
734
712
735
except Exception as e :
736
+ if os .path .exists (cache_path ):
737
+ os .remove (cache_path )
713
738
self .module .fail_json (msg = "Failed to parse plugin-versions.json" , details = to_native (e ))
714
739
715
740
plugin_versions = plugin_data .get ("plugins" , {}).get (name )
@@ -939,6 +964,8 @@ def main():
939
964
updates_url_password = dict (type = "str" , no_log = True ),
940
965
update_json_url_segment = dict (type = "list" , elements = "str" , default = ['update-center.json' ,
941
966
'updates/update-center.json' ]),
967
+ plugin_versions_url_segment = dict (type = "list" , elements = "str" , default = ['plugin-versions.json' ,
968
+ 'current/plugin-versions.json' ]),
942
969
latest_plugins_url_segments = dict (type = "list" , elements = "str" , default = ['latest' ]),
943
970
versioned_plugins_url_segments = dict (type = "list" , elements = "str" , default = ['download/plugins' , 'plugins' ]),
944
971
url = dict (default = 'http://localhost:8080' ),
@@ -968,7 +995,7 @@ def main():
968
995
module .params ['state' ] = 'present'
969
996
module .params ['version' ] = jp ._get_latest_compatible_plugin_version ()
970
997
971
- # Ser version to latest compatible version if version is latest
998
+ # Set version to latest compatible version if version is latest
972
999
if module .params ['version' ] == 'latest' :
973
1000
module .params ['version' ] = jp ._get_latest_compatible_plugin_version ()
974
1001
0 commit comments