Skip to content

Commit 05593e5

Browse files
author
Taniya Mathur
committed
Add versioned main template upload and separate existence check
- Always upload versioned main template alongside regular main template - Add separate check for versioned template existence when main template already exists - Fix 404 error handling for versioned template check using ClientError - Ensures both idp-main.yaml and idp-main_VERSION.yaml are always available in S3 - Prevents NoSuchKey errors during ACL operations on versioned templates
1 parent 5e141ab commit 05593e5

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

publish.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,6 +1492,7 @@ def build_main_template(self, webui_zipfile, components_needing_rebuild):
14921492

14931493
# Upload main template based on whether build was required
14941494
final_template_key = f"{self.prefix}/{self.main_template}"
1495+
versioned_template_key = f"{self.prefix}/{self.main_template.replace('.yaml', f'_{self.version}.yaml')}"
14951496
packaged_template_path = ".aws-sam/idp-main.yaml"
14961497

14971498
if main_needs_build:
@@ -1517,6 +1518,25 @@ def build_main_template(self, webui_zipfile, components_needing_rebuild):
15171518
except Exception as e:
15181519
self.console.print(f"[red]Failed to upload main template: {e}[/red]")
15191520
sys.exit(1)
1521+
1522+
# Also upload versioned copy
1523+
self.console.print(
1524+
f"[cyan]Uploading versioned main template to S3: {versioned_template_key}[/cyan]"
1525+
)
1526+
try:
1527+
self.s3_client.upload_file(
1528+
packaged_template_path,
1529+
self.bucket,
1530+
versioned_template_key,
1531+
)
1532+
self.console.print(
1533+
"[green]✅ Versioned main template uploaded successfully[/green]"
1534+
)
1535+
except Exception as e:
1536+
self.console.print(
1537+
f"[red]Failed to upload versioned main template: {e}[/red]"
1538+
)
1539+
sys.exit(1)
15201540
else:
15211541
# Main was not rebuilt, check if template exists in S3
15221542
try:
@@ -1542,6 +1562,19 @@ def build_main_template(self, webui_zipfile, components_needing_rebuild):
15421562
self.console.print(
15431563
"[green]✅ Main template uploaded successfully[/green]"
15441564
)
1565+
1566+
# Also upload versioned copy
1567+
self.console.print(
1568+
f"[cyan]Uploading versioned main template to S3: {versioned_template_key}[/cyan]"
1569+
)
1570+
self.s3_client.upload_file(
1571+
packaged_template_path,
1572+
self.bucket,
1573+
versioned_template_key,
1574+
)
1575+
self.console.print(
1576+
"[green]✅ Versioned main template uploaded successfully[/green]"
1577+
)
15451578
except Exception as e:
15461579
self.console.print(
15471580
f"[red]Failed to upload main template: {e}[/red]"
@@ -1553,6 +1586,43 @@ def build_main_template(self, webui_zipfile, components_needing_rebuild):
15531586
)
15541587
self.console.print("[yellow]Proceeding without upload[/yellow]")
15551588

1589+
# Check versioned template separately
1590+
try:
1591+
self.s3_client.head_object(
1592+
Bucket=self.bucket, Key=versioned_template_key
1593+
)
1594+
self.console.print(
1595+
"[green]✅ Versioned main template already exists in S3[/green]"
1596+
)
1597+
except ClientError as e:
1598+
if e.response["Error"]["Code"] == "404":
1599+
self.console.print(
1600+
f"[yellow]Versioned template missing from S3, uploading: {versioned_template_key}[/yellow]"
1601+
)
1602+
if not os.path.exists(packaged_template_path):
1603+
self.console.print(
1604+
f"[red]Error: No packaged template to upload at {packaged_template_path}[/red]"
1605+
)
1606+
sys.exit(1)
1607+
try:
1608+
self.s3_client.upload_file(
1609+
packaged_template_path,
1610+
self.bucket,
1611+
versioned_template_key,
1612+
)
1613+
self.console.print(
1614+
"[green]✅ Versioned main template uploaded successfully[/green]"
1615+
)
1616+
except Exception as upload_e:
1617+
self.console.print(
1618+
f"[red]Failed to upload versioned template: {upload_e}[/red]"
1619+
)
1620+
sys.exit(1)
1621+
else:
1622+
self.console.print(
1623+
f"[yellow]Could not check versioned template existence: {e}[/yellow]"
1624+
)
1625+
15561626
# Validate the template
15571627
template_url = (
15581628
f"https://s3.{self.region}.amazonaws.com/{self.bucket}/{final_template_key}"

0 commit comments

Comments
 (0)