-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.ps1
More file actions
58 lines (53 loc) · 1.85 KB
/
deploy.ps1
File metadata and controls
58 lines (53 loc) · 1.85 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
# Mimic CI/CD pipeline to deploy Java application 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 Java application to remote host, follow these steps:
#
# 1. Create a .env file with hostConnection and deployPath
# Example:
# ```
# username@hostname
# /path/to/deploy
# ```
# It can also be configured in your .ssh profile. hostConnection is
# what follows ssh or scp command.
#
# 2. Build Java application.
# In IntelliJ IDEA, go to maven tab and run 'install' in 'Lifecycle'.
# This will create a .jar file in target/ directory.
# Make sure to keep only the latest .jar file in target/ directory.
#
# 3. Run this script under project root directory.
#
# Pack target/*.jar and Dockerfile into tar.gz
Write-Host "Packing target/*.jar and Dockerfile into tar.gz"
$randomString = -join ((65..90) + (97..122) | Get-Random -Count 10 | % { [char]$_ })
$archiveName = "$randomString.tar.gz"
tar -czf $archiveName target/*.jar Dockerfile
# Read first and second line from .env file and raise error if not found
$envFile = Get-Content .env
$hostConnection = $envFile[0]
$deployPath = $envFile[1]
if (-not $hostConnection -or -not $deployPath)
{
Write-Host "Error: .env file is missing hostConnection or deployPath"
exit 1
}
# 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 = @(
"buaalogin; ",
"tar -xzf /tmp/$archiveName -C $deployPath --overwrite",
" && rm /tmp/$archiveName",
" && cd $deployPath",
" && ./deploy.sh"
)
ssh $hostConnection $commands
# Delete archive
Write-Host "Finishing up"
Remove-Item $archiveName