From 277734c621f30b05c6c8b82d155c0d5c646b5835 Mon Sep 17 00:00:00 2001
From: Naveen V <velnaveen99@gmail.com>
Date: Wed, 1 Nov 2023 06:18:13 +0000
Subject: [PATCH] update docker to build from source

---
 .github/workflows/node-cosmos-docker.yml | 48 ++++++++++++++++++++----
 packages/node/Dockerfile                 | 29 ++++++++------
 2 files changed, 58 insertions(+), 19 deletions(-)

diff --git a/.github/workflows/node-cosmos-docker.yml b/.github/workflows/node-cosmos-docker.yml
index c76a61da9..eccdbc79d 100644
--- a/.github/workflows/node-cosmos-docker.yml
+++ b/.github/workflows/node-cosmos-docker.yml
@@ -1,15 +1,40 @@
 name: "Node-to-docker"
 on:
+  release:
+    types:
+      - published
   workflow_dispatch:
     inputs:
       isLatest:
         description: 'Add latest tag'
         default: 'true'
-        require: true
+        required: true
 
 jobs:
+  check:
+    runs-on: ubuntu-latest
+    outputs:
+      changes_found: ${{ steps.check_changes.outputs.changes_found }}
+    steps:
+      - uses: actions/checkout@v2
+      - name: Check for package changes and commit message
+        id: check_changes
+        run: |
+          if [[ "${{ github.event_name }}" == "release" ]]
+          then
+            COMMIT_MESSAGE=$(git log -1 --pretty=format:"%s")
+            if [[ $COMMIT_MESSAGE == "[release]"* ]] && git diff --name-only HEAD~1 HEAD -- './packages/node/package.json'
+            then
+              echo "::set-output name=changes_found::true"
+            else
+              echo "::set-output name=changes_found::false"
+            fi
+          else
+            echo "::set-output name=changes_found::true"
+          fi
   node-build-push-docker:
-
+    needs: check
+    if: needs.check.outputs.changes_found == 'true'
     runs-on: ubuntu-latest
     steps:
       - uses: actions/checkout@v2
@@ -35,8 +60,12 @@ jobs:
         run: |
           sh .github/workflows/scripts/nodeVersion.sh
 
+      - run: yarn
+      - name: build
+        run: yarn build
+
       - name: Build and push
-        if: github.event.inputs.isLatest == 'false'
+        if: github.event_name == 'workflow_dispatch' && github.event.inputs.isLatest == 'false'
         uses: docker/build-push-action@v2
         with:
           push: true
@@ -46,7 +75,7 @@ jobs:
           build-args: RELEASE_VERSION=${{ steps.get-node-version.outputs.NODE_VERSION }}
 
       - name: Build and push
-        if: github.event.inputs.isLatest == 'true'
+        if: github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.isLatest == 'true')
         uses: docker/build-push-action@v2
         with:
           push: true
@@ -60,7 +89,8 @@ jobs:
 
 
   node-build-push-docker-subquery:
-
+    needs: check
+    if: needs.check.outputs.changes_found == 'true'
     runs-on: ubuntu-latest
     steps:
       - uses: actions/checkout@v2
@@ -86,8 +116,12 @@ jobs:
         run: |
           sh .github/workflows/scripts/nodeVersion.sh
 
+      - run: yarn
+      - name: build
+        run: yarn build
+
       - name: Build and push
-        if: github.event.inputs.isLatest == 'false'
+        if: github.event_name == 'workflow_dispatch' && github.event.inputs.isLatest == 'false'
         uses: docker/build-push-action@v2
         with:
           push: true
@@ -97,7 +131,7 @@ jobs:
           build-args: RELEASE_VERSION=${{ steps.get-node-version.outputs.NODE_VERSION }}
 
       - name: Build and push
-        if: github.event.inputs.isLatest == 'true'
+        if: github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.isLatest == 'true')
         uses: docker/build-push-action@v2
         with:
           push: true
diff --git a/packages/node/Dockerfile b/packages/node/Dockerfile
index 769dfb9f1..6e68ca1fb 100644
--- a/packages/node/Dockerfile
+++ b/packages/node/Dockerfile
@@ -1,14 +1,19 @@
-# production images
-FROM node:18 as builder
-ARG RELEASE_VERSION
-ENTRYPOINT ["subql-node-cosmos"]
-RUN npm i -g --unsafe-perm @subql/node-cosmos@${RELEASE_VERSION}
+# Build stage
+FROM node:18-alpine as builder
+WORKDIR /app
+COPY ./packages/node ./
+RUN npm install -g --force yarn@latest && \
+    yarn pack --filename app.tgz && \
+    rm -rf /root/.npm /root/.cache
 
+# Production stage
 FROM node:18-alpine
-ENV TZ utc
-
-RUN apk add --no-cache tini git curl
-COPY --from=builder /usr/local/lib/node_modules /usr/local/lib/node_modules
-
-ENTRYPOINT ["/sbin/tini", "--", "/usr/local/lib/node_modules/@subql/node-cosmos/bin/run"]
-CMD ["-f","/app"]
+RUN apk add --no-cache tini curl git
+COPY --from=builder /app/app.tgz /app.tgz
+RUN tar -xzvf /app.tgz --strip 1 && \
+    rm /app.tgz && \
+    yarn install --production && \
+    yarn cache clean && \
+    rm -rf /root/.npm /root/.cache
+ENTRYPOINT ["/sbin/tini", "--", "bin/run"]
+CMD ["-f","/app"]
\ No newline at end of file