feat: add guarded GitHub Pages deployment
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
param(
|
||||
[string]$Repository = 'https://github.com/DD4SN/DD4SN.github.io.git'
|
||||
)
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
|
||||
$projectRoot = Split-Path -Parent $PSScriptRoot
|
||||
$siteSource = Join-Path $projectRoot 'pages-dist'
|
||||
$workflowSource = Join-Path $projectRoot 'pages/deploy.yml'
|
||||
$temporaryRoot = Join-Path ([System.IO.Path]::GetTempPath()) ("holodori-pages-{0}" -f [guid]::NewGuid().ToString('N'))
|
||||
$checkout = Join-Path $temporaryRoot 'repository'
|
||||
|
||||
try {
|
||||
& npm --prefix $projectRoot run build:pages
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Pages build failed.' }
|
||||
|
||||
New-Item -ItemType Directory -Path $temporaryRoot | Out-Null
|
||||
& git clone $Repository $checkout
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Pages repository clone failed.' }
|
||||
|
||||
$resolvedCheckout = (Resolve-Path -LiteralPath $checkout).Path
|
||||
$resolvedTemporaryRoot = (Resolve-Path -LiteralPath $temporaryRoot).Path
|
||||
if (-not $resolvedCheckout.StartsWith($resolvedTemporaryRoot, [System.StringComparison]::OrdinalIgnoreCase)) {
|
||||
throw 'Refusing to update a checkout outside the temporary deployment directory.'
|
||||
}
|
||||
|
||||
$siteTarget = Join-Path $resolvedCheckout 'site'
|
||||
if (Test-Path -LiteralPath $siteTarget) {
|
||||
Remove-Item -LiteralPath $siteTarget -Recurse -Force
|
||||
}
|
||||
New-Item -ItemType Directory -Path $siteTarget | Out-Null
|
||||
Copy-Item -Path (Join-Path $siteSource '*') -Destination $siteTarget -Recurse -Force
|
||||
New-Item -ItemType File -Path (Join-Path $siteTarget '.nojekyll') -Force | Out-Null
|
||||
|
||||
$workflowTarget = Join-Path $resolvedCheckout '.github/workflows'
|
||||
New-Item -ItemType Directory -Path $workflowTarget -Force | Out-Null
|
||||
Copy-Item -LiteralPath $workflowSource -Destination (Join-Path $workflowTarget 'deploy.yml') -Force
|
||||
|
||||
Push-Location $resolvedCheckout
|
||||
try {
|
||||
& git add --all -- site .github/workflows/deploy.yml
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Failed to stage Pages files.' }
|
||||
& git diff --cached --quiet
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-Output 'GitHub Pages output is already up to date.'
|
||||
return
|
||||
}
|
||||
|
||||
$package = Get-Content -LiteralPath (Join-Path $projectRoot 'package.json') -Raw | ConvertFrom-Json
|
||||
& git commit -m ("deploy: HolodoriCalc v{0}" -f $package.version)
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Pages commit failed.' }
|
||||
& git push origin HEAD:main
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Pages push failed.' }
|
||||
} finally {
|
||||
Pop-Location
|
||||
}
|
||||
} finally {
|
||||
if (Test-Path -LiteralPath $temporaryRoot) {
|
||||
$resolvedTemporaryRoot = (Resolve-Path -LiteralPath $temporaryRoot).Path
|
||||
$systemTemporaryRoot = [System.IO.Path]::GetFullPath([System.IO.Path]::GetTempPath()).TrimEnd('\')
|
||||
if ($resolvedTemporaryRoot.StartsWith($systemTemporaryRoot, [System.StringComparison]::OrdinalIgnoreCase) -and
|
||||
(Split-Path -Leaf $resolvedTemporaryRoot).StartsWith('holodori-pages-')) {
|
||||
Remove-Item -LiteralPath $resolvedTemporaryRoot -Recurse -Force
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user