-
Notifications
You must be signed in to change notification settings - Fork 0
139 lines (138 loc) · 6.89 KB
/
standalone-activate-pm2.yml
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
131
132
133
134
135
136
137
138
139
on:
workflow_call:
inputs:
deployTo:
description: Directory where to deploy to (if empty that home dir is used)
type: string
required: false
ecosystemFile:
description: Override for which file to use for ecosystem.config.js
type: string
required: false
default: 'ecosystem.config.js'
applicationSuffixId:
description: Suffix used to differentiate between applications. This is meant to be used when a single deployment to a single environment requires multiple builds (i.e. b2c + b2b). Defaults to `main`.
type: string
required: false
default: "main"
scaleApplications:
description: Scale applications
type: boolean
required: false
default: true
updatePm2:
description: Invoke pm2 update after activation
type: boolean
required: false
default: true
deleteOldApplications:
description: Delete old applications
type: boolean
required: false
default: true
environment:
description: The github environment to inherit secrets from
type: string
required: true
serverHost:
type: string
required: true
serverPort:
type: string
required: true
serverUser:
type: string
required: true
additionalPath:
description: Additional paths that should be prefixed to PATH
type: string
required: false
default: '/data/web/.npm/bin/'
jobs:
activate-applications:
name: Activate applications
runs-on: ubuntu-latest
environment: ${{ inputs.environment }}
steps:
- name: Activate
uses: appleboy/ssh-action@master
env:
GITHUB_SHA: ${{ github.sha }}
with:
host: ${{ inputs.serverHost }}
username: ${{ inputs.serverUser }}
key: ${{ secrets.SSH_KEY }}
port: ${{ inputs.serverPort }}
envs: GITHUB_SHA,DOMAIN
script_stop: true
command_timeout: 30m
script: |
export PATH="${{ inputs.additionalPath }}:$PATH"
cp ${{ inputs.deployTo }}./graphcommerce${{ inputs.applicationSuffixId }}/current/${{ inputs.ecosystemFile }} ${{ inputs.deployTo }}ecosystem.config.js
cd ${{ inputs.deployTo }}
export NODE_OPTIONS=--max_old_space_size=2048
pm2 reload ecosystem.config.js
# #######################
# need to scale up/down if needed. Will execute for example: pm2 scale graphcommerceb2b 10
if [[ -f "github_run.sh" ]]; then
rm github_run.sh
fi
# $(node -pe 'JSON.stringify(eval(process.argv[1]).apps)' "$(cat ecosystem.config.js)" # This makes it a json from ecosystem.config.js
# jr is a json process command
# jq -r '.[] takes all arrays from json
# {name, instances} # take all indices called “name” and “instances”
# (.name|tostring) # print “name”
# (.instances|tostring)') # print “instances”
if [[ "${{ inputs.scaleApplications }}" == "true" ]]; then
echo "Scaling applications"
echo $'export toScale=$(node -pe \'JSON.stringify(eval(process.argv[1]).apps)\' "$(cat ecosystem.config.js)" | jq -r \'.[] | {name, instances} | "pm2 scale " + (.name|tostring) + " " + (.instances|tostring)\')' >> github_run.sh
echo $'SAVEIFS=$IFS' >> github_run.sh # Save current IFS (Internal Field Separator)
echo "IFS=$'\n'" >> github_run.sh # Change IFS to newline char
echo $'toScale=($toScale)' >> github_run.sh # split the string into an array by the same name
echo $'IFS=$SAVEIFS' >> github_run.sh # Restore original IFS
# need to run a for loop in a separate bash script because of bug: https://github.com/appleboy/drone-ssh/issues/175
echo 'for i in "${toScale[@]}"' >> github_run.sh
echo 'do' >> github_run.sh
echo ' echo "$i"' >> github_run.sh
echo ' $($i) || echo ""' >> github_run.sh
echo 'done' >> github_run.sh
chmod +x github_run.sh
./github_run.sh
rm github_run.sh
fi
# #######################
# get a list of applications that are running and that are in the ecosystem.config.js. Find the diff and then remove the old graphcommerce applications.
# $(node -pe 'JSON.stringify(eval(process.argv[1]).apps)' "$(cat ecosystem.config.js)" # This makes it a json from ecosystem.config.js
# sort | grep '^graphcommerce') # removes everything not starting with “graphcommerce”
# pm2 list #list all running applications
# sed -e '1,3d' # remove first 3 lines
# sed -e '$ d' # remove last line
# awk '{print $4}' # print 4th parameter
# sort | uniq | grep '^graphcommerce' # sort, remove duplicates, and only remove everything not starting with “graphcommerce”
if [[ "${{ inputs.deleteOldApplications }}" == "true" ]]; then
echo "Deleting old applications"
echo $'export toDelete=$(comm -13 <(node -pe \'JSON.stringify(eval(process.argv[1]).apps)\' "$(cat ecosystem.config.js)" | jq -r \'.[] | {name, instances} | (.name|tostring)\' | sort | grep \'^graphcommerce\') <(pm2 list | sed -e \'1,3d\' | sed -e \'$ d\' | awk \'{print $4}\' | sort | uniq | grep \'^graphcommerce\'))' >> github_run.sh
echo $'SAVEIFS=$IFS' >> github_run.sh # Save current IFS (Internal Field Separator)
echo "IFS=$'\n'" >> github_run.sh # Change IFS to newline char
echo $'toDelete=($toDelete)' >> github_run.sh # split the string into an array by the same name
echo $'IFS=$SAVEIFS' >> github_run.sh # Restore original IFS
echo $'if [[ "$toDelete" ]] ; then' >> github_run.sh
# need to run a for loop in a separate bash script because of bug: https://github.com/appleboy/drone-ssh/issues/175
echo 'for i in "${toDelete[@]}"' >> github_run.sh
echo 'do' >> github_run.sh
echo ' echo "pm2 delete $i"' >> github_run.sh
echo ' pm2 delete $i || echo ""' >> github_run.sh
echo 'done' >> github_run.sh
echo $'fi' >> github_run.sh
touch github_run.sh
chmod +x github_run.sh
./github_run.sh
rm github_run.sh
fi
# #######################
# Force reload of in-memory PM2 with local version of PM2, this is needed when deployment path and other settings change
if [[ "${{ inputs.updatePm2 }}" == "true" ]]; then
pm2 update
fi
# Save current process state so processes get restored across reboots
pm2 save