278 lines
9.1 KiB
YAML
278 lines
9.1 KiB
YAML
name: Hostinger Deploy
|
||
|
||
on:
|
||
push:
|
||
branches: [ {{BRANCH}} ]
|
||
workflow_dispatch:
|
||
|
||
jobs:
|
||
tests:
|
||
name: Run Tests
|
||
runs-on: ubuntu-latest
|
||
|
||
steps:
|
||
- name: Checkout code
|
||
uses: actions/checkout@v4
|
||
|
||
- name: Setup PHP
|
||
uses: shivammathur/setup-php@v2
|
||
with:
|
||
php-version: '{{PHP_VERSION}}'
|
||
extensions: mbstring, xml, bcmath, pdo_mysql, pdo_sqlite, gd, zip
|
||
coverage: none
|
||
|
||
- name: Copy .env
|
||
run: |
|
||
if [ -f .env.example ]; then
|
||
cp .env.example .env
|
||
else
|
||
echo "⚠️ .env.example not found, creating minimal .env"
|
||
echo "APP_ENV=testing" > .env
|
||
echo "APP_KEY=" >> .env
|
||
fi
|
||
|
||
- name: Install Composer Dependencies
|
||
run: composer install --prefer-dist --no-interaction --no-progress
|
||
|
||
- name: Generate Application Key
|
||
run: php artisan key:generate --ansi
|
||
|
||
- name: Run PHPUnit Tests
|
||
continue-on-error: true
|
||
run: |
|
||
if [ -f phpunit.xml ] || [ -f phpunit.xml.dist ]; then
|
||
php artisan test || php vendor/bin/phpunit
|
||
else
|
||
echo "⚠️ No PHPUnit configuration found, skipping tests"
|
||
fi
|
||
|
||
- name: Run Static Analysis (PHPStan/Pint)
|
||
continue-on-error: true
|
||
run: |
|
||
if [ -f vendor/bin/phpstan ]; then
|
||
vendor/bin/phpstan analyse --no-progress || echo "⚠️ PHPStan check failed or not configured"
|
||
fi
|
||
if [ -f vendor/bin/pint ]; then
|
||
vendor/bin/pint --test || echo "⚠️ Pint check failed or not configured"
|
||
fi
|
||
|
||
build-assets:
|
||
name: Build Frontend Assets
|
||
runs-on: ubuntu-latest
|
||
continue-on-error: true
|
||
|
||
steps:
|
||
- name: Checkout code
|
||
uses: actions/checkout@v4
|
||
|
||
- name: Setup PHP
|
||
uses: shivammathur/setup-php@v2
|
||
with:
|
||
php-version: '{{PHP_VERSION}}'
|
||
extensions: mbstring, xml, bcmath, pdo_mysql, pdo_sqlite, gd, zip
|
||
coverage: none
|
||
|
||
- name: Copy .env
|
||
run: |
|
||
if [ -f .env.example ]; then
|
||
cp .env.example .env
|
||
else
|
||
echo "⚠️ .env.example not found, creating minimal .env"
|
||
echo "APP_ENV=testing" > .env
|
||
echo "APP_KEY=" >> .env
|
||
fi
|
||
|
||
- name: Install Composer Dependencies
|
||
run: composer install --prefer-dist --no-interaction --no-progress
|
||
|
||
- name: Generate Application Key
|
||
run: php artisan key:generate --ansi
|
||
|
||
- name: Check for package.json
|
||
id: check_package
|
||
run: |
|
||
if [ -f package.json ]; then
|
||
echo "exists=true" >> $GITHUB_OUTPUT
|
||
else
|
||
echo "exists=false" >> $GITHUB_OUTPUT
|
||
fi
|
||
|
||
- name: Setup Node.js
|
||
if: steps.check_package.outputs.exists == 'true'
|
||
uses: actions/setup-node@v4
|
||
with:
|
||
node-version: '20'
|
||
cache: ${{ hashFiles('package-lock.json') != '' && 'npm' || '' }}
|
||
|
||
- name: Install NPM Dependencies
|
||
if: steps.check_package.outputs.exists == 'true'
|
||
continue-on-error: true
|
||
run: |
|
||
if [ -f package-lock.json ]; then
|
||
npm ci
|
||
else
|
||
echo "ℹ️ No package-lock.json found, using npm install"
|
||
npm install
|
||
fi
|
||
|
||
- name: Build Assets
|
||
if: steps.check_package.outputs.exists == 'true'
|
||
continue-on-error: true
|
||
run: |
|
||
if grep -q "\"build\"" package.json || grep -q "\"prod\"" package.json; then
|
||
npm run build || npm run prod || echo "⚠️ Build script not found or failed"
|
||
else
|
||
echo "⚠️ No build script found in package.json"
|
||
fi
|
||
|
||
- name: Upload Built Assets
|
||
if: steps.check_package.outputs.exists == 'true'
|
||
uses: actions/upload-artifact@v4
|
||
with:
|
||
name: build-assets
|
||
path: public/build/
|
||
if-no-files-found: ignore
|
||
retention-days: 1
|
||
|
||
deploy:
|
||
name: Deploy to Hostinger
|
||
runs-on: ubuntu-latest
|
||
needs: [tests, build-assets]
|
||
if: success() || failure() # Deploy even if tests fail (you can change to 'success()' to require passing tests)
|
||
|
||
steps:
|
||
- name: Checkout code
|
||
uses: actions/checkout@v4
|
||
|
||
- name: Setup PHP
|
||
uses: shivammathur/setup-php@v2
|
||
with:
|
||
php-version: '{{PHP_VERSION}}'
|
||
extensions: mbstring, xml, bcmath, pdo_mysql
|
||
|
||
- name: Download Built Assets
|
||
uses: actions/download-artifact@v4
|
||
with:
|
||
name: build-assets
|
||
path: public/build/
|
||
continue-on-error: true
|
||
|
||
- name: Setup Node.js (for asset building)
|
||
if: hashFiles('package.json') != ''
|
||
uses: actions/setup-node@v4
|
||
with:
|
||
node-version: '20'
|
||
cache: ${{ hashFiles('package-lock.json') != '' && 'npm' || '' }}
|
||
|
||
- name: Build Frontend Assets (if needed)
|
||
continue-on-error: true
|
||
if: hashFiles('package.json') != ''
|
||
run: |
|
||
echo "📦 Installing NPM dependencies..."
|
||
if [ -f package-lock.json ]; then
|
||
npm ci --prefer-offline --no-audit || npm install --prefer-offline --no-audit
|
||
else
|
||
echo "ℹ️ No package-lock.json found, using npm install"
|
||
npm install --prefer-offline --no-audit
|
||
fi
|
||
|
||
if grep -q "\"build\"" package.json || grep -q "\"prod\"" package.json; then
|
||
echo "🔨 Building assets for production..."
|
||
npm run build || npm run prod || echo "⚠️ Build script not found or failed"
|
||
echo "ℹ️ Note: Built assets should be committed to your repository for deployment"
|
||
echo " (NPM may not be available on the remote server)"
|
||
else
|
||
echo "ℹ️ No build script found in package.json"
|
||
fi
|
||
|
||
- name: Install SSH key
|
||
run: |
|
||
mkdir -p ~/.ssh/
|
||
echo "${{ secrets.SSH_KEY }}" > ~/.ssh/id_rsa
|
||
chmod 600 ~/.ssh/id_rsa
|
||
ssh-keyscan -t rsa -p ${{ secrets.SSH_PORT }} ${{ secrets.SSH_HOST }} >> ~/.ssh/known_hosts
|
||
|
||
- name: Copy Build Files to Target Server
|
||
continue-on-error: true
|
||
run: |
|
||
if [ -d "${{ github.workspace }}/public/build" ]; then
|
||
rsync -r -e "ssh -p ${{ secrets.SSH_PORT }}" ${{ github.workspace }}/public/build/ ${{ secrets.SSH_USERNAME }}@${{ secrets.SSH_HOST }}:~/domains/${{ secrets.WEBSITE_FOLDER }}/public/build
|
||
else
|
||
echo "ℹ️ No build directory found, skipping asset copy"
|
||
fi
|
||
|
||
- name: Deploy to Hostinger Server
|
||
uses: appleboy/ssh-action@master
|
||
with:
|
||
host: ${{ secrets.SSH_HOST }}
|
||
username: ${{ secrets.SSH_USERNAME }}
|
||
port: ${{ secrets.SSH_PORT }}
|
||
key: ${{ secrets.SSH_KEY }}
|
||
script: |
|
||
set -e
|
||
|
||
echo "🚀 Starting deployment..."
|
||
|
||
cd domains/${{ secrets.WEBSITE_FOLDER }}
|
||
|
||
# Ensure we're on the correct branch
|
||
git fetch origin
|
||
git checkout {{BRANCH}}
|
||
git reset --hard origin/{{BRANCH}}
|
||
|
||
echo "📦 Installing Composer dependencies..."
|
||
composer install --no-dev --optimize-autoloader --no-interaction --prefer-dist
|
||
|
||
# Note: Frontend assets should already be built and committed
|
||
# If you need to build on server, ensure npm/node is available
|
||
# and uncomment the following lines:
|
||
# if [ -f package.json ]; then
|
||
# npm ci --production && npm run build || npm run prod
|
||
# fi
|
||
|
||
echo "🔧 Running Laravel setup commands..."
|
||
|
||
# Run migrations
|
||
php artisan migrate --force
|
||
|
||
# Clear and cache configuration
|
||
php artisan config:clear
|
||
php artisan config:cache
|
||
|
||
# Clear and cache routes
|
||
php artisan route:clear
|
||
php artisan route:cache
|
||
|
||
# Clear and cache views
|
||
php artisan view:clear
|
||
php artisan view:cache
|
||
|
||
# Optimize application
|
||
php artisan optimize:clear
|
||
php artisan optimize
|
||
|
||
# Clear and cache events (if available)
|
||
php artisan event:clear || true
|
||
php artisan event:cache || true
|
||
|
||
# Restart queue workers (if using queues)
|
||
php artisan queue:restart || true
|
||
|
||
# Restart Horizon (if using Laravel Horizon)
|
||
php artisan horizon:terminate || true
|
||
|
||
# Clear application cache
|
||
php artisan cache:clear
|
||
|
||
# Clear OPcache (if available)
|
||
php artisan opcache:clear || true
|
||
|
||
echo "✅ Deployment completed successfully!"
|
||
|
||
# Show deployment info
|
||
echo ""
|
||
echo "📊 Deployment Information:"
|
||
echo " Branch: {{BRANCH}}"
|
||
echo " Commit: $(git rev-parse --short HEAD)"
|
||
echo " Date: $(date)"
|