22import logging
33import os
44import shutil
5+ import sys
56import uuid
67
78from gitopscli .git .create_git import create_git
@@ -83,12 +84,9 @@ def create_preview_command(
8384 new_image_tag = apps_git .get_last_commit_hash ()
8485 logging .info ("Using image tag from last app repo commit: %s" , new_image_tag )
8586 for image_path in gitops_config .image_paths :
86- yaml_replace_path = image_path ["yamlpath" ]
87- logging .info ("Replacing property %s with value: %s" , yaml_replace_path , new_image_tag )
88- update_yaml_file (
89- root_git .get_full_file_path (new_preview_folder_name + "/values.yaml" ), yaml_replace_path , new_image_tag ,
87+ __replace_image_tag_value (
88+ apps_git , image_path , new_image_tag , new_preview_folder_name , parent_id , pr_id , root_git
9089 )
91- root_git .commit (f"changed '{ yaml_replace_path } ' to '{ new_image_tag } '" )
9290 root_git .push (branch )
9391 logging .info ("Pushed branch %s" , branch )
9492 pr_comment_text = f"""
@@ -105,6 +103,27 @@ def create_preview_command(
105103 __merge_pullrequest (branch , pull_request , root_git )
106104
107105
106+ def __replace_image_tag_value (apps_git , image_path , new_image_tag , new_preview_folder_name , parent_id , pr_id , root_git ):
107+ yaml_replace_path = image_path ["yamlpath" ]
108+ logging .info ("Replacing property %s with value: %s" , yaml_replace_path , new_image_tag )
109+ value_replaced = update_yaml_file (
110+ root_git .get_full_file_path (new_preview_folder_name + "/values.yaml" ), yaml_replace_path , new_image_tag ,
111+ )
112+ if not value_replaced :
113+ __no_deployment_needed (apps_git , new_image_tag , parent_id , pr_id )
114+ sys .exit (0 )
115+ root_git .commit (f"changed '{ yaml_replace_path } ' to '{ new_image_tag } '" )
116+
117+
118+ def __no_deployment_needed (apps_git , new_image_tag , parent_id , pr_id ):
119+ logging .info ("The image tag %s has already been deployed. Doing nothing." , new_image_tag )
120+ pr_comment_text = f"""
121+ The version { new_image_tag } has already been deployed. Nothing to do here.
122+ """
123+ logging .info ("Creating PullRequest comment for pr with id %s and content: %s" , pr_id , pr_comment_text )
124+ apps_git .add_pull_request_comment (pr_id , pr_comment_text , parent_id )
125+
126+
108127def __create_new_preview_env (
109128 branch , gitops_config , new_preview_folder_name , preview_template_folder_name , root_git , route_host ,
110129):
@@ -115,15 +134,13 @@ def __create_new_preview_env(
115134 logging .info ("Looking for Chart.yaml at: %s" , chart_file_path )
116135 if root_git .get_full_file_path (chart_file_path ):
117136 update_yaml_file (root_git .get_full_file_path (chart_file_path ), "name" , new_preview_folder_name )
118- if gitops_config .route_paths :
119- for route_path in gitops_config .route_paths :
120- yaml_replace_path = route_path ["hostpath" ]
121- logging .info ("Replacing property %s with value: %s" , yaml_replace_path , route_host )
122- update_yaml_file (
123- root_git .get_full_file_path (new_preview_folder_name + "/values.yaml" ),
124- yaml_replace_path ,
125- route_host ,
126- )
137+ if gitops_config .route_paths :
138+ for route_path in gitops_config .route_paths :
139+ yaml_replace_path = route_path ["hostpath" ]
140+ logging .info ("Replacing property %s with value: %s" , yaml_replace_path , route_host )
141+ update_yaml_file (
142+ root_git .get_full_file_path (new_preview_folder_name + "/values.yaml" ), yaml_replace_path , route_host ,
143+ )
127144 root_git .commit (f"Initiated new preview env for branch { branch } '" )
128145 return route_host
129146
0 commit comments