-
Notifications
You must be signed in to change notification settings - Fork 0
144 lines (122 loc) · 5.56 KB
/
deployapp.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
140
141
142
143
144
# name: Deploy React App to EC2
# on:
# push:
# branches:
# - main # Adjust this if you want to deploy from a different branch
# jobs:
# deploy:
# name: Deploy to EC2
# runs-on: ubuntu-latest
# steps:
# - name: Checkout code
# uses: actions/checkout@v3
# - name: Set up Node.js
# uses: actions/setup-node@v3
# with:
# node-version: '18.x' # Match the node version in your package.json
# - name: Install dependencies
# run: npm install
# - name: Build the React app
# run: npm run build
# - name: Upload build artifacts
# uses: actions/upload-artifact@v3
# with:
# name: build-artifact
# path: build/
# - name: Download build artifact
# uses: actions/download-artifact@v3
# with:
# name: build-artifact
# - name: Deploy to EC2
# env:
# EC2_HOST: ${{ secrets.EC2_HOST }} # Add your EC2 public IP or domain as a secret in GitHub
# EC2_USER: ${{ secrets.EC2_USER }} # Add your EC2 user (e.g., 'ec2-user') as a secret in GitHub
# EC2_KEY: ${{ secrets.EC2_KEY }} # Add your private key for SSH as a secret in GitHub
# run: |
# echo "${{ secrets.EC2_KEY }}" > ~/ec2-key.pem
# chmod 600 ~/ec2-key.pem
# # Transfer build artifacts and SSH key to bastion host
# ssh -o StrictHostKeyChecking=no -i ~/ec2-key.pem ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} "mkdir /tmp/react"
# scp -o StrictHostKeyChecking=no -i ~/ec2-key.pem -r build/* ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }}:/tmp/react
# scp -o StrictHostKeyChecking=no -i ~/ec2-key.pem ~/ec2-key.pem ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }}:/tmp/react
# # SSH into bastion host and transfer to private instance
# ssh -o StrictHostKeyChecking=no -i ~/ec2-key.pem ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF'
# ssh -o StrictHostKeyChecking=no -i /tmp/react/ec2-key.pem ${{ secrets.EC2_USER }}@${{ secrets.EC2_PVT_HOST }} "mkdir /tmp/react"
# scp -o StrictHostKeyChecking=no -i /tmp/react/ec2-key.pem -r /tmp/react/* ${{ secrets.EC2_USER }}@${{ secrets.EC2_PVT_HOST }}:/tmp/react
# ssh -o StrictHostKeyChecking=no -i /tmp/react/ec2-key.pem ${{ secrets.EC2_USER }}@${{ secrets.EC2_PVT_HOST }} << 'INNER_EOF'
# sudo cp -r /tmp/react/* /var/www/html/
# sudo systemctl restart nginx
# INNER_EOF
# rm -f /tmp/ec2-key.pem
# EOF
# name: Deploy React App to EC2
# on:
# push:
# branches:
# - main # Adjust this if you want to deploy from a different branch
# jobs:
# deploy:
# name: Deploy to EC2
# runs-on: ubuntu-latest
# steps:
# - name: Checkout code
# uses: actions/checkout@v3
# - name: Set up Node.js
# uses: actions/setup-node@v3
# with:
# node-version: '18.x' # Match the node version in your package.json
# - name: Upload source code
# uses: actions/upload-artifact@v3
# with:
# name: source-code
# path: ./
# - name: Download source code
# uses: actions/download-artifact@v3
# with:
# name: source-code
# - name: Deploy to EC2 and start server
# env:
# EC2_HOST: ${{ secrets.EC2_HOST }} # Public IP or domain of your EC2 instance
# EC2_USER: ${{ secrets.EC2_USER }} # User to SSH into EC2 instance (e.g., 'ec2-user')
# EC2_KEY: ${{ secrets.EC2_KEY }} # SSH private key as a secret in GitHub
# EC2_PVT_HOST: ${{ secrets.EC2_PVT_HOST }} # Private IP or hostname of the EC2 instance if behind a bastion
# run: |
# echo "${{ secrets.EC2_KEY }}" > ~/ec2-key.pem
# chmod 600 ~/ec2-key.pem
# # Transfer source code to EC2
# ssh -o StrictHostKeyChecking=no -i ~/ec2-key.pem ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} "mkdir -p /tmp/react-app"
# scp -o StrictHostKeyChecking=no -i ~/ec2-key.pem ~/ec2-key.pem ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }}:/tmp/react-app
# scp -o StrictHostKeyChecking=no -i ~/ec2-key.pem -r ./* ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }}:/tmp/react-app
# ssh -o StrictHostKeyChecking=no -i ~/ec2-key.pem ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF'
# # ssh -o StrictHostKeyChecking=no -i /tmp/react-app/ec2-key.pem ${{ secrets.EC2_USER }}@${{ secrets.EC2_PVT_HOST }} "mkdir -p /tmp/react-app"
# scp -v -i /tmp/react-app/ec2-key.pem -r /tmp/react-app/* ${{ secrets.EC2_USER }}@${{ secrets.EC2_PVT_HOST }}:/tmp/react-app 2>&1 | tee /tmp/scp_error.log
# ssh -o StrictHostKeyChecking=no -i /tmp/react-app/ec2-key.pem ${{ secrets.EC2_USER }}@${{ secrets.EC2_PVT_HOST }} << 'INNER_EOF'
# echo "Hello world"
# if pgrep -f "docusaurus serve"; then
# sudo pkill -f "docusaurus serve"
# fi
# cd /tmp/react-app
# sudo yarn install
# sudo yarn build
# nohup sudo yarn serve > /dev/null 2>&1 &
# exit
# INNER_EOF
# EOF
name: Build
on:
push:
branches:
- main
jobs:
build:
name: Build
runs-on: ubuntu-latest
permissions: read-all
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- uses: sonarsource/sonarqube-scan-action@master
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST }}