Skip to content
Merged
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
238 changes: 238 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
name: Build and Deploy

on:
push:
branches:
- main
- staging
- dev

workflow_dispatch:
inputs:
channel:
description: "Release channel"
required: true
type: choice
options:
- dev
- staging
- main
default: "staging"
dry-run:
description: "Dry run (skip publishing)"
required: false
type: boolean
default: false

env:
NODE_VERSION: "22"
PNPM_VERSION: "9"
DOCKER_REGISTRY: cr.vetra.io
PROJECT_NAME: ecosystem-api

jobs:
prepare:
name: Prepare
runs-on: ubuntu-latest
outputs:
channel: ${{ steps.params.outputs.channel }}
branch: ${{ steps.params.outputs.branch }}
dry_run: ${{ steps.params.outputs.dry_run }}
version: ${{ steps.params.outputs.version }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Determine parameters
id: params
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
CHANNEL="${{ inputs.channel }}"
DRY_RUN="${{ inputs.dry-run }}"
else
# Determine channel from branch
BRANCH="${{ github.ref_name }}"
case "$BRANCH" in
main) CHANNEL="main" ;;
staging) CHANNEL="staging" ;;
dev) CHANNEL="dev" ;;
*) CHANNEL="dev" ;;
esac
DRY_RUN="false"
fi

BRANCH="${{ github.ref_name }}"
VERSION=$(node -p "require('./package.json').version")
SHORT_SHA=$(echo "${{ github.sha }}" | cut -c1-7)

# Create version tag
if [ "$CHANNEL" = "main" ]; then
VERSION_TAG="v${VERSION}"
else
VERSION_TAG="v${VERSION}-${CHANNEL}.${SHORT_SHA}"
fi

echo "channel=$CHANNEL" >> $GITHUB_OUTPUT
echo "branch=$BRANCH" >> $GITHUB_OUTPUT
echo "dry_run=$DRY_RUN" >> $GITHUB_OUTPUT
echo "version=$VERSION_TAG" >> $GITHUB_OUTPUT

echo "Channel: $CHANNEL"
echo "Branch: $BRANCH"
echo "Version: $VERSION_TAG"
echo "Dry Run: $DRY_RUN"

build-docker:
name: Build Docker Image
needs: [prepare]
if: needs.prepare.outputs.dry_run != 'true'
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4

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

- name: Ensure Docker project exists
run: |
PROJECT_NAME="${{ env.PROJECT_NAME }}"

STATUS=$(curl -s -o /dev/null -w "%{http_code}" \
-u "${{ secrets.DOCKER_USERNAME }}:${{ secrets.DOCKER_PASSWORD }}" \
"https://${{ env.DOCKER_REGISTRY }}/api/v2.0/projects?name=${PROJECT_NAME}")

if [ "$STATUS" = "200" ]; then
EXISTS=$(curl -s \
-u "${{ secrets.DOCKER_USERNAME }}:${{ secrets.DOCKER_PASSWORD }}" \
"https://${{ env.DOCKER_REGISTRY }}/api/v2.0/projects?name=${PROJECT_NAME}" | \
jq -r ".[] | select(.name==\"${PROJECT_NAME}\") | .name")

if [ "$EXISTS" = "$PROJECT_NAME" ]; then
echo "Project ${PROJECT_NAME} already exists"
else
echo "Creating project ${PROJECT_NAME}..."
curl -X POST \
-u "${{ secrets.DOCKER_USERNAME }}:${{ secrets.DOCKER_PASSWORD }}" \
-H "Content-Type: application/json" \
-d "{\"project_name\": \"${PROJECT_NAME}\", \"public\": false}" \
"https://${{ env.DOCKER_REGISTRY }}/api/v2.0/projects"
fi
else
echo "Creating project ${PROJECT_NAME}..."
curl -X POST \
-u "${{ secrets.DOCKER_USERNAME }}:${{ secrets.DOCKER_PASSWORD }}" \
-H "Content-Type: application/json" \
-d "{\"project_name\": \"${PROJECT_NAME}\", \"public\": false}" \
"https://${{ env.DOCKER_REGISTRY }}/api/v2.0/projects"
fi

- name: Login to Docker Registry
uses: docker/login-action@v3
with:
registry: ${{ env.DOCKER_REGISTRY }}
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Determine image tags
id: tags
run: |
VERSION="${{ needs.prepare.outputs.version }}"
CHANNEL="${{ needs.prepare.outputs.channel }}"

DOCKER_BASE="${{ env.DOCKER_REGISTRY }}/${{ env.PROJECT_NAME }}/api"

TAGS="${DOCKER_BASE}:${VERSION}"

if [ "$CHANNEL" = "main" ]; then
TAGS="${TAGS},${DOCKER_BASE}:latest"
else
TAGS="${TAGS},${DOCKER_BASE}:${CHANNEL}"
fi

echo "tags=$TAGS" >> $GITHUB_OUTPUT
echo "Image tags: $TAGS"

- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ steps.tags.outputs.tags }}
cache-from: type=gha
cache-to: type=gha,mode=max

update-k8s:
name: Update K8s Cluster
needs: [prepare, build-docker]
if: |
needs.prepare.outputs.dry_run != 'true' &&
(needs.prepare.outputs.channel == 'dev' || needs.prepare.outputs.channel == 'staging')
runs-on: ubuntu-latest
steps:
- name: Checkout k8s-hosting repository
uses: actions/checkout@v4
with:
repository: powerhouse-inc/powerhouse-k8s-hosting
token: ${{ secrets.K8S_REPO_PAT }}
path: k8s-hosting

- name: Install yq
uses: mikefarah/yq@v4

- name: Update image tags
run: |
CHANNEL="${{ needs.prepare.outputs.channel }}"
VERSION="${{ needs.prepare.outputs.version }}"
VALUES_FILE="k8s-hosting/tenants/${CHANNEL}/ecosystem-api-values.yaml"

echo "Updating ${VALUES_FILE} with version ${VERSION}"

yq -i ".image.tag = \"${VERSION}\"" "$VALUES_FILE"

echo "Updated image tag to ${VERSION}"
cat "$VALUES_FILE"

- name: Commit and push changes
run: |
cd k8s-hosting
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"

CHANNEL="${{ needs.prepare.outputs.channel }}"
VERSION="${{ needs.prepare.outputs.version }}"

git add "tenants/${CHANNEL}/ecosystem-api-values.yaml"

if git diff --staged --quiet; then
echo "No changes to commit"
else
git commit -m "chore(${CHANNEL}): update ecosystem-api image tag to ${VERSION}"
git push
fi

summary:
name: Release Summary
needs: [prepare, build-docker, update-k8s]
if: always()
runs-on: ubuntu-latest
steps:
- name: Summary
run: |
echo "## Release Summary" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "| Parameter | Value |" >> $GITHUB_STEP_SUMMARY
echo "|-----------|-------|" >> $GITHUB_STEP_SUMMARY
echo "| Channel | ${{ needs.prepare.outputs.channel }} |" >> $GITHUB_STEP_SUMMARY
echo "| Branch | ${{ needs.prepare.outputs.branch }} |" >> $GITHUB_STEP_SUMMARY
echo "| Version | ${{ needs.prepare.outputs.version }} |" >> $GITHUB_STEP_SUMMARY
echo "| Dry Run | ${{ needs.prepare.outputs.dry_run }} |" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Docker Image" >> $GITHUB_STEP_SUMMARY
echo "\`${{ env.DOCKER_REGISTRY }}/${{ env.PROJECT_NAME }}/api:${{ needs.prepare.outputs.version }}\`" >> $GITHUB_STEP_SUMMARY
19 changes: 15 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
# builder
FROM node:22.5-alpine AS builder

RUN npm install -g typescript pnpm
RUN npm install -g typescript

WORKDIR /app
COPY package.json pnpm-lock.yaml knexfile.js ./
RUN pnpm install
COPY package.json package-lock.json knexfile.js ./
RUN npm ci

COPY . ./
RUN tsc -p ./config/tsconfig.json

# runner
FROM node:22.5-alpine AS runner

RUN apk add --no-cache curl
RUN npm install -g knex

WORKDIR /app
COPY --from=builder /app/ ./
COPY docker/entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh

ENTRYPOINT [ "node" ]
ENV NODE_ENV=production
ENV PORT=4000

EXPOSE ${PORT}

HEALTHCHECK --interval=30s --timeout=3s --start-period=30s --retries=3 \
CMD curl -f http://localhost:${PORT}/healthz || exit 1

ENTRYPOINT ["/app/entrypoint.sh"]
12 changes: 12 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
set -e

# Run migrations if not skipped
if [ "$SKIP_DB_MIGRATIONS" != "true" ] && [ -n "$PG_CONNECTION_STRING" ]; then
echo "[entrypoint] Running database migrations..."
knex migrate:latest --knexfile ./knexfile.js
echo "[entrypoint] Migrations completed"
fi

echo "[entrypoint] Starting ecosystem-api on port ${PORT:-4000}..."
exec node ./build/index.js
4 changes: 3 additions & 1 deletion scripts/snapshots/data/blockNumbers-ADs.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ export default {
"2025/02": 21815725,
"2025/03": 22015856,
"2025/04": 22235856,
"2025/05": 22455856
"2025/05": 22455856,
"2025/06": 22675856,
"2025/07": 22895856
};
4 changes: 3 additions & 1 deletion scripts/snapshots/data/blockNumbers-DAIF.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,7 @@ export default {
"2025/02": 21815725,
"2025/03": 22015856,
"2025/04": 22235856,
"2025/05": 22455856
"2025/05": 22455856,
"2025/06": 22816918,
"2025/07": 23036191
};
4 changes: 3 additions & 1 deletion scripts/snapshots/data/blockNumbers-DEWIZ.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ export default {
"2025/02": 21815725,
"2025/03": 22015856,
"2025/04": 22235856,
"2025/05": 22455856
"2025/05": 22455856,
"2025/06": 22816918,
"2025/07": 23036191
};
4 changes: 3 additions & 1 deletion scripts/snapshots/data/blockNumbers-DRAFT-KEEPERS.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@ export default {
"2025/02": 21815725,
"2025/03": 22015856,
"2025/04": 22235856,
"2025/05": 22455856
"2025/05": 22455856,
"2025/06": 22816918,
"2025/07": 23036191
};
4 changes: 3 additions & 1 deletion scripts/snapshots/data/blockNumbers-DRAFT.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,7 @@ export default {
"2025/02": 21815725,
"2025/03": 22015856,
"2025/04": 22235856,
"2025/05": 22455856
"2025/05": 22455856,
"2025/06": 22816918,
"2025/07": 23036191
};
4 changes: 3 additions & 1 deletion scripts/snapshots/data/blockNumbers-IS.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ export default {
"2025/02": 21815725,
"2025/03": 22015856,
"2025/04": 22235856,
"2025/05": 22455856
"2025/05": 22455856,
"2025/06": 22816918,
"2025/07": 23036191,
};
4 changes: 3 additions & 1 deletion scripts/snapshots/data/blockNumbers-JETSTREAM.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ export default {
"2025/02": 21815725,
"2025/03": 22015856,
"2025/04": 22235856,
"2025/05": 22455856
"2025/05": 22455856,
"2025/06": 22816918,
"2025/07": 23036191
};
4 changes: 3 additions & 1 deletion scripts/snapshots/data/blockNumbers-PH.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ export default {
"2025/02": 21969838,
"2025/03": 22181687,
"2025/04": 22365931,
"2025/05": 22455856
"2025/05": 22597645,
"2025/06": 22816918,
"2025/07": 23036191,
};
4 changes: 3 additions & 1 deletion scripts/snapshots/data/blockNumbers-TECH-EA.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ export default {
"2025/02": 21815725,
"2025/03": 22015856,
"2025/04": 22235856,
"2025/05": 22455856
"2025/05": 22455856,
"2025/06": 22816918,
"2025/07": 23036191,
};
Loading