name: Linux arm64
run-name: Build and Deploy Portfolio

on: [push]

jobs:
  Build-and-Deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository code
        uses: actions/checkout@v3

      - name: Install dependencies and build
        run: |
          npm ci
          npm run build          

      - name: Set up SSH agent
        uses: webfactory/ssh-agent@v0.9.0
        with:
          ssh-private-key: ${{ secrets.SSH_KEY_PORTFOLIO }}

      - name: Add remote host to known_hosts
        run: |
          mkdir -p ~/.ssh
          ssh-keyscan -H git.ale-pri.com >> ~/.ssh/known_hosts

      - name: Upload build folder to temporary directory on remote server
        run: |
          scp -r build ubuntu@git.ale-pri.com:Poubelle/build

      - name: Deploy to final directory on remote server
        run: |
          ssh ubuntu@git.ale-pri.com << 'EOF'
          BUILD_TMP=/Poubelle/build
          DEPLOY_DIR=/var/www/portfolio

          sudo mkdir -p $DEPLOY_DIR

          sudo rsync -a --delete $BUILD_TMP/ $DEPLOY_DIR/

          sudo chown -R www-data:www-data $DEPLOY_DIR
          sudo chmod -R 755 $DEPLOY_DIR

          sudo rm -rf $BUILD_TMP

          sudo nginx -t && sudo systemctl reload nginx
          EOF
