diff --git a/.github/workflows/web-sync-server-deploy.yml b/.github/workflows/web-sync-server-deploy.yml index 41bf53ee6..24157226a 100644 --- a/.github/workflows/web-sync-server-deploy.yml +++ b/.github/workflows/web-sync-server-deploy.yml @@ -1,22 +1,28 @@ name: Web Sync Server deploy + on: push: branches: - main paths: - - '.github/workflows/web-sync-server*' - - 'moon/apps/sync-server/**' - - 'moon/packages/editor/**' - - 'moon/packages/config/**' - - 'moon/packages/types/**' - + - ".github/workflows/web-sync-server*" + - "moon/apps/sync-server/**" + - "moon/packages/editor/**" + - "moon/packages/config/**" + - "moon/packages/types/**" concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true +env: + REGISTRY_ALIAS: m8q5m4u3 + REPOSITORY: mega/web-sync-server + IMAGE_TAG: latest + GCP_PROJECT_ID: infra-20250121-20260121-0235 + jobs: - web-sync-server-deploy: + build-and-push: runs-on: ubuntu-latest timeout-minutes: 10 steps: @@ -25,7 +31,7 @@ jobs: - uses: actions/setup-node@v4 with: - node-version: '20' + node-version: "20" - name: Configure AWS Credentials uses: aws-actions/configure-aws-credentials@v4 @@ -40,26 +46,86 @@ jobs: with: registry-type: public + - name: Auth to GCP + uses: google-github-actions/auth@v2 + with: + credentials_json: ${{ secrets.GCP_SA_KEY }} + + - name: Configure docker for GCP + run: gcloud auth configure-docker us-central1-docker.pkg.dev + - name: Build, tag, and push docker image to Amazon ECR Public working-directory: moon env: REGISTRY: ${{ steps.login-ecr-public.outputs.registry }} - REGISTRY_ALIAS: m8q5m4u3 - REPOSITORY: mega/web-sync-server - IMAGE_TAG: latest run: | + set -euo pipefail + + AWS_IMAGE_BASE="$REGISTRY/${{ env.REGISTRY_ALIAS }}/${{ env.REPOSITORY }}" + GCP_IMAGE_BASE="us-central1-docker.pkg.dev/${{ env.GCP_PROJECT_ID }}/${{ env.REPOSITORY }}" + IMAGE_TAG="${{ env.IMAGE_TAG }}" + docker build \ -f apps/sync-server/Dockerfile \ - -t $REGISTRY/$REGISTRY_ALIAS/$REPOSITORY:$IMAGE_TAG . - docker push $REGISTRY/$REGISTRY_ALIAS/$REPOSITORY:$IMAGE_TAG + -t "$AWS_IMAGE_BASE:$IMAGE_TAG" . + + # Push to AWS ECR Public + docker push "$AWS_IMAGE_BASE:$IMAGE_TAG" + # Tag and push to GCP Artifact Registry + docker tag "$AWS_IMAGE_BASE:$IMAGE_TAG" "$GCP_IMAGE_BASE:$IMAGE_TAG" + docker push "$GCP_IMAGE_BASE:$IMAGE_TAG" + + deploy-aws: + needs: build-and-push + if: ${{ github.repository == 'web3infra-foundation/mega' }} + runs-on: ubuntu-latest + strategy: + matrix: + include: + - cluster: gitmega-com + service: mega-web-sync-server-dev-service-v3hdlyk8 + # TODO: update service name to actual gitmono sync server ECS service + - cluster: gitmono-com-mega-app + service: mega-web-sync-service + steps: - name: Force ECS redeploy run: | aws ecs update-service \ - --cluster gitmega-com \ - --service mega-web-sync-server-dev-service-v3hdlyk8 \ + --cluster ${{ matrix.cluster }} \ + --service ${{ matrix.service }} \ --force-new-deployment env: AWS_REGION: ap-southeast-2 AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + + deploy-gcp: + needs: build-and-push + if: ${{ github.repository == 'web3infra-foundation/mega' }} + runs-on: ubuntu-latest + steps: + - name: Auth to GCP + uses: google-github-actions/auth@v2 + with: + credentials_json: ${{ secrets.GCP_SA_KEY }} + + - name: Deploy to Cloud Run (force new revision) + env: + REGION: asia-east1 + SERVICE_NAME: buck2hub-notesync + run: | + set -euo pipefail + + IMAGE=$(gcloud run services describe "$SERVICE_NAME" \ + --project "$GCP_PROJECT_ID" \ + --region "$REGION" \ + --format="value(spec.template.spec.containers[0].image)") + + echo "Current image: $IMAGE" + + gcloud run deploy "$SERVICE_NAME" \ + --image "$IMAGE" \ + --region "$REGION" \ + --project "$GCP_PROJECT_ID" \ + --quiet diff --git a/moon/apps/sync-server/.env.buck2hub b/moon/apps/sync-server/.env.buck2hub deleted file mode 100644 index 1b89de60a..000000000 --- a/moon/apps/sync-server/.env.buck2hub +++ /dev/null @@ -1,4 +0,0 @@ -# Buck2Hub Environment -API_URL=https://api.buck2hub.com -PORT=9000 -NODE_ENV=production diff --git a/moon/apps/sync-server/.env.demo b/moon/apps/sync-server/.env.demo deleted file mode 100644 index a5d7401ce..000000000 --- a/moon/apps/sync-server/.env.demo +++ /dev/null @@ -1,4 +0,0 @@ -# Demo Environment -API_URL=http://api.gitmono.local:8080 -PORT=9000 -NODE_ENV=development diff --git a/moon/apps/sync-server/.env.example b/moon/apps/sync-server/.env.example deleted file mode 100644 index dc7826f1d..000000000 --- a/moon/apps/sync-server/.env.example +++ /dev/null @@ -1,10 +0,0 @@ -# API Configuration -API_URL=https://api.gitmega.com - -# Server Configuration -PORT=9000 -NODE_ENV=development - -# Sentry Configuration (optional) -# SENTRY_DSN= -# SENTRY_AUTH_TOKEN= diff --git a/moon/apps/sync-server/.env.gitmono b/moon/apps/sync-server/.env.gitmono deleted file mode 100644 index 18fd83184..000000000 --- a/moon/apps/sync-server/.env.gitmono +++ /dev/null @@ -1,4 +0,0 @@ -# Gitmono Environment -API_URL=https://api.gitmono.com -PORT=9000 -NODE_ENV=production diff --git a/moon/apps/sync-server/.env.openatom b/moon/apps/sync-server/.env.openatom deleted file mode 100644 index 83708a419..000000000 --- a/moon/apps/sync-server/.env.openatom +++ /dev/null @@ -1,4 +0,0 @@ -# OpenAtom Environment -API_URL=https://api.xuanwu.openatom.cn -PORT=9000 -NODE_ENV=production diff --git a/moon/apps/sync-server/.env.staging b/moon/apps/sync-server/.env.staging deleted file mode 100644 index 678e18ca2..000000000 --- a/moon/apps/sync-server/.env.staging +++ /dev/null @@ -1,4 +0,0 @@ -# Staging Environment -API_URL=https://api.gitmega.com -PORT=9000 -NODE_ENV=production diff --git a/moon/apps/sync-server/.env.staging-nju b/moon/apps/sync-server/.env.staging-nju deleted file mode 100644 index 4865f1866..000000000 --- a/moon/apps/sync-server/.env.staging-nju +++ /dev/null @@ -1,4 +0,0 @@ -# Staging NJU Environment -API_URL=http://api.gitmega.nju:8080 -PORT=9000 -NODE_ENV=development diff --git a/moon/apps/sync-server/src/config.ts b/moon/apps/sync-server/src/config.ts index 0dcebe676..57fba06f9 100644 --- a/moon/apps/sync-server/src/config.ts +++ b/moon/apps/sync-server/src/config.ts @@ -1,21 +1,12 @@ -import * as path from 'path' import * as dotenv from 'dotenv' -// Load environment-specific .env file -// Priority: .env.{NODE_ENV} > .env.local > .env -const envFile = process.env.NODE_ENV ? `.env.${process.env.NODE_ENV}` : '.env.local' +const NODE_ENV = process.env.NODE_ENV || 'development' -dotenv.config({ path: path.resolve(process.cwd(), envFile) }) - -// Fallback to .env.local if environment-specific file doesn't exist -if (process.env.NODE_ENV && !process.env.API_URL) { - dotenv.config({ path: path.resolve(process.cwd(), '.env.local') }) +// In non-production environments, load variables from .env.local for local development. +if (NODE_ENV !== 'production') { + dotenv.config({ path: '.env.local' }) } -// API URL - read from environment variable with fallback default -export const API_URL = process.env.API_URL || 'https://api.gitmega.com' - -// Server Configuration +export const API_URL = process.env.API_URL export const PORT = parseInt(process.env.PORT || '9000', 10) -export const NODE_ENV = process.env.NODE_ENV || 'development' export const IS_PRODUCTION = NODE_ENV === 'production'