-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy-angular.ps1
More file actions
131 lines (111 loc) · 4.25 KB
/
deploy-angular.ps1
File metadata and controls
131 lines (111 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# Deploy Angular Project Script
# Author: @dalthonmh
# Last update: 2026-01-11
# Allow execution: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# Run in PowerShell as administrator: .\deploy.ps1
Write-Host "========== Starting Angular Deployment ==========" -ForegroundColor Cyan
# CONFIGURATION VARIABLES
$PROJECT_NAME = "academico"
$PROJECT_PATH = "D:\app\academico"
$DEPLOY_PATH = "D:\www"
$NODE_VERSION = "24"
$BUILD_OUTPUT_DIR = "$DEPLOY_PATH\${PROJECT_NAME}-temp"
$ACTIVE_DIR = "$DEPLOY_PATH\$PROJECT_NAME"
$BACKUP_DIR = "$DEPLOY_PATH\${PROJECT_NAME}-old"
$LOG_FILE = "$DEPLOY_PATH\${PROJECT_NAME}.log"
# Change to project directory
Write-Host "Changing to project directory..." -ForegroundColor Yellow
if (-Not (Test-Path $PROJECT_PATH)) {
Write-Host "ERROR: Could not access $PROJECT_PATH" -ForegroundColor Red
exit 1
}
Set-Location $PROJECT_PATH
Write-Host "Synchronizing repository with Git..." -ForegroundColor Yellow
# Configure Git not to use persistent credentials on Windows Server
git config --global credential.helper manager-core
# Capture current commit hash (before pull)
$PREV_HASH = (git rev-parse HEAD 2>$null)
if (-Not $PREV_HASH) { $PREV_HASH = "(sin commits previos)" }
Write-Host "Current commit hash: $PREV_HASH" -ForegroundColor DarkGray
# Ensure we are on the main branch
git checkout main
if ($LASTEXITCODE -ne 0) {
Write-Host "ERROR: Could not switch to main branch" -ForegroundColor Red
exit 1
}
# Pull latest changes
git pull origin main
if ($LASTEXITCODE -ne 0) {
Write-Host "ERROR: git pull failed. Check your connection or credentials." -ForegroundColor Red
exit 1
}
# Capture new commit hash (after pull)
$NEW_HASH = (git rev-parse HEAD)
Write-Host "New commit hash: $NEW_HASH" -ForegroundColor DarkGray
# Change Node.js version
Write-Host "Configuring Node.js version $NODE_VERSION..." -ForegroundColor Yellow
nvm use $NODE_VERSION
if ($LASTEXITCODE -ne 0) {
Write-Host "ERROR: Could not switch to Node $NODE_VERSION" -ForegroundColor Red
exit 1
}
# Install dependencies
Write-Host "Installing dependencies with Yarn..." -ForegroundColor Yellow
yarn install
if ($LASTEXITCODE -ne 0) {
Write-Host "ERROR: yarn install failed" -ForegroundColor Red
exit 1
}
# Build Angular in temporary folder
Write-Host "Building Angular project..." -ForegroundColor Yellow
ng build --configuration production --output-path="$BUILD_OUTPUT_DIR"
if ($LASTEXITCODE -ne 0) {
Write-Host "ERROR: Angular build failed" -ForegroundColor Red
exit 1
}
# Backup active version if it exists
if (Test-Path $ACTIVE_DIR) {
Write-Host "Backing up current version..." -ForegroundColor Yellow
if (Test-Path $BACKUP_DIR) {
Remove-Item -Path $BACKUP_DIR -Recurse -Force
}
Move-Item -Path $ACTIVE_DIR -Destination $BACKUP_DIR -Force
if ($LASTEXITCODE -ne 0) {
Write-Host "ERROR: Could not backup current version" -ForegroundColor Red
exit 1
}
}
# Move new version to active directory
Write-Host "Activating new version..." -ForegroundColor Yellow
Move-Item -Path $BUILD_OUTPUT_DIR -Destination $ACTIVE_DIR -Force
if ($LASTEXITCODE -ne 0) {
Write-Host "ERROR: Could not activate new version" -ForegroundColor Red
# Restore backup if failed
if (Test-Path $BACKUP_DIR) {
Write-Host "Restoring previous version..." -ForegroundColor Yellow
Move-Item -Path $BACKUP_DIR -Destination $ACTIVE_DIR -Force
}
exit 1
}
# Remove old backup
if (Test-Path $BACKUP_DIR) {
Write-Host "Removing old backup..." -ForegroundColor Yellow
Remove-Item -Path $BACKUP_DIR -Recurse -Force
}
# ===== Write deploy log =====
$DEPLOY_DATE = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$LOG_ENTRY = @"
==================================================
Deploy Date : $DEPLOY_DATE
Previous Hash : $PREV_HASH
New Hash : $NEW_HASH
Branch : main
Project : $PROJECT_NAME
Deploy Path : $ACTIVE_DIR
==================================================
"@
# Append to log file (create if it doesn't exist)
Add-Content -Path $LOG_FILE -Value $LOG_ENTRY -Encoding UTF8
Write-Host "Deploy log saved to: $LOG_FILE" -ForegroundColor DarkGray
Write-Host "========== Deployment completed successfully ==========" -ForegroundColor Green
Write-Host "Project deployed at: $ACTIVE_DIR" -ForegroundColor Green