Last active
June 3, 2025 11:51
-
-
Save ThomasAunvik/33a2b7fd3248ea2f88341d441be4727d to your computer and use it in GitHub Desktop.
Domeneshop DNS Script for Azure Bicep Deployment with App Service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param targetDomain string | |
param appName string | |
param customDomainVerificationId string | |
@secure() | |
param domainUsername string | |
@secure() | |
param domainPassword string | |
resource ds 'Microsoft.Resources/deploymentScripts@2023-08-01' = { | |
kind: 'AzurePowerShell' | |
name: 'domeneshop-dns-${targetDomain}' | |
location: resourceGroup().location | |
properties: { | |
azPowerShellVersion: '10.4' | |
retentionInterval: 'PT1H' | |
timeout: 'PT5M' | |
cleanupPreference: 'OnSuccess' | |
arguments: '-appname ${appName} -customDomainVerificationId ${customDomainVerificationId} -targetDomain ${targetDomain}' | |
environmentVariables: [ | |
{ name: 'DOMAIN_USERNAME', secureValue: domainUsername } | |
{ name: 'DOMAIN_PASSWORD', secureValue: domainPassword } | |
] | |
scriptContent: ''' | |
param([string] $appname, [string] $customDomainVerificationId, [string] $targetDomain) | |
$zone = $targetDomain.Split(".")[-2..-1] -join "." | |
$domainHost = $targetDomain.Split(".")[0..($targetDomain.Split(".").Length - 3)] -join "." | |
$isRoot = $false | |
if ($domainHost -eq "" -or $domainHost -eq $targetDomain) { | |
# If the domain is a root domain (e.g., example.com), set the host to "@" and mark it as root | |
$domainHost = "@" | |
$isRoot = $true | |
} | |
$auth = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("${Env:DOMAIN_USERNAME}:${Env:DOMAIN_PASSWORD}")) | |
$domains = Invoke-WebRequest -Uri "https://api.domeneshop.no/v0/domains?domain=$zone" -Method Get ` | |
-Headers @{Authorization = $auth} | |
$domainId = (ConvertFrom-Json -InputObject $domains.Content)[0].id | |
Write-Output "Domain Content: $($domains.Content)" | |
Write-Output "Domain ID: $domainId" | |
Write-Output "DomainHost: $domainHost" | |
# Check if the TXT record already exists for the custom domain verification ID | |
$existingTxtRecord = Invoke-WebRequest -Uri "https://api.domeneshop.no/v0/domains/$domainId/dns?host=asuid.$($appname)&type=TXT" -Method Get ` | |
-Headers @{Authorization = $auth} ` | |
-ContentType "application/json" | |
$existingTxtList = (ConvertFrom-Json -InputObject $existingTxtRecord.Content) | |
if ($existingTxtList.Count -gt 0) { | |
Write-Output "TXT record already exists for asuid.$($appname)." | |
Write-Output "Existing TXT Record: $($existingTxtList | ConvertTo-Json)" | |
if($existingTxtList[0].data -eq $customDomainVerificationId) { | |
Write-Output "The existing TXT record matches the custom domain verification ID." | |
} else { | |
Write-Error "The existing TXT record does not match the custom domain verification ID. " | |
exit -1 | |
} | |
} else { | |
Write-Output "No existing TXT record found for asuid.$($appname)." | |
$txtRecord = Invoke-WebRequest -Uri "https://api.domeneshop.no/v0/domains/$domainId/dns" -Method Post ` | |
-Headers @{Authorization = $auth} ` | |
-ContentType "application/json" ` | |
-Body (@{ | |
host = "asuid.$($appname)" | |
ttl = 3600 | |
type = "TXT" | |
data = $customDomainVerificationId | |
} | ConvertTo-Json) | |
Write-Output "TXT Record Response: $($txtRecord.Content)" | |
} | |
$existingCnameRecord = Invoke-WebRequest -Uri "https://api.domeneshop.no/v0/domains/$domainId/dns?host=$domainHost&type=$($isRoot ? 'ANAME' : 'CNAME')" -Method Get ` | |
-Headers @{Authorization = $auth} ` | |
-ContentType "application/json" | |
$existingCnameList = (ConvertFrom-Json -InputObject $existingCnameRecord.Content) | |
if ($existingCnameList.Count -gt 0) { | |
Write-Output "CNAME record already exists for $domainHost." | |
Write-Output "Existing CNAME Record: $($existingCnameList | ConvertTo-Json)" | |
if ($existingCnameList[0].data -eq "$($appname).azurewebsites.net.") { | |
Write-Output "The existing CNAME record matches the Azure Web App." | |
} else { | |
Write-Error "The existing CNAME record does not match the Azure Web App. " | |
exit -1 | |
} | |
} else { | |
Write-Output "No existing CNAME record found for $domainHost." | |
$cnameRecord = Invoke-WebRequest -Uri "https://api.domeneshop.no/v0/domains/$domainId/dns" -Method Post ` | |
-Headers @{Authorization = $auth} ` | |
-ContentType "application/json" ` | |
-Body (@{ | |
host = $domainHost | |
ttl = 3600 | |
type = $isRoot ? "ANAME" : "CNAME" | |
data = "$($appname).azurewebsites.net" | |
} | ConvertTo-Json) | |
Write-Output "CNAME Record Response: $($cnameRecord.Content)" | |
} | |
''' | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment