# CI + Deployment (Gitea Actions; `gitea.*` is Gitea's name for the GitHub-compatible `github.*` context). # # - test: runs on every push to any branch. Its commit status is what the branch # protection of main requires, so a PR can only be merged once the tests are green. # - deploy: runs only on push to main (i.e. after a merge), only if the tests are green and # ONLY if the repository variable DEPLOY_ENABLED is "true" (deactivated by default). # # Target: DirectAdmin hosting, app checked out directly into the domain's public_html # (the root .htaccess/index.php forward every request to webroot/). # The server pulls the code itself over HTTPS with the job's short-lived token, so no # deploy key and no stored credentials are needed on the server. # # Repository variables (Settings → Actions → Variables): # DEPLOY_ENABLED "true" to enable deployment (anything else = disabled) # SSH_HOST e.g. plenario.gnilebein.de # SSH_PORT optional, default 22 # SSH_USER e.g. gnilebein.de # DEPLOY_PATH e.g. /home/gnilebein.de/domains/plenario.gnilebein.de/public_html # PHP_BIN optional, PHP CLI binary (default: auto-detect, must be >= 8.4) # COMPOSER_BIN optional, composer command (default: auto-detect, else composer.phar # is installed once to ~/.local/bin) # Repository secrets: # SSH_KEY private deploy key (preferred), or # SSH_PASSWORD password (fallback) # # One-time setup on the server: create config/.env (DEBUG=false, APP_FULL_BASE_URL, # SECURITY_SALT, mail settings – see config/.env.example). The deploy aborts without it. name: CI / Deploy on: push: branches: ['**'] jobs: # Matrix over the supported PHP versions. phpunit: name: PHP ${{ matrix.php }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: php: ['8.4', '8.5'] steps: - uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} extensions: mbstring, intl, pdo_sqlite, sqlite3 coverage: none - name: Install dependencies run: composer install --no-interaction --prefer-dist --no-progress - name: Run tests run: vendor/bin/phpunit - name: Coding standard run: vendor/bin/phpcs --warning-severity=0 # Stable check name required by the branch protection of main: green only if all PHP versions are green. test: needs: phpunit if: always() runs-on: ubuntu-latest steps: - name: All PHP versions passed run: | if [ "${{ needs.phpunit.result }}" != "success" ]; then echo "Matrix result: ${{ needs.phpunit.result }}" >&2 exit 1 fi deploy: needs: test # Deactivated until the repository variable DEPLOY_ENABLED is "true". if: vars.DEPLOY_ENABLED == 'true' && gitea.event_name == 'push' && gitea.ref == 'refs/heads/main' runs-on: ubuntu-latest concurrency: group: deploy-production cancel-in-progress: false steps: - name: Deploy via SSH uses: appleboy/ssh-action@v1.2.5 with: host: ${{ vars.SSH_HOST }} port: ${{ vars.SSH_PORT || 22 }} username: ${{ vars.SSH_USER }} key: ${{ secrets.SSH_KEY }} password: ${{ secrets.SSH_PASSWORD }} script_stop: true script: | set -eu DEPLOY_PATH="${{ vars.DEPLOY_PATH }}" PHP="${{ vars.PHP_BIN }}" COMPOSER="${{ vars.COMPOSER_BIN }}" REPO_URL="${{ gitea.server_url }}/${{ gitea.repository }}.git" # Same URL with the job token as credentials; only passed to fetch, never stored in .git/config. FETCH_URL="${REPO_URL%%://*}://oauth2:${{ gitea.token }}@${REPO_URL#*://}" if [ -z "$DEPLOY_PATH" ] || [ ! -d "$DEPLOY_PATH" ]; then echo "DEPLOY_PATH is not set or does not exist: '$DEPLOY_PATH'" >&2 exit 1 fi cd "$DEPLOY_PATH" # --- PHP --------------------------------------------------------------- if [ -z "$PHP" ]; then for candidate in /usr/local/php85/bin/php /usr/local/php84/bin/php /usr/local/bin/php php; do if command -v "$candidate" >/dev/null 2>&1 \ && "$candidate" -r 'exit(PHP_VERSION_ID >= 80400 ? 0 : 1);'; then PHP="$candidate" break fi done fi if [ -z "$PHP" ]; then echo "No PHP >= 8.4 found – set the repository variable PHP_BIN." >&2 exit 1 fi echo "Using PHP: $PHP ($("$PHP" -r 'echo PHP_VERSION;'))" # --- Composer (installed once, verified against the official checksum) -- if [ -z "$COMPOSER" ]; then if command -v composer >/dev/null 2>&1; then COMPOSER="$PHP $(command -v composer)" else PHAR="$HOME/.local/bin/composer.phar" if [ ! -f "$PHAR" ]; then mkdir -p "$HOME/.local/bin" EXPECTED="$(curl -fsSL https://composer.github.io/installer.sig)" curl -fsSL https://getcomposer.org/installer -o /tmp/composer-setup.php ACTUAL="$("$PHP" -r "echo hash_file('sha384', '/tmp/composer-setup.php');")" if [ "$EXPECTED" != "$ACTUAL" ]; then rm -f /tmp/composer-setup.php echo "Composer installer checksum mismatch" >&2 exit 1 fi "$PHP" /tmp/composer-setup.php --quiet --install-dir="$HOME/.local/bin" --filename=composer.phar rm -f /tmp/composer-setup.php fi COMPOSER="$PHP $PHAR" fi fi # --- Code: first run initialises the checkout, later runs update it ----- if [ ! -d .git ]; then git init -q git remote add origin "$REPO_URL" fi git remote set-url origin "$REPO_URL" git fetch --quiet "$FETCH_URL" '+refs/heads/main:refs/remotes/origin/main' git reset --hard origin/main echo "Deployed commit: $(git rev-parse --short HEAD)" if [ ! -f config/.env ]; then echo "config/.env is missing – create it once on the server (see config/.env.example)." >&2 exit 1 fi $COMPOSER install --no-dev --optimize-autoloader --no-interaction --no-progress "$PHP" bin/cake.php migrations migrate "$PHP" bin/cake.php cache clear_all