-
Notifications
You must be signed in to change notification settings - Fork 0
66 lines (52 loc) · 1.82 KB
/
nodejs_deploy.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
name: Deploy Node.js App to EC2
on:
push:
branches:
- Aws_deploy
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install dependencies
run: npm install
- name: Build
run: npm run build
- name: Create .ssh directory
run: mkdir -p ~/.ssh
- name: Add SSH key to agent
run: |
echo "${{ secrets.SERVER_SSH_KEY }}" > key.pem
chmod 400 key.pem
eval "$(ssh-agent -s)"
ssh-add key.pem
- name: Add server to known hosts
run: ssh-keyscan ${{ secrets.SERVER_HOST }} >> ~/.ssh/known_hosts
- name: Test SSH connection
run: ssh -i key.pem -o StrictHostKeyChecking=no ${{ secrets.SERVER_USERNAME }}@${{ secrets.SERVER_HOST }} "echo 'SSH connection successful'"
- name: Deploy to server
run: |
ssh -i key.pem ${{ secrets.SERVER_USERNAME }}@${{ secrets.SERVER_HOST }} "\
sudo apt-get update -y && \
sudo apt-get upgrade -y && \
sudo apt-get autoremove -y && \
sudo apt-get autoclean && \
sudo apt-get -f install && \
sudo dpkg --configure -a && \
sudo apt-get purge -y nodejs npm && \
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash - && \
sudo apt-get install -y git nodejs || exit 1; \
cd ~ && \
if [ ! -d \"CICD-Github-Actions\" ]; then \
git clone https://github.com/Julen-Smith/CICD-Github-Actions.git || exit 1; \
fi && \
cd CICD-Github-Actions && \
git checkout Aws_deploy || exit 1 && \
npm install || exit 1 && \
sudo npm install -g pm2 || exit 1 && \
pm2 restart all || pm2 start app.js || exit 1"