# deploy-aspblogbrainordure-test.ps1 # Polls Gitea for new commits on master; deploys ASPBlogBrainOrdure to test site if changed # Scheduled Task: ASPBlogBrainOrdure-Test-Deploy (every 5 minutes) $GITEA = "https://onefortheroadgit.sytes.net" $REPO = "dcovington/ASPBlogBrainOrdure" $TOKEN = "bac7c4befba3f0428e8786020cddb5e9595a6838" $WEBROOT = "C:\inetpub\wwwroot\aspblogbrainordure-test" $APPPOOL = "aspblogbrainordure-test" $STATEFILE = "C:\Scripts\.aspblog-last-commit" $LOGFILE = "C:\Scripts\aspblog-deploy.log" function Log($msg) { $ts = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss") "$ts $msg" | Out-File -Append -FilePath $LOGFILE Write-Host "$ts $msg" } # SSL bypass for self-signed cert Add-Type -TypeDefinition "using System.Net; using System.Security.Cryptography.X509Certificates; public class TrustAll : ICertificatePolicy { public bool CheckValidationResult(ServicePoint sp, X509Certificate cert, WebRequest req, int problem) { return true; } }" [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAll [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 Log "Checking for new commits on $REPO master..." # Get current master commit hash try { $headers = @{ "Authorization" = "token $TOKEN" } $branchUrl = "$GITEA/api/v1/repos/$REPO/branches/master" $resp = Invoke-WebRequest -Uri $branchUrl -Headers $headers -UseBasicParsing $branch = $resp.Content | ConvertFrom-Json $latestCommit = $branch.commit.id } catch { Log "ERROR fetching branch info: $_" exit 1 } # Compare with last deployed commit $lastCommit = "" if (Test-Path $STATEFILE) { $lastCommit = (Get-Content $STATEFILE -Raw).Trim() } if ($latestCommit -eq $lastCommit) { Log "No changes (commit $($latestCommit.Substring(0,8))). Nothing to do." exit 0 } Log "New commit detected: $($latestCommit.Substring(0,8)). Deploying..." # Download archive $archiveUrl = "$GITEA/api/v1/repos/$REPO/archive/master.zip" $zipPath = "C:\Scripts\aspblog-deploy.zip" try { $wc = New-Object System.Net.WebClient $wc.Headers.Add("Authorization", "token $TOKEN") $wc.DownloadFile($archiveUrl, $zipPath) Log "Downloaded archive." } catch { Log "ERROR downloading archive: $_" exit 1 } # Extract to temp folder $tmpDir = "C:\Scripts\aspblog-tmp" if (Test-Path $tmpDir) { Remove-Item $tmpDir -Recurse -Force } New-Item -ItemType Directory -Path $tmpDir | Out-Null Add-Type -AssemblyName System.IO.Compression.FileSystem [System.IO.Compression.ZipFile]::ExtractToDirectory($zipPath, $tmpDir) # Find the extracted subfolder (Gitea puts files in a subfolder) $extracted = Get-ChildItem $tmpDir -Directory | Select-Object -First 1 if (-not $extracted) { Log "ERROR: No directory found in archive."; exit 1 } $srcPath = $extracted.FullName Log "Extracted to $srcPath" # Deploy structure mirrors the repo layout: # IIS serves from $WEBROOT\public\ (Default.asp lives here) # core\, app\, db\ go alongside public\ so ../core/ includes resolve correctly $publicDst = Join-Path $WEBROOT "public" New-Item -ItemType Directory -Force -Path $publicDst | Out-Null foreach ($folder in @("public", "core", "app", "scripts")) { $folderSrc = Join-Path $srcPath $folder $folderDst = Join-Path $WEBROOT $folder if (Test-Path $folderSrc) { if (Test-Path $folderDst) { Remove-Item $folderDst -Recurse -Force } Copy-Item $folderSrc $WEBROOT -Recurse -Force Log "Copied $folder/." } } # Only update db/ if webdata.mdb doesn't exist yet (preserve live data) $dbSrc = Join-Path $srcPath "db" $dbDst = Join-Path $WEBROOT "db" if (Test-Path $dbSrc) { if (-not (Test-Path "$dbDst\webdata.mdb")) { if (Test-Path $dbDst) { Remove-Item $dbDst -Recurse -Force } Copy-Item $dbSrc $WEBROOT -Recurse -Force Log "Copied db/ (first deploy)." } else { # Only copy migration files, not the live database $migSrc = Join-Path $dbSrc "migrations" if (Test-Path $migSrc) { Copy-Item "$migSrc\*" "$dbDst\migrations\" -Recurse -Force Log "Updated db/migrations/." } } } # Update web.config ConnectionString to point to correct DB path $webConfigPath = Join-Path $publicDst "web.config" if (Test-Path $webConfigPath) { $wc = [System.IO.File]::ReadAllText($webConfigPath) $newConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=$WEBROOT\db\webdata.mdb;Persist Security Info=False;" $wc = [regex]::Replace($wc, 'Provider=Microsoft\.(ACE\.OLEDB\.12\.0|Jet\.OLEDB\.4\.0)[^"]+', $newConn) if ($wc -notmatch 'enableParentPaths') { $wc = $wc -replace '', '' } [System.IO.File]::WriteAllText($webConfigPath, $wc) Log "Updated web.config." } # Create webdata.mdb if it doesn't exist (use 32-bit cscript — JET 4.0 is 32-bit only) $mdbPath = "$WEBROOT\db\webdata.mdb" if (-not (Test-Path $mdbPath)) { Log "Creating empty webdata.mdb via ADOX (32-bit cscript)..." $tmpVbs = "C:\Scripts\create_mdb_tmp.vbs" @" Dim cat Set cat = CreateObject("ADOX.Catalog") cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=$mdbPath;" Set cat = Nothing WScript.Quit 0 "@ | Out-File -FilePath $tmpVbs -Encoding ASCII & "C:\Windows\SysWOW64\cscript.exe" "//nologo" $tmpVbs Remove-Item $tmpVbs -Force -ErrorAction SilentlyContinue if (Test-Path $mdbPath) { Log "Created webdata.mdb." } else { Log "ERROR: Failed to create webdata.mdb" exit 1 } } # Run database migrations (32-bit cscript required for JET OLEDB) $migrationsVbs = Join-Path $WEBROOT "scripts\runMigrations.vbs" if (Test-Path $migrationsVbs) { Log "Running database migrations..." $output = & "C:\Windows\SysWOW64\cscript.exe" "//nologo" $migrationsVbs "up" 2>&1 $output | ForEach-Object { Log " [migrate] $_" } if ($LASTEXITCODE -ne 0) { Log "ERROR: Migration failed (exit code $LASTEXITCODE)" exit 1 } Log "Migrations complete." } # Grant IIS_IUSRS write access to db folder $dbPath = Join-Path $WEBROOT "db" New-Item -ItemType Directory -Force -Path $dbPath | Out-Null if (Test-Path $dbPath) { $acl = Get-Acl $dbPath $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("IIS_IUSRS", "Modify", "ContainerInherit,ObjectInherit", "None", "Allow") $acl.SetAccessRule($rule) Set-Acl $dbPath $acl Log "Set IIS_IUSRS Modify on db folder." } # Recycle app pool Import-Module WebAdministration -ErrorAction SilentlyContinue try { Restart-WebAppPool $APPPOOL Log "Recycled app pool $APPPOOL." } catch { Log "WARNING: Could not recycle app pool: $_" } # Cleanup Remove-Item $tmpDir -Recurse -Force Remove-Item $zipPath -Force # Save new commit hash $latestCommit | Out-File -FilePath $STATEFILE -NoNewline Log "Deploy complete. Commit: $($latestCommit.Substring(0,8))"