Skip to content
Merged

Dev #183

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .cursorignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
!.github/workflows/

# Add directories or file patterns to ignore during indexing (e.g. foo/ or *.csv)

# Ignore all files in the node_modules directory
Expand Down Expand Up @@ -61,7 +59,8 @@ deployments/
ocf/build/

# GitHub workflows
.github/
!.github/workflows/
!.github/workflows/*.yml

# Generated files
*.gen.ts
Expand Down
10 changes: 6 additions & 4 deletions .github/workflows/deploy.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
${{ runner.os }}-yarn-

- name: Install Node Dependencies
run: yarn install --frozen-lockfile --network-concurrency 5 --no-progress --verbose
run: yarn install --frozen-lockfile --network-concurrency 5 --no-progress

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
Expand All @@ -52,6 +52,9 @@ jobs:
- name: Check Formatting
run: yarn format:check

- name: Build Application
run: npx esbuild src/app.js --bundle --platform=node --outfile=dist/app.cjs

- name: Deploy
shell: bash
env:
Expand Down Expand Up @@ -91,7 +94,7 @@ jobs:
cd /home/ubuntu/app-${DEPLOY_TIME} && \
echo 'Building image on host...' && \
source ./scripts/docker_container_utils.sh && \
docker build -t ocp-dev:${DEPLOY_TIME} -f Dockerfile.dev . && \
docker build -t ocp-dev:${DEPLOY_TIME} -f Dockerfile . && \

echo 'Cleaning up old resources...' && \
docker ps -q --filter 'publish=8081' | xargs -r docker rm -f && \
Expand All @@ -106,7 +109,6 @@ jobs:
--health-interval='2s' \
--health-retries='3' \
--health-timeout='5s' \
--restart always \
-e DOCKER_ENV='true' \
-e NODE_ENV='development' \
-e SENTRY_DSN='${SENTRY_DSN}' \
Expand All @@ -118,7 +120,7 @@ jobs:
-v '/home/ubuntu/global-bundle.pem:/global-bundle.pem' \
ocp-dev:${DEPLOY_TIME} && \

wait_for_health \"\$CONTAINER_NAME\" && \
wait_for_health \"\$CONTAINER_NAME\"
if [ \$? -eq 0 ]; then
handle_container_switch \"\$CONTAINER_NAME\" \"${DEPLOY_TIME}\" \"dev\"
else
Expand Down
10 changes: 6 additions & 4 deletions .github/workflows/deploy.prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
${{ runner.os }}-yarn-

- name: Install Node Dependencies
run: yarn install --frozen-lockfile --network-concurrency 5 --no-progress --verbose
run: yarn install --frozen-lockfile --network-concurrency 5 --no-progress

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
Expand All @@ -52,6 +52,9 @@ jobs:
- name: Check Formatting
run: yarn format:check

- name: Build Application
run: npx esbuild src/app.js --bundle --platform=node --outfile=dist/app.cjs

- name: Deploy
shell: bash
env:
Expand Down Expand Up @@ -91,7 +94,7 @@ jobs:
cd /home/ubuntu/app-${DEPLOY_TIME} && \
echo 'Building image on host...' && \
source ./scripts/docker_container_utils.sh && \
docker build -t ocp-prod:${DEPLOY_TIME} -f Dockerfile.prod . && \
docker build -t ocp-prod:${DEPLOY_TIME} -f Dockerfile . && \

echo 'Cleaning up old resources...' && \
docker ps -q --filter 'publish=8081' | xargs -r docker rm -f && \
Expand All @@ -106,7 +109,6 @@ jobs:
--health-interval='2s' \
--health-retries='3' \
--health-timeout='5s' \
--restart always \
-e DOCKER_ENV='true' \
-e NODE_ENV='production' \
-e SENTRY_DSN='${SENTRY_DSN}' \
Expand All @@ -118,7 +120,7 @@ jobs:
-v '/home/ubuntu/global-bundle.pem:/global-bundle.pem' \
ocp-prod:${DEPLOY_TIME} && \

wait_for_health \"\$CONTAINER_NAME\" && \
wait_for_health \"\$CONTAINER_NAME\"
if [ \$? -eq 0 ]; then
handle_container_switch \"\$CONTAINER_NAME\" \"${DEPLOY_TIME}\" \"prod\"
else
Expand Down
26 changes: 26 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Use the official Node.js 18 image
FROM node:18

# Set working directory
WORKDIR /app

# Environment variables to avoid platform-specific esbuild bloat
ENV npm_config_platform=linux
ENV npm_config_arch=x64
ENV NODE_ENV=production

# Copy dependency definitions first to leverage Docker cache
COPY package.json yarn.lock ./

# Install dependencies
# Using --frozen-lockfile is best practice for CI/CD
RUN yarn install --frozen-lockfile --network-concurrency 5 --no-progress

# Copy app source and pre-built files
COPY . .

# Expose app port
EXPOSE 8080

# Run the pre-built application
CMD ["node", "dist/app.cjs"]
15 changes: 0 additions & 15 deletions Dockerfile.dev

This file was deleted.

31 changes: 0 additions & 31 deletions Dockerfile.prod

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"@typescript-eslint/eslint-plugin": "^6.19.0",
"@typescript-eslint/parser": "^6.19.0",
"chalk": "4",
"esbuild": "^0.23.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-import-resolver-node": "^0.3.9",
Expand Down
24 changes: 13 additions & 11 deletions scripts/docker_container_utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ wait_for_health() {
local timeout_duration=30
local start_time=$(date +%s)
local end_time=$((start_time + timeout_duration))

echo "Waiting for container to be healthy..."
while (( $(date +%s) < end_time )); do
local status=$(check_health "$container_name")
echo "Container health status: [$status]"

if [[ "$status" = "healthy" ]]; then
return 0
fi
sleep 2
done

return 1
}

Expand All @@ -39,7 +39,7 @@ handle_container_switch() {
# Get current container config
local config=$(docker inspect "$container_name" --format='{{range .Config.Env}} -e {{.}}{{end}}')
local image=$(docker inspect "$container_name" --format='{{.Config.Image}}')

# Create new container but don't start it yet
echo "Creating final container..."
docker create \
Expand All @@ -53,7 +53,7 @@ handle_container_switch() {
$config \
-v '/home/ubuntu/global-bundle.pem:/global-bundle.pem' \
$image

# Atomic switch: stop old container and start new one as quickly as possible
echo "Performing atomic switch..."
(
Expand All @@ -63,29 +63,29 @@ handle_container_switch() {
docker rename ocp-${environment}-final ocp-${environment}
docker start ocp-${environment}
) & # Run in background

# Wait for background process
wait $!

# Verify new container is running
if ! docker ps --filter "name=ocp-${environment}" --filter "status=running" | grep -q ocp-${environment}; then
echo "Switch failed, rolling back..."
return 1
fi


# Stop and remove the old container
docker stop "$container_name"
docker rm "$container_name"

echo 'Performing final cleanup...'
# Force remove old images
docker image ls "ocp-${environment}:*" --format '{{.ID}}' | tail -n +3 | xargs -r docker image rm -f
# Clean up dangling images and volumes
docker system prune -af --volumes
# Tag the current image as latest
docker tag "ocp-${environment}:${deploy_time}" ocp-${environment}:latest

echo 'Deployment successful!'
cd /home/ubuntu && rm -rf "app-${deploy_time}"
return 0
Expand All @@ -97,9 +97,11 @@ handle_failed_deployment() {
local deploy_time="$2"
local environment="$3"
echo 'New container failed health check, rolling back...'
echo "Dumping logs for container: $container_name"
docker logs "$container_name"
docker stop "$container_name"
docker rm "$container_name"
docker image rm "ocp-${environment}:${deploy_time}"
cd /home/ubuntu && rm -rf "app-${deploy_time}"
return 1
}
}
5 changes: 5 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ app.use("/manifest", manifestRoutes);
// transactions
app.use("/transactions/", contractMiddleware, transactionRoutes);

// Health check endpoint
app.get("/health", (req, res) => {
res.status(200).send("OK");
});

const startServer = async () => {
// Connect to MongoDB
await connectDB();
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3322,7 +3322,7 @@ es6-promisify@^5.0.0:
dependencies:
es6-promise "^4.0.3"

esbuild@~0.23.0:
esbuild@^0.23.0, esbuild@~0.23.0:
version "0.23.1"
resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.23.1.tgz#40fdc3f9265ec0beae6f59824ade1bd3d3d2dab8"
integrity sha512-VVNz/9Sa0bs5SELtn3f7qhJCDPCF5oMEl5cO9/SSinpE9hbPVvxbd572HH5AKiP7WD8INO53GgfDDhRjkylHEg==
Expand Down