|
| 1 | +name: Build and Deploy |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [lince/test-deploy] |
| 6 | + workflow_dispatch: # Allows manual triggering |
| 7 | + |
| 8 | +jobs: |
| 9 | + build-and-deploy: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + environment: production # This tells GitHub to use the production environment secrets |
| 12 | + |
| 13 | + steps: |
| 14 | + - name: Checkout code |
| 15 | + uses: actions/checkout@v4 |
| 16 | + with: |
| 17 | + # fetch-depth: 0 is recommended for a more reliable diff across multiple commits in a push |
| 18 | + fetch-depth: 0 |
| 19 | + |
| 20 | + - name: Detect changed sections |
| 21 | + id: detect-changes |
| 22 | + run: | |
| 23 | + echo "🔍 Detecting changes based on custom build rules..." |
| 24 | +
|
| 25 | + # Use a more robust diff command that covers all commits in a push |
| 26 | + all_changed=$(git diff --name-only ${{ github.event.before }} ${{ github.event.after }} || git diff --name-only HEAD~1 HEAD) |
| 27 | +
|
| 28 | + if [ -z "$all_changed" ]; then |
| 29 | + echo "❌ No changes detected. Skipping build and deploy." |
| 30 | + echo "should_deploy=false" >> $GITHUB_OUTPUT |
| 31 | + exit 0 |
| 32 | + fi |
| 33 | +
|
| 34 | + echo "📁 All changed files:" |
| 35 | + echo "$all_changed" | sed 's/^/ - /' |
| 36 | +
|
| 37 | + changed_dirs="" |
| 38 | +
|
| 39 | + # Helper function to add a directory to the list if it's not already there |
| 40 | + add_dir() { |
| 41 | + if [[ ! "$changed_dirs" =~ (^|[[:space:]])$1($|[[:space:]]) ]]; then |
| 42 | + changed_dirs="$changed_dirs $1" |
| 43 | + fi |
| 44 | + } |
| 45 | +
|
| 46 | + for file in $all_changed; do |
| 47 | + case "$file" in |
| 48 | + frontend/src/pages/t/*) |
| 49 | + add_dir "t" |
| 50 | + ;; |
| 51 | + frontend/src/pages/markdown_pages/tldr/*|frontend/src/pages/tldr/*) |
| 52 | + add_dir "markdown_pages" |
| 53 | + add_dir "tldr" |
| 54 | + ;; |
| 55 | + frontend/src/pages/html_pages/cheatsheets/*|frontend/src/pages/c/*) |
| 56 | + add_dir "html_pages" |
| 57 | + add_dir "c" |
| 58 | + ;; |
| 59 | + frontend/src/pages/svg_icons/*) |
| 60 | + add_dir "svg_icons" |
| 61 | + ;; |
| 62 | + frontend/src/pages/png_icons/*) |
| 63 | + # If png_icons changes, we build both png_icons and svg_icons |
| 64 | + add_dir "png_icons" |
| 65 | + add_dir "svg_icons" |
| 66 | + ;; |
| 67 | + frontend/src/pages/emojies/*) |
| 68 | + add_dir "emojies" |
| 69 | + ;; |
| 70 | + frontend/public/*) |
| 71 | + # Public assets are served directly, just sync without rebuild |
| 72 | + add_dir "public_assets_only" |
| 73 | + ;; |
| 74 | + frontend/src/*) |
| 75 | + # Other src changes (components, layouts, styles, etc.) need full rebuild |
| 76 | + add_dir "full_rebuild" |
| 77 | + ;; |
| 78 | + *) |
| 79 | + # You can add a default case here if needed, for example, to build everything |
| 80 | + # echo "Change detected in unhandled path: $file" |
| 81 | + ;; |
| 82 | + esac |
| 83 | + done |
| 84 | +
|
| 85 | + # Clean up leading/trailing whitespace |
| 86 | + changed_dirs=$(echo "$changed_dirs" | xargs) |
| 87 | +
|
| 88 | + if [ -n "$changed_dirs" ]; then |
| 89 | + echo "📦 Changed sections to build: $changed_dirs" |
| 90 | + echo "should_deploy=true" >> $GITHUB_OUTPUT |
| 91 | + echo "changed_sections=$changed_dirs" >> $GITHUB_OUTPUT |
| 92 | + else |
| 93 | + echo "❌ No changes matched the build rules. Skipping deployment." |
| 94 | + echo "should_deploy=false" >> $GITHUB_OUTPUT |
| 95 | + fi |
| 96 | +
|
| 97 | + - name: Setup Node.js |
| 98 | + if: steps.detect-changes.outputs.should_deploy == 'true' |
| 99 | + uses: actions/setup-node@v4 |
| 100 | + with: |
| 101 | + node-version: "18" |
| 102 | + cache: "npm" |
| 103 | + cache-dependency-path: ./frontend/package-lock.json |
| 104 | + |
| 105 | + - name: Exclude unchanged sections from build |
| 106 | + if: steps.detect-changes.outputs.should_deploy == 'true' |
| 107 | + run: | |
| 108 | + echo "🔧 Excluding unchanged sections from build..." |
| 109 | + cd frontend/src/pages |
| 110 | +
|
| 111 | + changed_sections="${{ steps.detect-changes.outputs.changed_sections }}" |
| 112 | + echo "Building only these sections: $changed_sections" |
| 113 | +
|
| 114 | + # Only exclude sections if we're doing selective page builds |
| 115 | + if [[ "$changed_sections" != "full_rebuild" && "$changed_sections" != "public_assets_only" ]]; then |
| 116 | + for dir in */; do |
| 117 | + if [ -d "$dir" ]; then |
| 118 | + dir_name=${dir%/} |
| 119 | + if [[ "$changed_sections" =~ (^|[[:space:]])$dir_name($|[[:space:]]) ]] || [[ "$dir_name" == _* ]]; then |
| 120 | + echo "✅ Including: $dir_name" |
| 121 | + else |
| 122 | + echo "❌ Excluding: $dir_name -> _$dir_name" |
| 123 | + mv "$dir" "_$dir" |
| 124 | + fi |
| 125 | + fi |
| 126 | + done |
| 127 | + elif [[ "$changed_sections" == "full_rebuild" ]]; then |
| 128 | + # For components/lib changes, exclude tldr but build everything else |
| 129 | + echo "🔧 Building all sections except tldr (components/styles/lib changed)" |
| 130 | + for dir in */; do |
| 131 | + if [ -d "$dir" ]; then |
| 132 | + dir_name=${dir%/} |
| 133 | + if [[ "$dir_name" == "tldr" ]]; then |
| 134 | + echo "❌ Excluding: $dir_name -> _$dir_name" |
| 135 | + mv "$dir" "_$dir" |
| 136 | + else |
| 137 | + echo "✅ Including: $dir_name" |
| 138 | + fi |
| 139 | + fi |
| 140 | + done |
| 141 | + else |
| 142 | + echo "🔧 Building all sections (public assets only)" |
| 143 | + fi |
| 144 | +
|
| 145 | + - name: Install dependencies and build |
| 146 | + if: steps.detect-changes.outputs.should_deploy == 'true' && steps.detect-changes.outputs.changed_sections != 'public_assets_only' |
| 147 | + run: | |
| 148 | + echo "📦 Installing dependencies and building..." |
| 149 | + cd frontend |
| 150 | + npm install |
| 151 | + npx astro build |
| 152 | +
|
| 153 | + - name: Restore original folder structure |
| 154 | + if: always() && steps.detect-changes.outputs.should_deploy == 'true' |
| 155 | + run: | |
| 156 | + echo "🔄 Restoring original folder structure..." |
| 157 | + cd frontend/src/pages |
| 158 | +
|
| 159 | + for dir in _*/; do |
| 160 | + if [ -d "$dir" ]; then |
| 161 | + original_name=${dir#_} |
| 162 | + original_name=${original_name%/} |
| 163 | + echo "Restoring _$original_name to $original_name" |
| 164 | + mv "$dir" "$original_name/" |
| 165 | + fi |
| 166 | + done |
| 167 | +
|
| 168 | + - name: Setup SSH |
| 169 | + if: steps.detect-changes.outputs.should_deploy == 'true' |
| 170 | + uses: webfactory/ssh-agent@v0.8.0 |
| 171 | + with: |
| 172 | + ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} |
| 173 | + |
| 174 | + - name: Add server to known hosts |
| 175 | + if: steps.detect-changes.outputs.should_deploy == 'true' |
| 176 | + run: | |
| 177 | + ssh-keyscan -H ${{ secrets.SSH_HOST }} >> ~/.ssh/known_hosts |
| 178 | +
|
| 179 | + - name: Deploy to server |
| 180 | + if: steps.detect-changes.outputs.should_deploy == 'true' |
| 181 | + run: | |
| 182 | + echo "🚀 Deploying to server..." |
| 183 | + echo "Deployed sections: ${{ steps.detect-changes.outputs.changed_sections }}" |
| 184 | +
|
| 185 | + changed_sections="${{ steps.detect-changes.outputs.changed_sections }}" |
| 186 | +
|
| 187 | + if [[ "$changed_sections" == "public_assets_only" ]]; then |
| 188 | + echo "📁 Only public assets changed - syncing files only" |
| 189 | + rsync -rvz --no-perms --no-owner --no-group --no-times --progress ./frontend/public/ ${{ secrets.SSH_USERNAME }}@${{ secrets.SSH_HOST }}:/tools/ |
| 190 | + else |
| 191 | + echo "📦 Full deployment" |
| 192 | + rsync -rvz --no-perms --no-owner --no-group --no-times --progress ./frontend/dist/ ${{ secrets.SSH_USERNAME }}@${{ secrets.SSH_HOST }}:/tools/ |
| 193 | + fi |
| 194 | +
|
| 195 | + - name: Skip deployment message |
| 196 | + if: steps.detect-changes.outputs.should_deploy == 'false' |
| 197 | + run: | |
| 198 | + echo "⏭️ Skipping deployment - no changes matched the build rules." |
0 commit comments