|
- # 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"
-
- # Copy public/ folder contents to webroot (this is the IIS-served content)
- $publicSrc = Join-Path $srcPath "public"
- if (Test-Path $publicSrc) {
- # Copy all files from public/ to webroot root
- Copy-Item "$publicSrc\*" $WEBROOT -Recurse -Force
- Log "Copied public/ to webroot."
- } else {
- # If no public folder, copy everything
- Copy-Item "$srcPath\*" $WEBROOT -Recurse -Force
- Log "Copied all files to webroot."
- }
-
- # Copy core/, app/ alongside Default.asp (needed for ../core/ relative includes)
- foreach ($folder in @("core", "app", "db")) {
- $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/ to webroot."
- }
- }
-
- # Update web.config ConnectionString to point to correct DB path
- $webConfigPath = Join-Path $WEBROOT "web.config"
- if (Test-Path $webConfigPath) {
- $wc = [System.IO.File]::ReadAllText($webConfigPath)
- $newConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$WEBROOT\db\webdata.accdb;Persist Security Info=False;"
- $wc = [regex]::Replace($wc, 'Provider=Microsoft\.ACE\.OLEDB[^"]+', $newConn)
- [System.IO.File]::WriteAllText($webConfigPath, $wc)
- Log "Updated web.config ConnectionString."
- }
-
- # Grant IIS_IUSRS write access to db folder
- $dbPath = Join-Path $WEBROOT "db"
- 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))"
|