Skip to content

Commit

Permalink
Merge pull request #35 from JuribaDev/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
JuribaDev authored Aug 31, 2024
2 parents 097c7b0 + 3cd25d7 commit 4cac1b9
Show file tree
Hide file tree
Showing 117 changed files with 22,120 additions and 12,379 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LOCAL_MONGODB_URI=mongodb://admin:password@localhost:27017/dev_database?authSource=admin

JWT_SECRET=your_jwt_secret_key_here
JWT_EXPIRATION=8h

PORT=3000
103 changes: 103 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
name: Build and Deploy Docker Images

on:
push:
branches:
- main

jobs:
build-and-push:
runs-on: ubuntu-latest

permissions:
contents: 'read'
id-token: 'write'

env:
LOCATION: me-central1


steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v2
with:
credentials_json: '${{ secrets.GCP_CREDENTIALS }}'

- name: Set up Google Cloud SDK
uses: google-github-actions/setup-gcloud@v2
with:
project_id: ${{ secrets.GCP_PROJECT_ID }}

- name: Configure Docker for Artifact Registry
run: |
gcloud auth configure-docker ${{env.LOCATION}}-docker.pkg.dev
- name: Build and push Docker image for client
run: |
docker build \
--build-arg BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') \
--build-arg GITHUB_SHA=${{ github.sha }} \
--build-arg GITHUB_REF_NAME=${{ github.ref_name }} \
-f apps/client/Dockerfile \
-t ${{env.LOCATION}}-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.REPO_NAME }}/client:${{ github.sha }}-${{ github.ref_name }} \
.
docker push ${{env.LOCATION}}-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.REPO_NAME }}/client:${{ github.sha }}-${{ github.ref_name }}
- name: Build and push Docker image for server
run: |
docker build \
--build-arg BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') \
--build-arg GITHUB_SHA=${{ github.sha }} \
--build-arg GITHUB_REF_NAME=${{ github.ref_name }} \
-f apps/server/Dockerfile \
-t ${{env.LOCATION}}-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.REPO_NAME }}/server:${{ github.sha }}-${{ github.ref_name }} \
.
docker push ${{env.LOCATION}}-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.REPO_NAME }}/server:${{ github.sha }}-${{ github.ref_name }}
- name: Deploy Server to Cloud Run
id: deploy-server
uses: google-github-actions/deploy-cloudrun@v2
with:
service: ${{ secrets.SERVER_SERVICE }}
region: ${{env.LOCATION}}
image: ${{env.LOCATION}}-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.REPO_NAME }}/server:${{ github.sha }}-${{ github.ref_name }}
env_vars: |
JWT_SECRET=${{ secrets.JWT_KEY_SECRET }}
PROD_MONGODB_URI=${{ secrets.MONGODB_URI_SECRET }}
TOKEN_EXPIRATION=${{ secrets.JWT_EXPIRATION }}
flags: |
--port 3000
--memory 512Mi
--cpu 1
--max-instances 3
--min-instances 1
--concurrency 80
--ingress=internal
timeout: 600s

- name: Deploy Client to Cloud Run
id: deploy-client
uses: google-github-actions/deploy-cloudrun@v2
with:
service: ${{ secrets.CLIENT_SERVICE }}
region: ${{env.LOCATION}}
image: ${{env.LOCATION}}-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.REPO_NAME }}/client:${{ github.sha }}-${{ github.ref_name }}
env_vars: |
API_URL=${{ steps.deploy-server.outputs.url }}
flags: |
--port 80
--memory 256Mi
--cpu 1
--max-instances 3
--min-instances 1
--concurrency 100
--allow-unauthenticated
--ingress=internal-and-cloud-load-balancing
timeout: 600s
27 changes: 0 additions & 27 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,3 @@ jobs:
name: test-coverage
path: 'coverage/apps/**'

e2e:
runs-on: ubuntu-latest
needs: lint-and-build
services:
mongodb:
image: mongo:4.4
ports:
- 27017:27017
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- run: npm ci --legacy-peer-deps
- name: Install Xvfb
run: sudo apt-get update && sudo apt-get install -y xvfb
- name: Run E2E Tests
run: |
xvfb-run --auto-servernum npx nx run server:serve &
sleep 10 # Give the server some time to start
xvfb-run --auto-servernum npx nx run-many --target=e2e --all
- uses: actions/upload-artifact@v3
with:
name: e2e-results
path: cypress/videos/**

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ testem.log
# System Files
.DS_Store
Thumbs.db

.env
.nx/cache
.nx/workspace-data

Expand Down
10 changes: 0 additions & 10 deletions apps/client-e2e/.eslintrc.json

This file was deleted.

17 changes: 0 additions & 17 deletions apps/client-e2e/cypress.config.ts

This file was deleted.

10 changes: 0 additions & 10 deletions apps/client-e2e/project.json

This file was deleted.

13 changes: 0 additions & 13 deletions apps/client-e2e/src/e2e/app.cy.ts

This file was deleted.

5 changes: 0 additions & 5 deletions apps/client-e2e/src/fixtures/example.json

This file was deleted.

1 change: 0 additions & 1 deletion apps/client-e2e/src/support/app.po.ts

This file was deleted.

35 changes: 0 additions & 35 deletions apps/client-e2e/src/support/commands.ts

This file was deleted.

17 changes: 0 additions & 17 deletions apps/client-e2e/src/support/e2e.ts

This file was deleted.

24 changes: 0 additions & 24 deletions apps/client-e2e/tsconfig.json

This file was deleted.

50 changes: 39 additions & 11 deletions apps/client/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
# Build stage
FROM docker.io/node:lts-alpine3.20 AS build
FROM node:lts-alpine AS build
WORKDIR /app

RUN apk add --no-cache python3 make g++

COPY package*.json ./

RUN npm ci

COPY . .
RUN npm run build -- --prod

# Production stage
RUN npx nx build client --configuration=production

FROM nginx:stable-alpine
COPY --from=build /app/dist/client /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf
RUN chown -R nginx:nginx /usr/share/nginx/html && chmod -R 755 /usr/share/nginx/html && \
chown -R nginx:nginx /var/cache/nginx && \
chown -R nginx:nginx /var/log/nginx && \
chown -R nginx:nginx /etc/nginx/conf.d

ARG GITHUB_SHA
ARG BUILD_DATE
ARG GITHUB_REF_NAME
LABEL org.opencontainers.image.title="R&D Platform Client"
LABEL org.opencontainers.image.source="https://github.com/JuribaDev/rnd-platform"
LABEL org.opencontainers.image.revision=${GITHUB_SHA}
LABEL org.opencontainers.image.created=${BUILD_DATE}
LABEL org.opencontainers.image.version=${GITHUB_REF_NAME}
LABEL maintainer="Juriba Saleh [email protected]"

COPY --from=build /app/dist/apps/client/browser /usr/share/nginx/html

COPY apps/client/nginx.conf /etc/nginx/nginx.conf

RUN apk add --no-cache gettext

COPY apps/client/env.sh /docker-entrypoint.d/40-env.sh

RUN chmod +x /docker-entrypoint.d/40-env.sh

RUN chown -R nginx:nginx /usr/share/nginx/html && \
chmod -R 755 /usr/share/nginx/html && \
chown -R nginx:nginx /var/cache/nginx && \
chown -R nginx:nginx /var/log/nginx && \
chown -R nginx:nginx /etc/nginx/conf.d

RUN touch /var/run/nginx.pid && \
chown -R nginx:nginx /var/run/nginx.pid
chown -R nginx:nginx /var/run/nginx.pid

USER nginx

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
25 changes: 25 additions & 0 deletions apps/client/env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh

set -e

log() {
echo "[INFO] $1"
}

if [ -z "$API_URL" ]; then
echo "[ERROR] API_URL environment variable is not set."
exit 1
fi

log "Replacing API_URL in main JavaScript files"

find /usr/share/nginx/html/ -name 'main*.js' -exec sed -i 's|apiUrl:[^,]*|apiUrl:"'"${API_URL}"'"|g' {} +

log "API_URL successfully replaced in JavaScript files"

log "Verifying changes in JavaScript files"
for file in /usr/share/nginx/html/main*.js; do
head -n 5 "$file"
done

log "Script completed successfully"
Loading

0 comments on commit 4cac1b9

Please sign in to comment.