Skip to content

Commit

Permalink
chore: update project settings and dependencies
Browse files Browse the repository at this point in the history
feat: add dockerization and CI/CD pipeline

refactor: improve code quality and error handling

docs: update documentation and project description
  • Loading branch information
shba007 committed Dec 16, 2024
1 parent 60f20fa commit a8798c9
Show file tree
Hide file tree
Showing 22 changed files with 8,395 additions and 9,921 deletions.
11 changes: 11 additions & 0 deletions .changeset/tiny-kids-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'unai-api': patch
---

chore: update project settings and dependencies

feat: add dockerization and CI/CD pipeline

refactor: improve code quality and error handling

docs: update documentation and project description
25 changes: 25 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Nitro dev/build outputs
.data
.output
.nitro
.cache
dist

# Node dependencies
node_modules

# Logs
logs
*.log

# Misc
.DS_Store
.fleet
.idea

# Local env files
.env
.env.*

.dockerignore
Dockerfile
2 changes: 0 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
PORT=
PRESET=
FIREBASE_CONFIG=
STORAGE_BUCKET=
QDRANT_URL=
Expand Down
135 changes: 99 additions & 36 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,60 +1,123 @@
name: Nitro CD
run-name: ${{ github.ref_name }}

on:
release:
types: [published]
push:
tags:
- 'v*.*.*'

concurrency: ${{ github.workflow }}-${{ github.ref }}
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
REPO: ${{ github.repository }}
REGISTRY: ${{ vars.REGISTRY }}
USERNAME: ${{ vars.USERNAME }}
PORT: ${{ vars.PORT }}
NODE_ENV: production
NITRO_PRESET: ${{ vars.NITRO_PRESET }}

jobs:
build:
runs-on: ubuntu-24.04
runs-on: ubuntu-latest

permissions:
contents: read
packages: write
attestations: write
id-token: write

outputs:
image_name: ${{ steps.metadata.outputs.image_name }}
image_title: ${{ steps.metadata.outputs.image_title }}
buildtime: ${{ steps.metadata.outputs.buildtime }}
version: ${{ steps.metadata.outputs.version }}

steps:
- name: Checkout Repo
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
node-version: '20.x'
cache: 'npm'
registry: ${{ env.REGISTRY }}
username: ${{ env.USERNAME }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Install Dependencies
run: npm ci
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.REPO }}
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha,prefix=sha-,format=short
- name: Bulid Artifacts
run: npm run build
- name: Set tags as environment variable
id: metadata
run: |
echo "Setting metadata to env variables"
echo "image_name=${{ fromJSON(steps.meta.outputs.json).tags[0] }}" >> $GITHUB_OUTPUT
echo "image_title=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.title'] }}" >> $GITHUB_OUTPUT
echo "buildtime=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}" >> $GITHUB_OUTPUT
echo "version=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}" >> $GITHUB_OUTPUT
- name: Wrap Inside a artifact folder
run: mkdir artifact && mv .output artifact/.output
- name: Build and push Docker image
id: push
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
VERSION=${{ steps.metadata.outputs.version }}
BUILD_TIME=${{ steps.metadata.outputs.buildtime }}
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Upload artifact
uses: actions/upload-artifact@v4
- name: Generate artifact attestation
uses: actions/attest-build-provenance@v1
with:
name: artifact
path: |
artifact
if-no-files-found: error
subject-name: ${{ env.REGISTRY }}/${{ env.REPO }}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: false

deploy:
needs: build
runs-on: ubuntu-24.04
steps:
- name: Download artifact
uses: actions/download-artifact@v4
runs-on: ubuntu-latest

- name: Upload artifact to VPS
uses: appleboy/scp-action@v0.1.7
steps:
- name: Deploy to VPS
uses: appleboy/ssh-action@v1.1.0
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
port: 22
source: artifact/*
target: ~/api-vision/
rm: true
overwrite: true
strip_components: 1
host: ${{ secrets.VPS_HOST }}
username: ${{ secrets.VPS_USERNAME }}
password: ${{ secrets.VPS_PASSWORD }}
script: |
# Pull new image
docker pull ${{ needs.build_web.outputs.image_name }}
# Stop the existing container
docker stop ${{ needs.build_web.outputs.image_title }} || true
docker rm ${{ needs.build_web.outputs.image_title }} || true
# Create and start new container
docker run --detach \
--name ${{ needs.build_web.outputs.image_title }} \
--restart unless-stopped \
--mount type=bind,source=$(pwd)/${{ needs.build_web.outputs.image_title }}/static,target=/app/static,readonly \
--env-file ~/${{ needs.build_web.outputs.image_title }}/.env.prod \
-p ${{ env.PORT }}:3000 \
${{ needs.build_web.outputs.image_name }}
# Clean up unused images
docker image prune -f
36 changes: 26 additions & 10 deletions .github/workflows/integrate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,45 @@ name: Nitro CI
on:
push:
branches:
- main
- develop

jobs:
build:
runs-on: ubuntu-24.04
runs-on: ubuntu-latest

permissions:
contents: write
pull-requests: write

steps:
- name: Checkout Repo
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup Node.js
uses: actions/setup-node@v3
- name: Setup Corepack for pnpm
run: corepack enable

- name: Setup Node.js with Corepack
uses: actions/setup-node@v4
with:
node-version: '20.x'
cache: 'npm'
node-version: lts/*
cache: 'pnpm'

- name: Install Dependencies
run: npm ci
run: pnpm install

- name: Bulid Artifacts
run: npm run build
- name: Build Artifacts
run: pnpm build

# - name: Run Tests
# run: npm test
# run: pnpm test

- name: Lint Code
run: pnpm lint || echo "Linting failed, but continuing"

- name: Format Code
run: pnpm format

- name: Create Release Pull Request
uses: changesets/action@v1
Expand Down
12 changes: 8 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Nitro dev/build outputs
.data
.output
.nitro
.cache
Expand All @@ -11,10 +12,13 @@ node_modules
logs
*.log

# Local env files
# Misc
.DS_Store
.fleet
.idea

# Env files
.env*
!.env.example

firebase-cred.json
assets/images/*
!assets/images/.gitkeep
firebase-cred.json
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh

#Lint Commit
pnpm dlx commitlint --edit $1
20 changes: 20 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env sh

# Get the list of staged files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR | sed 's| |\\ |g')

# Run gitleaks on each staged file using --no-git
for file in $STAGED_FILES; do
if [[ -f "$file" ]]; then
gitleaks detect --source="$file" --no-git --no-banner --verbose
fi
done

# Lint the staged files
pnpm lint $STAGED_FILES

# Format the staged files, ignoring unknown files
pnpm format $STAGED_FILES --ignore-unknown

# Update the index to mark the changes in the staged files
git update-index --again
2 changes: 0 additions & 2 deletions .npmrc

This file was deleted.

14 changes: 8 additions & 6 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# Nuxt dev/build outputs
# Nitro dev/build outputs
.data
.output
.nuxt
.nitro
.cache
dist

# Node dependencies
node_modules

# Package Manager
package.json
package-lock.json
pnpm-lock.yaml

# Logs
logs
Expand All @@ -19,6 +21,6 @@ logs
.fleet
.idea

# Local env files
.env
.env.*
# Env files
.env*
!.env.example
32 changes: 32 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
FROM node:lts-alpine AS builder

RUN corepack enable

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"

WORKDIR /app

COPY package.json pnpm-lock.yaml ./

RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile

COPY . .

RUN pnpm build

FROM node:lts-alpine AS runner

ARG VERSION
ARG BUILD_TIME

WORKDIR /app

COPY --from=builder /app/.output ./.output

ENV NODE_ENV=production
ENV NUXT_APP_VERSION=$VERSION

EXPOSE 3000

ENTRYPOINT ["node", ".output/server/index.mjs"]
Loading

0 comments on commit a8798c9

Please sign in to comment.