From e1a8a33531c53037ae29900fd9f22671817172e6 Mon Sep 17 00:00:00 2001 From: sm47916 Date: Sun, 3 Aug 2025 08:02:47 +0000 Subject: [PATCH] Fix both qa and prod builds --- .github/workflows/docker-image-prod.yml | 87 ++++++++++++++++++ .../{docker-image.yml => docker-image-qa.yml} | 46 ++++++---- .gitignore | 1 + docker-builds/server/Dockerfile | 92 +++++++++---------- docker-builds/ui/Dockerfile | 72 +++++++-------- docker-compose.yml | 29 ++---- ui/src/ethlance/ui/config.cljs | 2 +- 7 files changed, 206 insertions(+), 123 deletions(-) create mode 100644 .github/workflows/docker-image-prod.yml rename .github/workflows/{docker-image.yml => docker-image-qa.yml} (80%) diff --git a/.github/workflows/docker-image-prod.yml b/.github/workflows/docker-image-prod.yml new file mode 100644 index 00000000..66ca5ea4 --- /dev/null +++ b/.github/workflows/docker-image-prod.yml @@ -0,0 +1,87 @@ +name: Docker Image CI + +on: + push: + # branches: [ "master", "main", "migrate_docker_files"] + branches: ["master", "main"] + +permissions: + contents: read + +jobs: + docker_build: + name: Build images and deploy contracts + strategy: + matrix: + env: ["prod"] + permissions: + contents: read + runs-on: ubuntu-latest + steps: + - name: Checkout ethlance code + uses: actions/checkout@v4 + + - name: Set up QEMU + id: setup-qemu + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + id: setup-buildx + uses: docker/setup-buildx-action@v2 + + - name: Configure AWS Credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ secrets.AWS_DEFAULT_REGION }} + + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v2 + + - name: Checkout ethlance-configs repo + uses: actions/checkout@v4 + with: + repository: district0x/ethlance-config + path: ethlance-config + token: ${{ secrets.ETHLANCE_CONFIG_PAT }} + ref: master + + - name: check configs + run: | + # cp -r ethlance-config/config . + ls -lah + ls -lah ethlance-config + + - name: Build and push Server + uses: docker/build-push-action@v6 + with: + context: . + builder: ${{ steps.setup-buildx.outputs.name }} + platforms: linux/amd64,linux/arm64 + file: docker-builds/server/Dockerfile + build-args: | + ETHLANCE_ENV=${{ matrix.env }} + ETHLANCE_DEPLOY_SEED=${{ secrets.ETHLANCE_DEPLOY_SEED }} + DOCKER_REGISTRY=${{ secrets.ECR_REGISTRY }} + push: true + tags: | + ${{ secrets.ECR_REGISTRY }}/ethlance-${{ matrix.env }}-server:${{ github.sha }} + ${{ secrets.ECR_REGISTRY }}/ethlance-${{ matrix.env }}-server:latest + + - name: Build and push UI + uses: docker/build-push-action@v6 + with: + context: . + platforms: linux/amd64,linux/arm64 + builder: ${{ steps.setup-buildx.outputs.name }} + file: docker-builds/ui/Dockerfile + build-args: | + ETHLANCE_ENV=${{ matrix.env }} + ETHLANCE_DEPLOY_SEED=${{ secrets.ETHLANCE_DEPLOY_SEED }} + DOCKER_REGISTRY=${{ secrets.ECR_REGISTRY }} + push: true + tags: | + ${{ secrets.ECR_REGISTRY }}/ethlance-${{ matrix.env }}-ui:${{ github.sha }} + ${{ secrets.ECR_REGISTRY }}/ethlance-${{ matrix.env }}-ui:latest diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image-qa.yml similarity index 80% rename from .github/workflows/docker-image.yml rename to .github/workflows/docker-image-qa.yml index 68e8a6ea..a06eb5e0 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image-qa.yml @@ -1,9 +1,8 @@ name: Docker Image CI on: - push: - branches: [ "master", "main", "newlance"] pull_request: + branches: ["master"] permissions: contents: read @@ -123,7 +122,7 @@ jobs: - name: Set up QEMU id: setup-qemu - uses: docker/setup-qemu-action@v2 + uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx id: setup-buildx @@ -148,29 +147,40 @@ jobs: token: ${{ secrets.ETHLANCE_CONFIG_PAT }} ref: master - - name: Copy configs + - name: check configs run: | - cp -r ethlance-config/config . - ls -lah config/ + # cp -r ethlance-config/config . + ls -lah + ls -lah ethlance-config - - name: Build and push UI - uses: docker/build-push-action@v4 + - name: Build and push Server + uses: docker/build-push-action@v6 with: context: . - platforms: linux/amd64 builder: ${{ steps.setup-buildx.outputs.name }} - file: docker-builds/ui/Dockerfile - build-args: BUILD_ENV=${{ matrix.env }} + platforms: linux/amd64,linux/arm64 + file: docker-builds/server/Dockerfile + build-args: | + ETHLANCE_ENV=${{ matrix.env }} + ETHLANCE_DEPLOY_SEED=${{ secrets.ETHLANCE_DEPLOY_SEED }} + DOCKER_REGISTRY=${{ secrets.ECR_REGISTRY }} push: true - tags: ${{ secrets.ECR_REGISTRY }}/ethlance-ui:latest-${{ matrix.env }} + tags: | + ${{ secrets.ECR_REGISTRY }}/ethlance-${{ matrix.env }}-server:${{ github.sha }} + ${{ secrets.ECR_REGISTRY }}/ethlance-${{ matrix.env }}-server:latest - - name: Build and push Server - uses: docker/build-push-action@v4 + - name: Build and push UI + uses: docker/build-push-action@v6 with: context: . + platforms: linux/amd64,linux/arm64 builder: ${{ steps.setup-buildx.outputs.name }} - platforms: linux/amd64 - file: docker-builds/server/Dockerfile - build-args: BUILD_ENV=${{ matrix.env }} + file: docker-builds/ui/Dockerfile + build-args: | + ETHLANCE_ENV=${{ matrix.env }} + ETHLANCE_DEPLOY_SEED=${{ secrets.ETHLANCE_DEPLOY_SEED }} + DOCKER_REGISTRY=${{ secrets.ECR_REGISTRY }} push: true - tags: ${{ secrets.ECR_REGISTRY }}/ethlance-server:latest-${{ matrix.env }} + tags: | + ${{ secrets.ECR_REGISTRY }}/ethlance-${{ matrix.env }}-ui:${{ github.sha }} + ${{ secrets.ECR_REGISTRY }}/ethlance-${{ matrix.env }}-ui:latest diff --git a/.gitignore b/.gitignore index 39a9612f..f37dd378 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ temp release .clj-kondo .lsp +.env \ No newline at end of file diff --git a/docker-builds/server/Dockerfile b/docker-builds/server/Dockerfile index e0b22288..d4bfb6e5 100644 --- a/docker-builds/server/Dockerfile +++ b/docker-builds/server/Dockerfile @@ -1,63 +1,57 @@ -FROM --platform=amd64 487920318758.dkr.ecr.us-west-2.amazonaws.com/cljs-web3-ci:node-20.18.1 as init -ARG BUILD_ENV=qa -ARG ETHLANCE_DEPLOY_SEED="sign bachelor state zoo expire boat morning situate scene unveil oven crew" -USER root +ARG DOCKER_REGISTRY +FROM $DOCKER_REGISTRY/cljs-web3-ci:node-20.18.1 AS builder -ENV ETHLANCE_COMPONENT="server" -ENV ETHLANCE_SOURCE_ROOT=/build/ethlance -ENV ETHLANCE_SERVER_ROOT=/build/ethlance/server -ENV ETHLANCE_ENV=$BUILD_ENV -ENV DEPLOY_TARGET=/deploy/ -ENV ETHLANCE_DEPLOY_SEED=${ETHLANCE_DEPLOY_SEED} +ARG ETHLANCE_ENV +ARG ETHLANCE_DEPLOY_SEED +USER root -ENV SMART_CONTRACTS_PATH="/build/ethlance/shared/src/ethlance/shared/smart_contracts_${BUILD_ENV}.cljs" -ENV SMART_CONTRACTS_BUILD_PATH="/build/ethlance/resources/public/contracts/build" -ENV ETHLANCE_CONFIG_PATH="/build/ethlance/config/${ETHLANCE_COMPONENT}-config-${BUILD_ENV}.edn" -ENV UI_CONFIG_PATH="/build/ethlance/config/ui-config-${BUILD_ENV}.edn" +# Set environment variables +ENV ETHLANCE_SOURCE_ROOT=/build/ethlance \ + ETHLANCE_ENV=$ETHLANCE_ENV \ + BUILD_ENV=$ETHLANCE_ENV +ENV ETHLANCE_SERVER_ROOT=${ETHLANCE_SOURCE_ROOT}/server \ + ETHLANCE_DEPLOY_SEED=${ETHLANCE_DEPLOY_SEED} \ + SMART_CONTRACTS_BUILD_PATH="${ETHLANCE_SOURCE_ROOT}/resources/public/contracts/build" \ + SMART_CONTRACTS_PATH="${ETHLANCE_SOURCE_ROOT}/shared/src/ethlance/shared/smart_contracts_${ETHLANCE_ENV}.cljs" \ + ETHLANCE_CONFIG_PATH="${ETHLANCE_SOURCE_ROOT}/config/server-config-${ETHLANCE_ENV}.edn" \ + UI_CONFIG_PATH="${ETHLANCE_SOURCE_ROOT}/config/ui-config-${ETHLANCE_ENV}.edn" -WORKDIR /build/ethlance +WORKDIR ${ETHLANCE_SOURCE_ROOT} COPY . . +COPY ethlance-config/config "${ETHLANCE_SOURCE_ROOT}/config" -RUN cat "${SMART_CONTRACTS_PATH}" +# Clone required libraries +RUN git clone --depth 1 https://github.com/district0x/d0x-libs /build/d0x-libs -# WORKDIR /build/ethlance-config/ -# COPY ethlance-config . -WORKDIR /build -COPY ethlance-config/config /build/ethlance/config -# RUN cp /build/ethlance/ethlance-config/config /build/ethlance/ -RUN git clone https://github.com/district0x/d0x-libs - -FROM init as build_server -WORKDIR ${ETHLANCE_SOURCE_ROOT} -RUN yarn install && ETHLANCE_ENV="${BUILD_ENV}" npx truffle compile +# Compile contracts +RUN yarn install && ETHLANCE_ENV="${ETHLANCE_ENV}" npx truffle compile -# Release deployment +# Build UI WORKDIR "${ETHLANCE_SOURCE_ROOT}/ui" RUN yarn && \ - ETHLANCE_ENV="${BUILD_ENV}" npx shadow-cljs release dev-ui && \ + ETHLANCE_ENV="${ETHLANCE_ENV}" npx shadow-cljs release dev-ui && \ ./node_modules/less/bin/lessc resources/public/less/main.less resources/public/css/main.css --verbose -# Release deployment +# Build server WORKDIR "${ETHLANCE_SOURCE_ROOT}/server" RUN yarn && npx shadow-cljs release dev-server -ARG BUILD_ENV=qa -ARG ETHLANCE_ENV=qa -FROM node:20 -ENV ETHLANCE_SOURCE_ROOT=/deploy -ENV ETHLANCE_SERVER_ROOT=/deploy/server -ENV ETHLANCE_ENV=qa -ENV DEPLOY_TARGET=/deploy -ENV UI_CONFIG_PATH="/deploy/resources/config/ui-config-${BUILD_ENV}.edn" -ENV SERVER_CONFIG_PATH="/deploy/resources/config/server-config-${BUILD_ENV}.edn" -WORKDIR "${ETHLANCE_SERVER_ROOT}" - -COPY --from=build_server /build/ethlance/config/ui-config-${ETHLANCE_ENV}.edn /deploy/resources/config/ui-config-${ETHLANCE_ENV}.edn -COPY --from=build_server /build/ethlance/config/server-config-${ETHLANCE_ENV}.edn /deploy/resources/config/server-config-${ETHLANCE_ENV}.edn -COPY --from=build_server /build/ethlance/resources /deploy/resources -COPY --from=build_server /build/ethlance/server/node_modules /deploy/server/node_modules/ -COPY --from=build_server /build/ethlance/server/package.json /build/ethlance/server/out/ethlance_server* /deploy/server/ -COPY --from=build_server /build/ethlance/resources/public/contracts /deploy/ui/contracts/ -COPY --from=build_server /build/ethlance/ui/resources/public/* /deploy/ui/ -RUN echo -n "{:last-processed-block 9622279}" > ethlance-events.log -CMD [ "node", "ethlance_server.js" ] +# Final stage - minimal runtime image +FROM node:20-slim +ARG ETHLANCE_ENV +ENV ETHLANCE_ENV=$ETHLANCE_ENV \ + ETHLANCE_SOURCE_ROOT=/build/ethlance +ENV ETHLANCE_SERVER_ROOT=/build/ethlance/server \ + SMART_CONTRACTS_BUILD_PATH="${ETHLANCE_SOURCE_ROOT}/resources/public/contracts/build" \ + SMART_CONTRACTS_PATH="${ETHLANCE_SOURCE_ROOT}/shared/src/ethlance/shared/smart_contracts_${ETHLANCE_ENV}.cljs" \ + UI_CONFIG_PATH="/build/ethlance/config/ui-config-prod.edn" \ + ETHLANCE_CONFIG_PATH="/build/ethlance/config/server-config-prod.edn" + +WORKDIR ${ETHLANCE_SERVER_ROOT} + +COPY --from=builder /build /build +# Initialize events log +RUN echo -n "{:last-processed-block 27409455}" > ethlance-events.log + +EXPOSE 6300 +CMD ["node", "out/ethlance_server.js"] diff --git a/docker-builds/ui/Dockerfile b/docker-builds/ui/Dockerfile index 7b664409..9054f1b8 100644 --- a/docker-builds/ui/Dockerfile +++ b/docker-builds/ui/Dockerfile @@ -1,52 +1,52 @@ -FROM --platform=amd64 487920318758.dkr.ecr.us-west-2.amazonaws.com/cljs-web3-ci:node-20.18.1 as init -ARG BUILD_ENV=qa -ARG ETHLANCE_DEPLOY_SEED="sign bachelor state zoo expire boat morning situate scene unveil oven crew" +ARG DOCKER_REGISTRY +FROM $DOCKER_REGISTRY/cljs-web3-ci:node-20.18.1 AS builder + +ARG ETHLANCE_ENV +ARG ETHLANCE_DEPLOY_SEED USER root -ENV ETHLANCE_COMPONENT="ui" -ENV ETHLANCE_SOURCE_ROOT=/build/ethlance -ENV ETHLANCE_SERVER_ROOT=/build/ethlance/server -ENV ETHLANCE_ENV=$BUILD_ENV -ENV DEPLOY_TARGET=/deploy/ -ENV ETHLANCE_DEPLOY_SEED="${ETHLANCE_DEPLOY_SEED}" -ENV SMART_CONTRACTS_PATH="/build/ethlance/shared/src/ethlance/shared/smart_contracts_${BUILD_ENV}.cljs" -ENV SMART_CONTRACTS_BUILD_PATH="/build/ethlance/resources/public/contracts/build" -ENV ETHLANCE_CONFIG_PATH="/build/ethlance/config/${ETHLANCE_COMPONENT}-config-${BUILD_ENV}.edn" - -WORKDIR /build/ethlance +# Set environment variables +ENV ETHLANCE_SOURCE_ROOT=/build/ethlance \ + ETHLANCE_ENV=$ETHLANCE_ENV \ + BUILD_ENV=$ETHLANCE_ENV +ENV ETHLANCE_SERVER_ROOT=${ETHLANCE_SOURCE_ROOT}/server \ + ETHLANCE_DEPLOY_SEED=${ETHLANCE_DEPLOY_SEED} \ + SMART_CONTRACTS_BUILD_PATH="${ETHLANCE_SOURCE_ROOT}/resources/public/contracts/build" \ + SMART_CONTRACTS_PATH="${ETHLANCE_SOURCE_ROOT}/shared/src/ethlance/shared/smart_contracts_${ETHLANCE_ENV}.cljs" \ + ETHLANCE_CONFIG_PATH="${ETHLANCE_SOURCE_ROOT}/config/server-config-${ETHLANCE_ENV}.edn" \ + UI_CONFIG_PATH="${ETHLANCE_SOURCE_ROOT}/config/ui-config-${ETHLANCE_ENV}.edn" + +# Set working directory and prepare source files +WORKDIR ${ETHLANCE_SOURCE_ROOT} COPY . . +COPY ethlance-config/config "${ETHLANCE_SOURCE_ROOT}/config" -RUN cat "${SMART_CONTRACTS_PATH}" - -# WORKDIR /build/ethlance-config -# COPY config . - -WORKDIR /build -COPY ethlance-config/config /build/ethlance/config -RUN git clone https://github.com/district0x/d0x-libs +# Clone required dependencies +RUN git clone --depth 1 https://github.com/district0x/d0x-libs /build/d0x-libs +# Build stage - compile contracts and UI +# Compile contracts +RUN yarn install && ETHLANCE_ENV="${ETHLANCE_ENV}" npx truffle compile -ARG BUILD_ENV=qa -ARG ETHLANCE_ENV=qa -FROM init as build_stage -ENV ETHLANCE_ENV=qa -WORKDIR $ETHLANCE_SOURCE_ROOT -RUN yarn install && ETHLANCE_ENV="${BUILD_ENV}" npx truffle compile - -WORKDIR $ETHLANCE_SOURCE_ROOT/ui +# Build UI components +WORKDIR ${ETHLANCE_SOURCE_ROOT}/ui RUN yarn install && \ - ETHLANCE_ENV="${BUILD_ENV}" npx shadow-cljs release dev-ui && \ + ETHLANCE_ENV="${ETHLANCE_ENV}" npx shadow-cljs release dev-ui && \ ./node_modules/less/bin/lessc resources/public/less/main.less resources/public/css/main.css --verbose +# Final stage - using nginx to serve the UI FROM nginx:alpine -ENV ETHLANCE_ENV=qa +ARG ETHLANCE_ENV +ENV ETHLANCE_ENV=$ETHLANCE_ENV +# Configure nginx COPY docker-builds/ui/ethlance.conf /etc/nginx/conf.d/ COPY docker-builds/ui/default.conf /etc/nginx/conf.d/default.conf COPY docker-builds/ui/nginx.conf /etc/nginx/nginx.conf -COPY --from=build_stage /build/ethlance/resources /deploy/resources -COPY --from=build_stage /build/ethlance/resources/public/contracts /deploy/ui/contracts/ -COPY --from=build_stage /build/ethlance/ui/resources/public /deploy/ui/ - +# Copy built assets from builder stage +COPY --from=builder /build/ethlance/resources /deploy/resources +COPY --from=builder /build/ethlance/resources/public/contracts /deploy/ui/contracts/ +COPY --from=builder /build/ethlance/ui/resources/public /deploy/ui/ +COPY --from=builder /build/ethlance/config /deploy/config EXPOSE 80 diff --git a/docker-compose.yml b/docker-compose.yml index f35d1536..d09b59ba 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,20 +8,22 @@ services: POSTGRES_PASSWORD: test POSTGRES_DB: test server: - image: ethlance-server:latest + image: ${DOCKER_REGISTRY}/ethlance-server:latest container_name: ethlance-server build: context: . dockerfile: "docker-builds/server/Dockerfile" args: - ETHLANCE_ENV: "qa" + ETHLANCE_ENV: ${ETHLANCE_ENV} + DOCKER_REGISTRY: ${DOCKER_REGISTRY} + ETHLANCE_DEPLOY_SEED: ${ETHLANCE_DEPLOY_SEED} ports: - 6300:6300 command: ["node", "ethlance_server.js"] environment: - ETHLANCE_ENV: "qa" + ETHLANCE_ENV: ${ETHLANCE_ENV} ui: - image: ethlance-ui:latest + image: ${DOCKER_REGISTRY}/ethlance-ui:latest container_name: ethlance-ui ports: - 9080:80 @@ -29,21 +31,10 @@ services: context: . dockerfile: "docker-builds/ui/Dockerfile" args: - ETHLANCE_ENV: "qa" + ETHLANCE_ENV: ${ETHLANCE_ENV} + DOCKER_REGISTRY: ${DOCKER_REGISTRY} + ETHLANCE_DEPLOY_SEED: ${ETHLANCE_DEPLOY_SEED} depends_on: - server environment: - ETHLANCE_ENV: "qa" - - # Collect deps, compiles and deploys contracts - base: - image: ethlance-base:local - container_name: ethlance-base - command: ["exit", "0"] - build: - context: . - dockerfile: "docker-builds/base/Dockerfile" - args: - ETHLANCE_ENV: "qa" - environment: - ETHLANCE_ENV: "qa" + ETHLANCE_ENV: ${ETHLANCE_ENV} \ No newline at end of file diff --git a/ui/src/ethlance/ui/config.cljs b/ui/src/ethlance/ui/config.cljs index de48e004..26912e8e 100644 --- a/ui/src/ethlance/ui/config.cljs +++ b/ui/src/ethlance/ui/config.cljs @@ -84,7 +84,7 @@ (def config-prod - {:server-config {:url "http://api.ethlance.com"}}) + {:server-config {:url "https://api.ethlance.io/config"}}) (defn get-config