Skip to content

Commit fe76622

Browse files
rajbosCopilot
andcommitted
fix: import stranded cert instead of deleting and recreating
When a Terraform apply times out waiting for managed cert provisioning, Azure retains the cert in a Pending/Creating state but TF drops it from state. The reconcile step previously saw the mismatch and deleted the Azure cert, forcing a fresh creation that also timed out -- an infinite loop. Fix: before deleting anything, look up whether the correctly-named cert ('sharing-cert') already exists in Azure. If it does, import it into TF state so the next apply skips the 60-minute creation wait and only re-runs the fast cert-binding step. The existing delete-and-recreate path is retained as a fallback if the import fails. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent f981c4b commit fe76622

1 file changed

Lines changed: 59 additions & 23 deletions

File tree

.github/workflows/sharing-server-deploy.yml

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -205,35 +205,71 @@ jobs:
205205
| grep -E '^\s+name\s+=' | head -1 \
206206
| sed 's/.*= "\(.*\)".*/\1/' || true)
207207
208-
if [[ "$CURRENT_CERT_NAME" != "$EXPECTED_CERT_NAME" ]]; then
209-
echo "Cert not TF-managed (current: '${CURRENT_CERT_NAME:-none}'). Cleaning up Azure resources so Terraform can recreate them."
210-
211-
# Remove hostname binding first (cert cannot be deleted while a domain uses it)
212-
az containerapp hostname delete \
213-
--name "$TF_VAR_app_name" \
214-
--resource-group "$TF_VAR_resource_group_name" \
215-
--hostname "$TF_VAR_custom_domain" --yes 2>/dev/null || true
216-
217-
# Find the cert by subject name and delete it
218-
AZURE_CERT_NAME=$(az containerapp env certificate list \
208+
if [[ "$CURRENT_CERT_NAME" == "$EXPECTED_CERT_NAME" ]]; then
209+
echo "Cert already TF-managed as '$EXPECTED_CERT_NAME'. No cleanup needed."
210+
else
211+
# Cert is absent from TF state or has a mismatched name.
212+
# Before deleting anything, check whether the correctly-named cert already
213+
# exists in Azure (e.g. a previous apply timed out while polling for the cert
214+
# to become Succeeded, leaving it stranded in Azure but dropped from TF state).
215+
AZURE_CERT_ID=$(az containerapp env certificate list \
219216
--name "$ENV_NAME" \
220217
--resource-group "$TF_VAR_resource_group_name" \
221-
--query "[?properties.subjectName=='$TF_VAR_custom_domain'].name | [0]" \
218+
--query "[?name=='$EXPECTED_CERT_NAME'].id | [0]" \
222219
-o tsv 2>/dev/null || true)
223-
if [[ -n "$AZURE_CERT_NAME" && "$AZURE_CERT_NAME" != "None" ]]; then
224-
echo "Deleting Azure cert: $AZURE_CERT_NAME"
225-
az containerapp env certificate delete \
220+
221+
if [[ -n "$AZURE_CERT_ID" && "$AZURE_CERT_ID" != "None" ]]; then
222+
# The correctly-named cert exists in Azure but TF lost track of it.
223+
# Import it so apply doesn't delete-and-recreate (which resets provisioning
224+
# and triggers another 60-minute wait).
225+
echo "Cert '$EXPECTED_CERT_NAME' found in Azure but not in TF state. Importing..."
226+
if terraform import 'azurerm_container_app_environment_managed_certificate.this[0]' "$AZURE_CERT_ID"; then
227+
# Drop stale custom-domain state so cert_binding re-runs to re-bind.
228+
terraform state rm 'azurerm_container_app_custom_domain.this[0]' 2>/dev/null || true
229+
echo "Import done. Terraform will rebind the cert without recreating it."
230+
else
231+
# Import failed; delete the Azure cert so apply doesn't hit "already exists".
232+
echo "Import failed. Deleting Azure cert so Terraform can create a fresh one."
233+
az containerapp hostname delete \
234+
--name "$TF_VAR_app_name" \
235+
--resource-group "$TF_VAR_resource_group_name" \
236+
--hostname "$TF_VAR_custom_domain" --yes 2>/dev/null || true
237+
az containerapp env certificate delete \
238+
--name "$ENV_NAME" \
239+
--resource-group "$TF_VAR_resource_group_name" \
240+
--certificate "$EXPECTED_CERT_NAME" --yes 2>/dev/null || true
241+
terraform state rm 'azurerm_container_app_custom_domain.this[0]' 2>/dev/null || true
242+
terraform state rm 'azurerm_container_app_environment_managed_certificate.this[0]' 2>/dev/null || true
243+
echo "Cleanup done. Terraform will create cert and domain binding from scratch."
244+
fi
245+
else
246+
echo "Cert not TF-managed (current: '${CURRENT_CERT_NAME:-none}'). Cleaning up Azure resources so Terraform can recreate them."
247+
248+
# Remove hostname binding first (cert cannot be deleted while a domain uses it)
249+
az containerapp hostname delete \
250+
--name "$TF_VAR_app_name" \
251+
--resource-group "$TF_VAR_resource_group_name" \
252+
--hostname "$TF_VAR_custom_domain" --yes 2>/dev/null || true
253+
254+
# Find the cert by subject name and delete it
255+
AZURE_CERT_NAME=$(az containerapp env certificate list \
226256
--name "$ENV_NAME" \
227257
--resource-group "$TF_VAR_resource_group_name" \
228-
--certificate "$AZURE_CERT_NAME" --yes 2>/dev/null || true
229-
fi
258+
--query "[?properties.subjectName=='$TF_VAR_custom_domain'].name | [0]" \
259+
-o tsv 2>/dev/null || true)
260+
if [[ -n "$AZURE_CERT_NAME" && "$AZURE_CERT_NAME" != "None" ]]; then
261+
echo "Deleting Azure cert: $AZURE_CERT_NAME"
262+
az containerapp env certificate delete \
263+
--name "$ENV_NAME" \
264+
--resource-group "$TF_VAR_resource_group_name" \
265+
--certificate "$AZURE_CERT_NAME" --yes 2>/dev/null || true
266+
fi
230267
231-
# Remove stale TF state entries so Terraform creates fresh resources
232-
terraform state rm 'azurerm_container_app_custom_domain.this[0]' 2>/dev/null || true
233-
terraform state rm 'azurerm_container_app_environment_managed_certificate.this[0]' 2>/dev/null || true
234-
echo "Cleanup done. Terraform will create cert and domain binding from scratch."
235-
else
236-
echo "Cert already TF-managed as '$EXPECTED_CERT_NAME'. No cleanup needed."
268+
# Remove stale TF state entries so Terraform creates fresh resources
269+
terraform state rm 'azurerm_container_app_custom_domain.this[0]' 2>/dev/null || true
270+
terraform state rm 'azurerm_container_app_environment_managed_certificate.this[0]' 2>/dev/null || true
271+
echo "Cleanup done. Terraform will create cert and domain binding from scratch."
272+
fi
237273
fi
238274
239275
- name: Terraform plan

0 commit comments

Comments
 (0)