Skip to content

Commit 7a06de2

Browse files
authored
Add configuration files for static web app with global headers (#2968)
This pull request introduces a new workflow step to automatically merge environment-specific configuration files for static web apps during the build process. It also adds initial configuration files for different deployment environments, enabling environment-based customization of global HTTP headers. **Workflow automation:** * Added a "Merge Configuration Files" step to `.github/workflows/main.yml` that locates and merges environment-specific `staticwebapp.*.json` files using `jq`, producing a unified `staticwebapp.config.json` for deployment. This step includes error handling and debug output for easier troubleshooting. **Environment-specific configuration:** * Added `staticwebapp.config.canary.json` as a placeholder for canary environment configuration in `docs/static/`. * Added `staticwebapp.config.preview.json` with global HTTP headers for CORS, allowing requests from `https://devopsmigration.io` in the preview environment. * Added `staticwebapp.config.production.json` with global HTTP headers for CORS, allowing requests from the production Azure static app domain.
2 parents faf81b1 + d9e38e4 commit 7a06de2

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

.github/workflows/main.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,67 @@ jobs:
670670
with:
671671
name: AzureDevOpsMigrationTools-Site2
672672
path: ./_site2
673+
674+
- name: Merge Configuration Files
675+
run: |
676+
# Find all files matching the pattern in a safe way
677+
$files = Get-ChildItem -Path "." -Recurse -Filter "staticwebapp.*.json" -ErrorAction Stop
678+
679+
if ($files.Count -eq 0) {
680+
Write-Host "No files matching the pattern 'staticwebapp.*.json' were found."
681+
exit 1
682+
}
683+
684+
# Output each file's full name (for verification/debugging purposes)
685+
$files | ForEach-Object {
686+
Write-Host "Found file: $($_.FullName)"
687+
}
688+
689+
# Paths to main and environment-specific config files
690+
$environment = ("${{ (needs.Setup.outputs.nkdAgility_Ring) }}").ToLower()
691+
$rootConfig = "./_site2/staticwebapp.config.json"
692+
$environmentConfig = "./_site2/staticwebapp.config.$environment.json"
693+
694+
# Check if both target files exist
695+
if ((Test-Path -Path $rootConfig -ErrorAction Stop) -and (Test-Path -Path $environmentConfig -ErrorAction Stop)) {
696+
try {
697+
# Run jq to merge files and capture the output
698+
$mergedContent = & jq -s 'reduce .[] as $item ({}; . * $item)' $rootConfig $environmentConfig # $routesConfig
699+
700+
if ($mergedContent -ne "") {
701+
# Write the merged content to the output file
702+
$mergedContent | Set-Content -Path "./_site2/staticwebapp.config.json"
703+
Write-Host "Merged JSON files successfully."
704+
}
705+
else {
706+
Write-Host "jq command produced empty output. Check JSON structures in input files."
707+
exit 1
708+
}
709+
}
710+
catch {
711+
Write-Host "Error merging JSON files with jq: $_"
712+
exit 1
713+
}
714+
}
715+
else {
716+
Write-Host "One or both of the specified config files were not found:"
717+
if (!(Test-Path -Path $rootConfig)) { Write-Host " - $rootConfig not found" }
718+
if (!(Test-Path -Path $environmentConfig)) { Write-Host " - $environmentConfig not found" }
719+
exit 1
720+
}
721+
722+
# Verify and read the merged file content
723+
try {
724+
$content = Get-Content -Path "./_site2/staticwebapp.config.json" -ErrorAction Stop
725+
Write-Host "Content of merged config file:"
726+
Write-Output $content
727+
}
728+
catch {
729+
Write-Host "Error reading the merged config file: $_"
730+
exit 1
731+
}
732+
733+
673734
- name: "Find files"
674735
shell: pwsh
675736
run: |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}

docs/static/staticwebapp.config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
{
22
"routes": [
3+
{
4+
"route": "/version.json",
5+
"headers": {
6+
"Access-Control-Allow-Origin": "*",
7+
"Access-Control-Allow-Methods": "GET, OPTIONS",
8+
"Access-Control-Allow-Headers": "Content-Type"
9+
}
10+
}
311
],
412
"responseOverrides": {
513
"404": {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}

0 commit comments

Comments
 (0)