From b7fc113af72a9df1e23b071f09cb9e2f9da9adc7 Mon Sep 17 00:00:00 2001
From: PauloVTSilva <pilutechinformatica@gmail.com>
Date: Sat, 11 May 2024 21:50:54 -0300
Subject: [PATCH] feat: add workflow for push docker image

---
 .dockerignore                |  9 +++++++
 .github/workflows/build.yml  |  2 +-
 .github/workflows/deploy.yml | 46 ++++++++++++++++++++++++++++++++++++
 .github/workflows/test.yml   |  2 +-
 .npmrc                       |  6 ++---
 Dockerfile                   | 28 ++++++++++++++++++++++
 package.json                 |  4 +++-
 7 files changed, 91 insertions(+), 6 deletions(-)
 create mode 100644 .dockerignore
 create mode 100644 .github/workflows/deploy.yml
 create mode 100644 Dockerfile

diff --git a/.dockerignore b/.dockerignore
new file mode 100644
index 0000000..cc9df7a
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1,9 @@
+.git 
+dist
+.github
+.husky 
+node_modules
+.gitignore
+README.md 
+.editorconfig
+.env
\ No newline at end of file
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index d64984b..88c3ba9 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -11,7 +11,7 @@ jobs:
     runs-on: ubuntu-latest
     steps:
       - name: Code Checkout
-        uses: actions/checkout@v3
+        uses: actions/checkout@v4
 
       - name: Setup deps
         uses: ./.github/actions/install-deps
diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml
new file mode 100644
index 0000000..c21b17f
--- /dev/null
+++ b/.github/workflows/deploy.yml
@@ -0,0 +1,46 @@
+name: DEPLOY-CI
+
+on:
+  push:
+    branches:
+      - master
+
+jobs:
+  build-and-push:
+    name: "Build and Push Image"
+    runs-on: ubuntu-latest
+
+    steps:
+      - name: Checkout
+        uses: actions/checkout@v4
+
+      - name: Generate sha
+        id: generate_sha
+        run: |
+          SHA=$(echo $GITHUB_SHA | head -c7)
+          echo "sha=$SHA" >> "$GITHUB_OUTPUT"
+
+      - name: Build docker image
+        run: docker build -t devhatt/discord-bot:${{steps.generate_sha.outputs.sha}} .
+
+      - name: Log into the container registry
+        uses: docker/login-action@v3
+        with:
+          username: ${{secrets.DOCKER_HUB_USERNAME}}
+          password: ${{secrets.DOCKER_HUB_PASSWORD}}
+
+      - name: Push Image
+        run: |
+          docker push devhatt/discord-bot:${{steps.generate_sha.outputs.sha}}
+          docker tag devhatt/discord-bot:${{steps.generate_sha.outputs.sha}} devhatt/discord-bot:latest
+          docker push devhatt/discord-bot:latest
+
+      - name: Update image tag
+        uses: fjogeleit/yaml-update-action@main
+        with:
+          branch: main
+          valueFile: "deploy/values.yaml"
+          propertyPath: "image.tag"
+          value: "${{steps.generate_sha.outputs.sha}}"
+          commitChange: true
+          message: "new: Updated tag in values helm"
\ No newline at end of file
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 2111bbd..f7bafa1 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -11,7 +11,7 @@ jobs:
     runs-on: ubuntu-latest
     steps:
       - name: Code Checkout
-        uses: actions/checkout@v3
+        uses: actions/checkout@v4
 
       - name: Setup deps
         uses: ./.github/actions/install-deps
diff --git a/.npmrc b/.npmrc
index f6a3992..701924c 100644
--- a/.npmrc
+++ b/.npmrc
@@ -43,11 +43,11 @@ save-workspace-protocol=rolling
 # pnpm will automatically install the specified version of Node.js and use it 
 # for running pnpm run commands or the pnpm node command.
 # https://pnpm.io/npmrc#save-workspace-protocol
-use-node-version=20.10.0
+use-node-version=20.13.1
 
 # Prevent contributors of your project from adding new incompatible dependencies
 # This way, even if someone is using Node.js v16, they will not be able to install
-# a new dependency that doesn't support Node.js v20.10.0
+# a new dependency that doesn't support Node.js v20.13.1
 # https://pnpm.io/npmrc#use-node-version
-node-version=20.10.0
+node-version=20.13.1
 engine-strict=true
\ No newline at end of file
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..1be407c
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,28 @@
+FROM node:20.13.1 AS base
+
+RUN npm install -g pnpm@8
+
+FROM base AS depencencies
+WORKDIR /usr/src/app
+COPY package.json pnpm-lock.yaml ./
+
+RUN pnpm install --frozen-lockfile --ignore-scripts
+
+FROM base AS build
+WORKDIR /usr/src/app
+COPY . ./
+COPY --from=depencencies /usr/src/app/node_modules ./node_modules
+RUN pnpm run build
+RUN pnpm prune --prod 
+
+FROM node:20.13.1-alpine3.19 AS deploy
+
+WORKDIR /usr/src/app
+
+COPY --from=build /usr/src/app/dist ./dist
+COPY --from=build /usr/src/app/node_modules ./node_modules
+COPY --from=build /usr/src/app/package.json ./package.json
+
+EXPOSE 3000
+
+CMD ["node", "dist/main.js"]
\ No newline at end of file
diff --git a/package.json b/package.json
index 44d594d..220a040 100644
--- a/package.json
+++ b/package.json
@@ -4,11 +4,13 @@
   "description": "",
   "main": "src/main.ts",
   "engines": {
-    "node": "20.10.0",
+    "node": "20.13.1",
     "pnpm": "8.x"
   },
   "scripts": {
     "build": "tsup",
+    "build:watch": "tsup --watch",
+    "start": "node dist/main.js",
     "dev": "tsx src/main.ts",
     "test": "jest",
     "test:watch": "jest --watch",