ASP Classic blog framework - BrainOrdure
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.9KB

  1. # deploy-aspblogbrainordure-test.ps1
  2. # Polls Gitea for new commits on master; deploys ASPBlogBrainOrdure to test site if changed
  3. # Scheduled Task: ASPBlogBrainOrdure-Test-Deploy (every 5 minutes)
  4. $GITEA = "https://onefortheroadgit.sytes.net"
  5. $REPO = "dcovington/ASPBlogBrainOrdure"
  6. $TOKEN = "bac7c4befba3f0428e8786020cddb5e9595a6838"
  7. $WEBROOT = "C:\inetpub\wwwroot\aspblogbrainordure-test"
  8. $APPPOOL = "aspblogbrainordure-test"
  9. $STATEFILE = "C:\Scripts\.aspblog-last-commit"
  10. $LOGFILE = "C:\Scripts\aspblog-deploy.log"
  11. function Log($msg) {
  12. $ts = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
  13. "$ts $msg" | Out-File -Append -FilePath $LOGFILE
  14. Write-Host "$ts $msg"
  15. }
  16. # SSL bypass for self-signed cert
  17. 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; } }"
  18. [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAll
  19. [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
  20. Log "Checking for new commits on $REPO master..."
  21. # Get current master commit hash
  22. try {
  23. $headers = @{ "Authorization" = "token $TOKEN" }
  24. $branchUrl = "$GITEA/api/v1/repos/$REPO/branches/master"
  25. $resp = Invoke-WebRequest -Uri $branchUrl -Headers $headers -UseBasicParsing
  26. $branch = $resp.Content | ConvertFrom-Json
  27. $latestCommit = $branch.commit.id
  28. } catch {
  29. Log "ERROR fetching branch info: $_"
  30. exit 1
  31. }
  32. # Compare with last deployed commit
  33. $lastCommit = ""
  34. if (Test-Path $STATEFILE) { $lastCommit = (Get-Content $STATEFILE -Raw).Trim() }
  35. if ($latestCommit -eq $lastCommit) {
  36. Log "No changes (commit $($latestCommit.Substring(0,8))). Nothing to do."
  37. exit 0
  38. }
  39. Log "New commit detected: $($latestCommit.Substring(0,8)). Deploying..."
  40. # Download archive
  41. $archiveUrl = "$GITEA/api/v1/repos/$REPO/archive/master.zip"
  42. $zipPath = "C:\Scripts\aspblog-deploy.zip"
  43. try {
  44. $wc = New-Object System.Net.WebClient
  45. $wc.Headers.Add("Authorization", "token $TOKEN")
  46. $wc.DownloadFile($archiveUrl, $zipPath)
  47. Log "Downloaded archive."
  48. } catch {
  49. Log "ERROR downloading archive: $_"
  50. exit 1
  51. }
  52. # Extract to temp folder
  53. $tmpDir = "C:\Scripts\aspblog-tmp"
  54. if (Test-Path $tmpDir) { Remove-Item $tmpDir -Recurse -Force }
  55. New-Item -ItemType Directory -Path $tmpDir | Out-Null
  56. Add-Type -AssemblyName System.IO.Compression.FileSystem
  57. [System.IO.Compression.ZipFile]::ExtractToDirectory($zipPath, $tmpDir)
  58. # Find the extracted subfolder (Gitea puts files in a subfolder)
  59. $extracted = Get-ChildItem $tmpDir -Directory | Select-Object -First 1
  60. if (-not $extracted) { Log "ERROR: No directory found in archive."; exit 1 }
  61. $srcPath = $extracted.FullName
  62. Log "Extracted to $srcPath"
  63. # Copy public/ folder contents to webroot (this is the IIS-served content)
  64. $publicSrc = Join-Path $srcPath "public"
  65. if (Test-Path $publicSrc) {
  66. # Copy all files from public/ to webroot root
  67. Copy-Item "$publicSrc\*" $WEBROOT -Recurse -Force
  68. Log "Copied public/ to webroot."
  69. } else {
  70. # If no public folder, copy everything
  71. Copy-Item "$srcPath\*" $WEBROOT -Recurse -Force
  72. Log "Copied all files to webroot."
  73. }
  74. # Copy core/, app/ alongside Default.asp (needed for ../core/ relative includes)
  75. foreach ($folder in @("core", "app", "db")) {
  76. $folderSrc = Join-Path $srcPath $folder
  77. $folderDst = Join-Path $WEBROOT $folder
  78. if (Test-Path $folderSrc) {
  79. if (Test-Path $folderDst) { Remove-Item $folderDst -Recurse -Force }
  80. Copy-Item $folderSrc $WEBROOT -Recurse -Force
  81. Log "Copied $folder/ to webroot."
  82. }
  83. }
  84. # Update web.config ConnectionString to point to correct DB path
  85. $webConfigPath = Join-Path $WEBROOT "web.config"
  86. if (Test-Path $webConfigPath) {
  87. $wc = [System.IO.File]::ReadAllText($webConfigPath)
  88. $newConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$WEBROOT\db\webdata.accdb;Persist Security Info=False;"
  89. $wc = [regex]::Replace($wc, 'Provider=Microsoft\.ACE\.OLEDB[^"]+', $newConn)
  90. [System.IO.File]::WriteAllText($webConfigPath, $wc)
  91. Log "Updated web.config ConnectionString."
  92. }
  93. # Grant IIS_IUSRS write access to db folder
  94. $dbPath = Join-Path $WEBROOT "db"
  95. if (Test-Path $dbPath) {
  96. $acl = Get-Acl $dbPath
  97. $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("IIS_IUSRS", "Modify", "ContainerInherit,ObjectInherit", "None", "Allow")
  98. $acl.SetAccessRule($rule)
  99. Set-Acl $dbPath $acl
  100. Log "Set IIS_IUSRS Modify on db folder."
  101. }
  102. # Recycle app pool
  103. Import-Module WebAdministration -ErrorAction SilentlyContinue
  104. try {
  105. Restart-WebAppPool $APPPOOL
  106. Log "Recycled app pool $APPPOOL."
  107. } catch {
  108. Log "WARNING: Could not recycle app pool: $_"
  109. }
  110. # Cleanup
  111. Remove-Item $tmpDir -Recurse -Force
  112. Remove-Item $zipPath -Force
  113. # Save new commit hash
  114. $latestCommit | Out-File -FilePath $STATEFILE -NoNewline
  115. Log "Deploy complete. Commit: $($latestCommit.Substring(0,8))"

Powered by TurnKey Linux.