Skip to content

Commit 83ec488

Browse files
authored
Merge branch 'deployment' into temporary
2 parents f6d83e8 + 08d1281 commit 83ec488

28 files changed

+545
-317
lines changed

.github/workflows/main.yml

Lines changed: 39 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
branches: [main]
88

99
jobs:
10-
test:
10+
deploy:
1111
runs-on: ubuntu-latest
1212
services:
1313
docker:
@@ -25,29 +25,13 @@ jobs:
2525

2626
- name: Install dependencies for frontend
2727
working-directory: ./visual-compiler/frontend
28-
run: npm install
29-
30-
- name: Tests for frontend
31-
working-directory: ./visual-compiler/frontend
32-
run: npm run test -- --coverage
33-
34-
- name: Upload coverage for frontend
35-
uses: codecov/codecov-action@v4
36-
with:
37-
flags: frontend
38-
name: coverage-frontend
39-
directory: ./visual-compiler/frontend
40-
files: ./visual-compiler/frontend/coverage/lcov.info
28+
run: npm ci
4129

4230
- name: Go setup for Backend
4331
uses: actions/setup-go@v5
4432
with:
4533
go-version: '1.21'
4634

47-
- name: Linting for Frontend using ESLint
48-
working-directory: ./visual-compiler/frontend
49-
run: npm run lint -- --fix
50-
5135
- name: Go Modules Cache
5236
uses: actions/cache@v4
5337
with:
@@ -70,39 +54,6 @@ jobs:
7054
CUSTOM_API: ${{secrets.CUSTOM_API}}
7155
OPENAI_API_KEY: ${{secrets.OPENAI_API_KEY}}
7256

73-
- name: Tests for Backend
74-
working-directory: ./visual-compiler/backend
75-
env:
76-
Mongo_username: ${{ secrets.MONGODB_USERNAME }}
77-
Mongo_password: ${{secrets.MONGODB_PASSWORD}}
78-
Mongo_URI: ${{secrets.MONGODB_URI}}
79-
AUTH0_DOMAIN: ${{secrets.AUTH0_DOMAIN}}
80-
CLIENT_ID: ${{secrets.CLIENT_ID}}
81-
CLIENT_SECRET: ${{secrets.CLIENT_SECRET}}
82-
CUSTOM_API: ${{secrets.CUSTOM_API}}
83-
OPENAI_API_KEY: ${{secrets.OPENAI_API_KEY}}
84-
run: go test -tags=coverage -coverpkg=./... ./... -coverprofile=coverage.out
85-
86-
- name: Upload coverage for backend
87-
uses: codecov/codecov-action@v4
88-
with:
89-
flags: backend
90-
name: coverage-backend
91-
directory: ./visual-compiler/backend
92-
files: coverage.out
93-
94-
- name: Linting for Backend using Semgrep
95-
uses: returntocorp/semgrep-action@v1
96-
with:
97-
config: ./visual-compiler/backend/.semgrep-go.yml
98-
99-
e2e_test:
100-
runs-on: ubuntu-latest
101-
needs: test
102-
steps:
103-
- name: Checkout main code
104-
uses: actions/checkout@v4
105-
10657
- name: Docker Compose Installation
10758
run: |
10859
sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
@@ -120,49 +71,44 @@ jobs:
12071
echo "CUSTOM_API=${{secrets.CUSTOM_API}}" >> backend.env
12172
echo "OPENAI_API_KEY=${{secrets.OPENAI_API_KEY}}" >> backend.env
12273
123-
- name: Docker Build and Compose for deployment
124-
run: docker compose --env-file backend.env up -d --build
74+
- name: Docker images build
75+
run: |
76+
docker build -t vc-frontend:latest ./visual-compiler/frontend
77+
docker build -t vc-backend:latest ./visual-compiler/backend
12578
126-
- name: Making sure Backend is running
79+
- name: Save images as executable files
12780
run: |
128-
for i in {1..30}; do
129-
if curl -s http://localhost:8080/api/health > /dev/null; then
130-
echo "Backend is running"
131-
exit 0
132-
fi
133-
echo "Waiting..."
134-
sleep 5
135-
done
136-
echo "Backend failed"
137-
exit 1
138-
- name: Making sure Frontend is running
81+
docker save vc-frontend:latest -o frontend.tar
82+
docker save vc-backend:latest -o backend.tar
83+
84+
- name: Setting up SSH for EC2 instance
13985
run: |
140-
for i in {1..30}; do
141-
if curl -s http://localhost:5173 > /dev/null; then
142-
echo "Frontend is running"
143-
exit 0
144-
fi
145-
echo "Waiting..."
146-
sleep 5
147-
done
148-
echo "Frontend failed"
149-
exit 1
150-
151-
- name: Cypress Installation
152-
working-directory: ./visual-compiler
153-
run: npm install cypress --save-dev
154-
155-
- name: Run Cypress
156-
working-directory: ./visual-compiler
157-
run: npx cypress run
158-
159-
- name: Cypress screenshots upload
160-
if: failure()
161-
uses: actions/upload-artifact@v4
162-
with:
163-
name: cypress-screenshots
164-
path: visual-compiler/e2e-tests/screenshots
86+
mkdir -p ~/.ssh
87+
echo "${{secrets.EC2_SSH_KEY}}" > ~/.ssh/id_rsa
88+
chmod 600 ~/.ssh/id_rsa
89+
ssh-keyscan -H ${{secrets.EC2_HOST}} >> ~/.ssh/known_hosts
16590
166-
- name: Docker Compose Shut down
167-
if: always()
168-
run: docker compose down
91+
- name: Copy image to instance
92+
run: |
93+
rsync -az -e "ssh -i ~/.ssh/id_rsa" frontend.tar backend.tar docker-compose.yml backend.env ${{secrets.EC2_USER}}@${{secrets.EC2_HOST}}:/home/${{secrets.EC2_USER}}/app/
94+
95+
- name: SSH into instance and Compose
96+
run: |
97+
ssh -i ~/.ssh/id_rsa ${{secrets.EC2_USER}}@${{secrets.EC2_HOST}} << EOF
98+
cd ~/app
99+
100+
echo "Mongo_username=${{secrets.MONGODB_USERNAME}}" >> backend.env
101+
echo "Mongo_password=${{secrets.MONGODB_PASSWORD}}" >> backend.env
102+
echo "Mongo_URI=${{secrets.MONGODB_URI}}" >> backend.env
103+
echo "AUTH0_DOMAIN=${{secrets.AUTH0_DOMAIN}}" >> backend.env
104+
echo "CLIENT_ID=${{secrets.CLIENT_ID}}" >> backend.env
105+
echo "CLIENT_SECRET=${{secrets.CLIENT_SECRET}}" >> backend.env
106+
echo "CUSTOM_API=${{secrets.CUSTOM_API}}" >> backend.env
107+
echo "OPENAI_API_KEY=${{secrets.OPENAI_API_KEY}}" >> backend.env
108+
109+
docker load -i frontend.tar
110+
docker load -i backend.tar
111+
112+
docker-compose down || true
113+
docker-compose --env-file backend.env up -d
114+
EOF

docker-compose.yml

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,20 @@ version: "3.9"
22

33
services:
44
frontend:
5-
build:
6-
context: ./visual-compiler/frontend
7-
dockerfile: Dockerfile
5+
image: vc-frontend:latest
86
ports:
9-
- "5173:5173"
10-
volumes:
11-
- ./visual-compiler/frontend:/visual-compiler-frontend
12-
- /visual-compiler-frontend/node_modules
13-
command: npm run dev -- --host
7+
- "3000:3000"
8+
mem_limit: 256m
9+
cpus: 0.5
1410

1511
backend:
16-
build:
17-
context: ./visual-compiler/backend
18-
dockerfile: Dockerfile
12+
image: vc-backend:latest
1913
env_file:
2014
- backend.env
2115
ports:
2216
- "8080:8080"
17+
mem_limit: 256m
18+
cpus: 0.5
2319
volumes:
24-
- ./visual-compiler/backend:/visual-compiler-backend
25-
environment:
26-
- Mongo_username
27-
- Mongo_password
28-
- Mongo_URI
29-
command: go run main.go
20+
- ./visual-compiler/backend/.env:/visual-compiler-backend/backend.env:ro
21+
restart: unless-stopped

visual-compiler/backend/Dockerfile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
FROM golang:1.24.3
1+
FROM alpine:latest
22

33
WORKDIR /visual-compiler-backend
44

5-
COPY go.mod go.sum ./
5+
COPY backend .
66

7-
RUN go mod download
7+
RUN chmod +x ./backend
88

9-
COPY . .
9+
ENV GOGC=50
10+
ENV GOMAXPROCS=1
1011

1112
EXPOSE 8080
1213

13-
CMD [ "go", "run" , "main.go" ]
14+
CMD [ "./backend" ]

visual-compiler/backend/backend

31.8 MB
Binary file not shown.

visual-compiler/backend/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package main
66

77
import (
8+
"io"
89
"log"
910
"net/http"
1011
"os"
@@ -26,14 +27,17 @@ import (
2627
// @host localhost:8080
2728
// @BasePath /api
2829
func main() {
30+
log.SetOutput(io.Discard)
31+
gin.SetMode(gin.ReleaseMode)
32+
2933
db.ConnectClient()
3034

3135
// Set up Gin engine
3236
router := gin.Default()
3337

3438
// Attach CORS middleware
3539
router.Use(cors.New(cors.Config{
36-
AllowOrigins: []string{"http://localhost:5173", "http://127.0.0.1:5173", "https://visual-compiler.co.za"},
40+
AllowOrigins: []string{"http://localhost:5173", "http://127.0.0.1:5173", "https://visual-compiler.co.za", "http://localhost:3000", "http://127.0.0.1:3000"},
3741
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
3842
AllowHeaders: []string{"Origin", "Content-Type", "Accept", "Authorization"},
3943
ExposeHeaders: []string{"Content-Length"},

0 commit comments

Comments
 (0)