|
| 1 | +name: Deploy |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + environment: |
| 7 | + description: "Target deployment environment" |
| 8 | + required: true |
| 9 | + type: choice |
| 10 | + options: |
| 11 | + - staging |
| 12 | + - production |
| 13 | + default: staging |
| 14 | + skip_tests: |
| 15 | + description: "Skip tests before deploying (not recommended)" |
| 16 | + required: false |
| 17 | + type: boolean |
| 18 | + default: false |
| 19 | + |
| 20 | +jobs: |
| 21 | + # ────────────────────────────────────────────── |
| 22 | + # Job 1 – Full Test Suite |
| 23 | + # ────────────────────────────────────────────── |
| 24 | + test: |
| 25 | + name: "Test Suite (PHP 8.3)" |
| 26 | + runs-on: ubuntu-latest |
| 27 | + if: ${{ !inputs.skip_tests }} |
| 28 | + |
| 29 | + steps: |
| 30 | + - name: Checkout code |
| 31 | + uses: actions/checkout@v7 |
| 32 | + |
| 33 | + - name: Set up PHP 8.3 |
| 34 | + uses: shivammathur/setup-php@v2 |
| 35 | + with: |
| 36 | + php-version: "8.3" |
| 37 | + extensions: pdo, pdo_sqlite, mbstring, xml |
| 38 | + coverage: none |
| 39 | + |
| 40 | + - name: Cache Composer dependencies |
| 41 | + uses: actions/cache@v6 |
| 42 | + with: |
| 43 | + path: vendor |
| 44 | + key: composer-deploy-${{ hashFiles('composer.lock') }} |
| 45 | + restore-keys: composer-deploy- |
| 46 | + |
| 47 | + - name: Install Composer dependencies (with dev) |
| 48 | + run: composer install --no-progress --prefer-dist --no-interaction |
| 49 | + |
| 50 | + - name: Run syntax check |
| 51 | + run: find src/ public/ -name "*.php" -print0 | xargs -0 -n1 php -l |
| 52 | + |
| 53 | + - name: Run PHPUnit tests |
| 54 | + run: vendor/bin/phpunit --colors=always |
| 55 | + |
| 56 | + # ────────────────────────────────────────────── |
| 57 | + # Job 2 – Production Build |
| 58 | + # ────────────────────────────────────────────── |
| 59 | + build: |
| 60 | + name: "Build (production)" |
| 61 | + runs-on: ubuntu-latest |
| 62 | + needs: test |
| 63 | + # Always run build: even when tests were skipped |
| 64 | + if: always() && (needs.test.result == 'success' || needs.test.result == 'skipped') |
| 65 | + |
| 66 | + steps: |
| 67 | + - name: Checkout code |
| 68 | + uses: actions/checkout@v7 |
| 69 | + |
| 70 | + - name: Set up PHP 8.3 |
| 71 | + uses: shivammathur/setup-php@v2 |
| 72 | + with: |
| 73 | + php-version: "8.3" |
| 74 | + extensions: pdo, mbstring, xml |
| 75 | + coverage: none |
| 76 | + |
| 77 | + - name: Install production Composer dependencies only |
| 78 | + run: | |
| 79 | + composer install \ |
| 80 | + --no-dev \ |
| 81 | + --optimize-autoloader \ |
| 82 | + --classmap-authoritative \ |
| 83 | + --no-progress \ |
| 84 | + --prefer-dist \ |
| 85 | + --no-interaction |
| 86 | +
|
| 87 | + - name: Create .env from example (CI placeholder) |
| 88 | + run: cp .env.example .env |
| 89 | + |
| 90 | + - name: Verify autoloader integrity |
| 91 | + run: php -r "require 'vendor/autoload.php'; echo 'Autoloader OK' . PHP_EOL;" |
| 92 | + |
| 93 | + - name: Package release artifact |
| 94 | + run: | |
| 95 | + mkdir -p dist |
| 96 | + zip -r dist/release-${{ github.sha }}.zip \ |
| 97 | + public/ src/ vendor/ .env.example composer.json \ |
| 98 | + --exclude "*.git*" |
| 99 | + echo "📦 Build artifact: dist/release-${{ github.sha }}.zip" |
| 100 | + ls -lh dist/ |
| 101 | +
|
| 102 | + - name: Upload release artifact |
| 103 | + uses: actions/upload-artifact@v4 |
| 104 | + with: |
| 105 | + name: release-${{ github.sha }} |
| 106 | + path: dist/release-${{ github.sha }}.zip |
| 107 | + retention-days: 7 |
| 108 | + |
| 109 | + # ────────────────────────────────────────────── |
| 110 | + # Job 3 – Deploy |
| 111 | + # ────────────────────────────────────────────── |
| 112 | + deploy: |
| 113 | + name: "Deploy → ${{ inputs.environment }}" |
| 114 | + runs-on: ubuntu-latest |
| 115 | + needs: build |
| 116 | + environment: ${{ inputs.environment }} |
| 117 | + |
| 118 | + steps: |
| 119 | + - name: Checkout code |
| 120 | + uses: actions/checkout@v7 |
| 121 | + |
| 122 | + - name: Download release artifact |
| 123 | + uses: actions/download-artifact@v4 |
| 124 | + with: |
| 125 | + name: release-${{ github.sha }} |
| 126 | + path: dist/ |
| 127 | + |
| 128 | + - name: Copy artifact to server via SCP |
| 129 | + uses: appleboy/scp-action@v1 |
| 130 | + with: |
| 131 | + host: ${{ secrets.SSH_HOST }} |
| 132 | + username: ${{ secrets.SSH_USER }} |
| 133 | + key: ${{ secrets.SSH_PRIVATE_KEY }} |
| 134 | + port: ${{ secrets.SSH_PORT || '22' }} |
| 135 | + source: "dist/release-${{ github.sha }}.zip" |
| 136 | + target: "/tmp/" |
| 137 | + |
| 138 | + - name: Extract and deploy via SSH |
| 139 | + uses: appleboy/ssh-action@v1 |
| 140 | + env: |
| 141 | + DEPLOY_PATH: ${{ secrets.DEPLOY_PATH }} |
| 142 | + SHA: ${{ github.sha }} |
| 143 | + with: |
| 144 | + host: ${{ secrets.SSH_HOST }} |
| 145 | + username: ${{ secrets.SSH_USER }} |
| 146 | + key: ${{ secrets.SSH_PRIVATE_KEY }} |
| 147 | + port: ${{ secrets.SSH_PORT || '22' }} |
| 148 | + envs: DEPLOY_PATH,SHA |
| 149 | + script: | |
| 150 | + # 1. Create releases and shared directories if they don't exist |
| 151 | + mkdir -p $DEPLOY_PATH/releases |
| 152 | + mkdir -p $DEPLOY_PATH/shared |
| 153 | + |
| 154 | + # 2. Extract artifact into a new release folder |
| 155 | + mkdir -p $DEPLOY_PATH/releases/$SHA |
| 156 | + unzip -q /tmp/dist/release-$SHA.zip -d $DEPLOY_PATH/releases/$SHA |
| 157 | + |
| 158 | + # 3. Ensure shared .env exists, then link it |
| 159 | + touch $DEPLOY_PATH/shared/.env |
| 160 | + ln -sf $DEPLOY_PATH/shared/.env $DEPLOY_PATH/releases/$SHA/.env |
| 161 | + |
| 162 | + # 4. Swap the "current" symlink for zero-downtime deployment |
| 163 | + ln -sfn $DEPLOY_PATH/releases/$SHA $DEPLOY_PATH/current |
| 164 | + |
| 165 | + # 5. Clean up the temp zip file |
| 166 | + rm -f /tmp/dist/release-$SHA.zip |
| 167 | + |
| 168 | + # 6. Keep only the last 5 releases (cleanup) |
| 169 | + ls -dt $DEPLOY_PATH/releases/* | tail -n +6 | xargs -d '\n' rm -rf -- || true |
| 170 | + |
| 171 | + echo "✅ Successfully deployed $SHA to $DEPLOY_PATH/current" |
| 172 | +
|
| 173 | + - name: Post-deploy smoke test (stub) |
| 174 | + run: | |
| 175 | + echo "🔍 Running smoke test against ${{ inputs.environment }}..." |
| 176 | + echo "👉 Add curl/http check against your deployed URL here:" |
| 177 | + echo ' curl -sf https://your-domain.com/ | grep "Welcome to Basic PHP RESTful API"' |
| 178 | + echo "✅ Smoke test stub passed." |
0 commit comments