@@ -261,6 +261,36 @@ def _get_release_version_from_release_json(release_json, version_re, release_jso
261261 return release_component_version
262262
263263
264+ def _load_agent_version_cache (ctx , pipeline_id = None , project_name = None ):
265+ """Return parsed agent-version.cache contents, or None if unavailable.
266+
267+ A local cache file is used whenever it exists. S3 is only contacted when the
268+ file is missing and CI coordinates are present. This matters for Omnibus:
269+ its sanitized environment omits CI_PIPELINE_ID, so Windows package builds
270+ cannot re-fetch the cache, but they do copy the file into the source tree.
271+ """
272+ if project_name is None :
273+ project_name = os .getenv ("CI_PROJECT_NAME" )
274+ try :
275+ cache_exists = os .path .exists (AGENT_VERSION_CACHE_NAME )
276+ if not cache_exists and pipeline_id and str (pipeline_id ).isdigit () and project_name == REPO_NAME :
277+ result = ctx .run (
278+ f"aws s3 cp s3://dd-ci-artefacts-build-stable/datadog-agent/{ pipeline_id } /{ AGENT_VERSION_CACHE_NAME } ." ,
279+ hide = "stdout" ,
280+ )
281+ if "unable to locate credentials" in result .stderr .casefold ():
282+ raise Exit ("Permanent error: unable to locate credentials, retry the job" , 42 )
283+ cache_exists = True
284+ if not cache_exists :
285+ return None
286+ with open (AGENT_VERSION_CACHE_NAME ) as file :
287+ return json .load (file )
288+ except (OSError , json .JSONDecodeError ) as e :
289+ # If a cache file is found but corrupted we ignore it.
290+ print (f"Error while recovering the version from { AGENT_VERSION_CACHE_NAME } : { e } " , file = sys .stderr )
291+ return None
292+
293+
264294def get_version (
265295 ctx ,
266296 url_safe = False ,
@@ -280,28 +310,15 @@ def get_version(
280310
281311 project_name = os .getenv ("CI_PROJECT_NAME" )
282312 try :
283- agent_version_cache_file_exist = os .path .exists (AGENT_VERSION_CACHE_NAME )
284- if not agent_version_cache_file_exist :
285- if pipeline_id and pipeline_id .isdigit () and project_name == REPO_NAME :
286- result = ctx .run (
287- f"aws s3 cp s3://dd-ci-artefacts-build-stable/datadog-agent/{ pipeline_id } /{ AGENT_VERSION_CACHE_NAME } ." ,
288- hide = "stdout" ,
289- )
290- if "unable to locate credentials" in result .stderr .casefold ():
291- raise Exit ("Permanent error: unable to locate credentials, retry the job" , 42 )
292- agent_version_cache_file_exist = True
293-
294- if agent_version_cache_file_exist :
295- with open (AGENT_VERSION_CACHE_NAME ) as file :
296- cache_data = json .load (file )
297-
313+ cache_data = _load_agent_version_cache (ctx , pipeline_id = pipeline_id , project_name = project_name )
314+ if cache_data :
298315 version , pre , commits_since_version , git_sha , pipeline_id = cache_data [major_version ]
299316 # Dev's versions behave the same as nightly
300317 is_nightly = cache_data ["nightly" ] or cache_data ["dev" ]
301318
302319 if pre and include_pre :
303320 version = f"{ version } -{ pre } "
304- except (OSError , json . JSONDecodeError , IndexError ) as e :
321+ except (IndexError , KeyError , TypeError , ValueError ) as e :
305322 # If a cache file is found but corrupted we ignore it.
306323 print (f"Error while recovering the version from { AGENT_VERSION_CACHE_NAME } : { e } " , file = sys .stderr )
307324 version = ""
@@ -344,24 +361,14 @@ def get_version_numeric_only(ctx, major_version='7'):
344361 version = ""
345362 pipeline_id = os .getenv ("CI_PIPELINE_ID" )
346363 project_name = os .getenv ("CI_PROJECT_NAME" )
347- if pipeline_id and pipeline_id .isdigit () and project_name == REPO_NAME :
348- try :
349- if not os .path .exists (AGENT_VERSION_CACHE_NAME ):
350- result = ctx .run (
351- f"aws s3 cp s3://dd-ci-artefacts-build-stable/datadog-agent/{ pipeline_id } /{ AGENT_VERSION_CACHE_NAME } ." ,
352- hide = "stdout" ,
353- )
354- if "unable to locate credentials" in result .stderr .casefold ():
355- raise Exit ("Permanent error: unable to locate credentials, retry the job" , 42 )
356-
357- with open (AGENT_VERSION_CACHE_NAME ) as file :
358- cache_data = json .load (file )
359-
364+ try :
365+ cache_data = _load_agent_version_cache (ctx , pipeline_id = pipeline_id , project_name = project_name )
366+ if cache_data :
360367 version , * _ = cache_data [major_version ]
361- except (OSError , json . JSONDecodeError , IndexError ) as e :
362- # If a cache file is found but corrupted we ignore it.
363- print (f"Error while recovering the version from { AGENT_VERSION_CACHE_NAME } : { e } " )
364- version = ""
368+ except (IndexError , KeyError , TypeError , ValueError ) as e :
369+ # If a cache file is found but corrupted we ignore it.
370+ print (f"Error while recovering the version from { AGENT_VERSION_CACHE_NAME } : { e } " , file = sys . stderr )
371+ version = ""
365372 if not version :
366373 version , * _ = query_version (ctx , major_version )
367374 return version
0 commit comments