-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.ps1
73 lines (67 loc) · 2.19 KB
/
deploy.ps1
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
# Mimic CI/CD pipeline to deploy project to remote host
#
# To run this script, you should have PowerShell 7.0 or later installed.
# You can download it at https://github.com/PowerShell/PowerShell/releases
#
# To auto-deploy to remote host, follow these steps:
#
# 1. Create a .env file with the following content:
# ```
# username@hostname
# /path/to/deploy
# app-name
# build folder
# ```
# - `username@hostname` can also be the connection name in your SSH config file.
# - `app-name` is the name of the app folder on the remote host, thus the final path
# will be `/path/to/deploy/app-name`.
# - `build folder` is the folder to be deployed, e.g. `dist` for npm projects.
#
# 2. Run this script under project root directory.
#
# Read first and second line from .env file and raise error if not found
$envFile = Get-Content .env
$hostConnection = $envFile[0]
if (-not $hostConnection) {
Write-Host "Error: hostConnection not found in .env file" -ForegroundColor Red
exit 1
}
$deployPath = $envFile[1]
if (-not $deployPath) {
Write-Host "Error: deployPath not found in .env file" -ForegroundColor Red
exit 1
}
$tempPath = "$deployPath/tmp"
$app = $envFile[2]
if (-not $app) {
Write-Host "Error: app not found in .env file" -ForegroundColor Red
exit 1
}
$build = $envFile[3]
if (-not $build) {
Write-Host "Error: build not found in .env file" -ForegroundColor Red
exit 1
}
# Build project
Write-Host "Building project"
./build.ps1
# Pack build folder into tar.gz
Write-Host "Packing build folder into tar.gz"
$randomString = -join ((65..90) + (97..122) | Get-Random -Count 10 | % {[char]$_})
$archiveName = "$randomString.tar.gz"
tar -czf $archiveName $build
# Copy archive to remote host
Write-Host "Copying archive to remote host"
scp $archiveName ${hostConnection}:/tmp/$archiveName
# Execute remote commands:
Write-Host "Executing deploy commands"
$commands= @(
" rm -rf $tempPath/$build $deployPath/$app",
" && tar -xzf /tmp/$archiveName -C $tempPath --overwrite",
" && mv $tempPath/$build $deployPath/$app",
" && rm /tmp/$archiveName"
)
ssh $hostConnection $commands
# Delete archive
Write-Host "Finishing up"
Remove-Item $archiveName