From 3eba25740ca68d5dcc2e82c02e5900bb5a73152d Mon Sep 17 00:00:00 2001
From: itsscb <dev@itsscb.de>
Date: Fri, 6 Sep 2024 14:36:38 +0200
Subject: [PATCH] feat(workflow): improves cache and removes the need for
 --allow-dirty

Update prod.yml

Update .gitignore

feat(workflow): refactor that shuttle deploy works without dirty

ref(workflow): packages whole project for deployment

fix(workflow): ignore failed read on packaging

fix(workflow): package whole dir instead of zipping it

fix(workflow): fix invalid artifact name in job deploy

feat(workflow): refactor that shuttle deploy works without dirty (#34)

* feat(workflow): refactor that shuttle deploy works without dirty

* ref(workflow): packages whole project for deployment

* fix(workflow): ignore failed read on packaging

* fix(workflow): package whole dir instead of zipping it

* fix(workflow): fix invalid artifact name in job deploy

fix(workflow): invalid files in cache key

fix(workflow): cache not working
---
 .github/workflows/{prod.yml => deploy.yml} | 91 ++++++++++++++--------
 .github/workflows/test.yml                 | 41 ++++++++++
 .gitignore                                 |  6 ++
 frontend/package-lock.json                 | 12 +--
 frontend/package.json                      |  3 +-
 5 files changed, 113 insertions(+), 40 deletions(-)
 rename .github/workflows/{prod.yml => deploy.yml} (51%)
 create mode 100644 .github/workflows/test.yml

diff --git a/.github/workflows/prod.yml b/.github/workflows/deploy.yml
similarity index 51%
rename from .github/workflows/prod.yml
rename to .github/workflows/deploy.yml
index 6be93c4..80f7d2c 100644
--- a/.github/workflows/prod.yml
+++ b/.github/workflows/deploy.yml
@@ -14,37 +14,46 @@ jobs:
       - name: checkout
         uses: actions/checkout@v4
 
-      - name: install rust
-        uses: actions-rs/toolchain@v1
-        with:
-          profile: minimal
-          toolchain: stable
-
-      - name: cache trunk
-        uses: actions/cache@v4
-        with:
-          path: |
-            ~/.cargo/bin/trunk
-            ~/.cache/trunk/
-          key: ${{ runner.os }}-cargo-${{ hashFiles('~/.cargo/bin/trunk') }}
-          restore-keys: ${{ runner.os }}-trunk
-
       - name: cache dependencies
         uses: actions/cache@v4
         with:
           path: |
-            ~/.cargo/
+            ~/.rustup/
+            ~/.cargo/registry/
             ~/.cargo/bin/
+            ~/.cache/trunk/
+            frontend/target/
+            frontend/node_modules/
             target/
             node_modules/
-          key: ${{ runner.os }}-cargo-${{ hashFiles('./Cargo.lock') }}
+          key: ${{ runner.os }}-cargo-${{ hashFiles('./Cargo.lock', '~/.cargo/bin/trunk', './frontend/package-lock.json') }}
           restore-keys: ${{ runner.os }}-cargo-wordl
-          
+
+      - name: install rust
+        uses: actions-rs/toolchain@v1
+        with:
+          profile: minimal
+          toolchain: nightly
+        
       - name: add rust target wasm32-unknown-unknown
         run: rustup target add wasm32-unknown-unknown
-      
+  
+      - name: run frontend tests
+        working-directory: ./frontend/
+        run: |
+          cargo test
+
+      - name: run backend tests
+        working-directory: .
+        run: |
+          cargo test
+            
+      - name: install cargo-binstall
+        uses: cargo-bins/cargo-binstall@main
+
       - name: install trunk
-        run: cargo install trunk
+        run: |
+          cargo binstall trunk --no-confirm
 
       - name: install node.js
         uses: actions/setup-node@v4
@@ -52,51 +61,65 @@ jobs:
           node-version: 20
 
       - name: install tailwindcss
-        run: npm install -g tailwindcss
+        working-directory: ./frontend/
+        run: |
+          npm i -D
 
-      - name: list frontend dir (pre build) [DEBUG]
-        run: ls -la ./frontend/dist
+      - name: create ./frontend/dist dir
+        run: mkdir -p ./frontend/dist
 
       - name: build frontend
         working-directory: ./frontend/
         run: |
+          sed -i 's/tailwindcss/npx/' Trunk.toml
+          sed -i 's/command_arguments \= \[/command_arguments \= \[\"tailwindcss\",/' Trunk.toml
+          cat Trunk.toml
+          sed -i '/frontend\/public\/styles.css/s/^/#/' ../.gitignore
           trunk build --release
 
-      - name: list frontend dir (pre upload) [DEBUG]
-        run: ls -la ./frontend/dist
-
       - name: upload frontend artifacts
         uses: actions/upload-artifact@v4
         with:
           name: frontend-artifacts
           path: ./frontend/dist
 
-      - name: run tests
-        run: |
-          cargo test --verbose
-
   deploy:
     runs-on: ubuntu-latest
     needs: build
-    steps:
+    steps:          
       - name: checkout
         uses: actions/checkout@v4
 
+      - name: setup git
+        run: |
+          git config user.name github-actions
+          git config user.email github-actions@github.com
+
       - name: remove ./frontend/dist dir
         run: |
           rm -rf ./frontend/dist
-
+  
       - name: download frontend artifacts
         uses: actions/download-artifact@v4
         with:
           name: frontend-artifacts
           path: ./frontend/dist
+  
+      - name: remove items from .gitignore
+        run: |
+          sed -i '/frontend\/dist\//s/^/#/' .gitignore
+          sed -i '/frontend\/public\/styles.css/s/^/#/' .gitignore
+
+      - name: commit frontend build
+        run: |
+          git add frontend/dist -f
+          git add .gitignore
+          git commit -m "[${{github.run_attempt }}] ${{github.event.pusher}} > ${{ github.repository }}@${{ github.event.head_commit}}" || echo "No changes to commit"
 
       - name: List downloaded artifacts
-        run: ls -la ./frontend/dist
+        run: ls -laR ./frontend/dist
 
       - uses: shuttle-hq/deploy-action@main
         with:
-          # allow-dirty: "true"
           deploy-key: ${{ secrets.SHUTTLE_API_KEY }}
           working-directory: .
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
new file mode 100644
index 0000000..ddecba2
--- /dev/null
+++ b/.github/workflows/test.yml
@@ -0,0 +1,41 @@
+on:
+  pull_request:
+    branches:
+      - master
+
+permissions:
+  contents: write
+  issues: read
+
+jobs:
+  test:
+    runs-on: ubuntu-latest
+    steps:
+      - name: checkout
+        uses: actions/checkout@v4
+
+      - name: cache dependencies
+        uses: actions/cache@v4
+        with:
+          path: |
+            ~/.rustup/
+            ~/.cargo/registry/
+            ~/.cargo/bin/
+            ~/.cache/trunk/
+            frontend/target/
+            target/
+            frontend/node_modules/
+            node_modules/
+          key: ${{ runner.os }}-cargo-${{ hashFiles('./Cargo.lock', '~/.cargo/bin/trunk', './frontend/package-lock.json') }}
+          restore-keys: ${{ runner.os }}-cargo-wordl
+
+      - name: install rust
+        uses: actions-rs/toolchain@v1
+        with:
+          profile: minimal
+          toolchain: nightly
+          
+      - name: run frontend tests
+        working-directory: ./frontend/
+        run: |
+          cargo test
diff --git a/.gitignore b/.gitignore
index 9fcc62e..feb92c5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,6 +6,12 @@ target/
 frontend/target/
 frontend/dist/
 
+frontend/public/styles.css
+
+
+node_modules/
+frontend/node_modules/
+
 # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
 # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
 # Cargo.lock
diff --git a/frontend/package-lock.json b/frontend/package-lock.json
index 0ddb416..6f7197d 100644
--- a/frontend/package-lock.json
+++ b/frontend/package-lock.json
@@ -1,11 +1,12 @@
 {
-  "name": "wordl",
+  "name": "wordl-frontend",
   "lockfileVersion": 3,
   "requires": true,
   "packages": {
     "": {
+      "name": "wordl-frontend",
       "devDependencies": {
-        "tailwindcss": "^3.4.3"
+        "tailwindcss": "^3.4.10"
       }
     },
     "node_modules/@alloc/quick-lru": {
@@ -1176,10 +1177,11 @@
       }
     },
     "node_modules/tailwindcss": {
-      "version": "3.4.3",
-      "resolved": "https://registry.npmjs.org/tailwindcss/-/tailwindcss-3.4.3.tgz",
-      "integrity": "sha512-U7sxQk/n397Bmx4JHbJx/iSOOv5G+II3f1kpLpY2QeUv5DcPdcTsYLlusZfq1NthHS1c1cZoyFmmkex1rzke0A==",
+      "version": "3.4.10",
+      "resolved": "https://registry.npmjs.org/tailwindcss/-/tailwindcss-3.4.10.tgz",
+      "integrity": "sha512-KWZkVPm7yJRhdu4SRSl9d4AK2wM3a50UsvgHZO7xY77NQr2V+fIrEuoDGQcbvswWvFGbS2f6e+jC/6WJm1Dl0w==",
       "dev": true,
+      "license": "MIT",
       "dependencies": {
         "@alloc/quick-lru": "^5.2.0",
         "arg": "^5.0.2",
diff --git a/frontend/package.json b/frontend/package.json
index af38bdf..7a7b3cd 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -1,5 +1,6 @@
 {
+  "name": "wordl-frontend",
   "devDependencies": {
-    "tailwindcss": "^3.4.3"
+    "tailwindcss": "^3.4.10"
   }
 }