@@ -70,27 +70,39 @@ def create_preview_command(
7070 logging .info ("Using the preview template folder: %s" , preview_template_folder_name )
7171 new_preview_folder_name = gitops_config .application_name + "-" + shortened_branch_hash + "-preview"
7272 logging .info ("New folder for preview: %s" , new_preview_folder_name )
73- route_host = None
74- logging .info ("New folder for preview: %s" , new_preview_folder_name )
7573 branch_preview_env_already_exist = os .path .exists (root_git .get_full_file_path (new_preview_folder_name ))
7674 logging .info ("Is preview env already existing for branch? %s" , branch_preview_env_already_exist )
77- if gitops_config .route_paths :
78- route_host = gitops_config .route_host .replace ("previewplaceholder" , shortened_branch_hash )
79- logging .info ("Created route host: %s" , route_host )
8075 if not branch_preview_env_already_exist :
81- __create_new_preview_env (
82- branch , gitops_config , new_preview_folder_name , preview_template_folder_name , root_git , route_host ,
83- )
76+ __create_new_preview_env (branch , new_preview_folder_name , preview_template_folder_name , root_git )
8477 new_image_tag = apps_git .get_last_commit_hash ()
8578 logging .info ("Using image tag from last app repo commit: %s" , new_image_tag )
86- for image_path in gitops_config .image_paths :
87- __replace_image_tag_value (
88- apps_git , image_path , new_image_tag , new_preview_folder_name , parent_id , pr_id , root_git
79+ route_host = None
80+ value_replaced = False
81+ for replacement in gitops_config .replacements :
82+ route_host , value_replaced = __replace_value (
83+ gitops_config ,
84+ new_image_tag ,
85+ new_preview_folder_name ,
86+ replacement ,
87+ root_git ,
88+ route_host ,
89+ shortened_branch_hash ,
90+ value_replaced ,
8991 )
92+ if not value_replaced :
93+ __no_deployment_needed (apps_git , new_image_tag , parent_id , pr_id )
94+ sys .exit (0 )
95+ root_git .commit (f"Upated preview environment for { gitops_config .application_name } ." )
9096 root_git .push (branch )
9197 logging .info ("Pushed branch %s" , branch )
9298 pr_comment_text = f"""
93- Preview created successfully. Access it here: https://{ route_host } .
99+ New Preview Environment for { gitops_config .application_name } created successfully. Access it here:
100+ https://{ route_host }
101+ """
102+ if branch_preview_env_already_exist :
103+ pr_comment_text = f"""
104+ Preview Environment for { gitops_config .application_name } updated successfully. Access it here:
105+ https://{ route_host }
94106"""
95107 logging .info ("Creating PullRequest comment for pr with id %s and content: %s" , pr_id , pr_comment_text )
96108 apps_git .add_pull_request_comment (pr_id , pr_comment_text , parent_id )
@@ -103,16 +115,33 @@ def create_preview_command(
103115 __merge_pullrequest (branch , pull_request , root_git )
104116
105117
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 ,
118+ def __replace_value (
119+ gitops_config ,
120+ new_image_tag ,
121+ new_preview_folder_name ,
122+ replacement ,
123+ root_git ,
124+ route_host ,
125+ shortened_branch_hash ,
126+ value_replaced ,
127+ ):
128+ replacement_value = None
129+ logging .info ("Replacement: %s" , replacement )
130+ replacement_path_ = replacement ["path" ]
131+ replacement_variable = replacement ["variable" ]
132+ if replacement_variable == "GIT_COMMIT" :
133+ replacement_value = new_image_tag
134+ elif replacement_variable == "ROUTE_HOST" :
135+ route_host = gitops_config .route_host .replace ("{SHA256_8CHAR_BRANCH_HASH}" , shortened_branch_hash )
136+ logging .info ("Created route host: %s" , route_host )
137+ replacement_value = route_host
138+ else :
139+ logging .info ("Unknown replacement variable: %s" , replacement_variable )
140+ value_replaced = value_replaced | update_yaml_file (
141+ root_git .get_full_file_path (new_preview_folder_name + "/values.yaml" ), replacement_path_ , replacement_value ,
111142 )
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 } '" )
143+ logging .info ("Replacing property %s with value: %s" , replacement_path_ , replacement_value )
144+ return route_host , value_replaced
116145
117146
118147def __no_deployment_needed (apps_git , new_image_tag , parent_id , pr_id ):
@@ -125,7 +154,7 @@ def __no_deployment_needed(apps_git, new_image_tag, parent_id, pr_id):
125154
126155
127156def __create_new_preview_env (
128- branch , gitops_config , new_preview_folder_name , preview_template_folder_name , root_git , route_host ,
157+ branch , new_preview_folder_name , preview_template_folder_name , root_git ,
129158):
130159 shutil .copytree (
131160 root_git .get_full_file_path (preview_template_folder_name ), root_git .get_full_file_path (new_preview_folder_name ),
@@ -134,15 +163,7 @@ def __create_new_preview_env(
134163 logging .info ("Looking for Chart.yaml at: %s" , chart_file_path )
135164 if root_git .get_full_file_path (chart_file_path ):
136165 update_yaml_file (root_git .get_full_file_path (chart_file_path ), "name" , new_preview_folder_name )
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- )
144166 root_git .commit (f"Initiated new preview env for branch { branch } '" )
145- return route_host
146167
147168
148169def __create_pullrequest (branch , gitops_config , root_git ):
0 commit comments