129 lines
4.3 KiB
YAML
129 lines
4.3 KiB
YAML
name: Deploy to WordPress
|
|
|
|
on:
|
|
push:
|
|
branches: [ main ]
|
|
|
|
permissions:
|
|
contents: write
|
|
|
|
# Cancel previous deploys if a new push arrives while one is running.
|
|
concurrency:
|
|
group: deploy-production
|
|
cancel-in-progress: true
|
|
|
|
jobs:
|
|
# -- Run tests first --
|
|
test:
|
|
name: Pre-deploy tests
|
|
uses: ./.github/workflows/ci.yml
|
|
|
|
# -- Push clean plugin files to the deploy branch --
|
|
sync-deploy-branch:
|
|
name: Sync deploy branch
|
|
runs-on: ubuntu-latest
|
|
needs: test
|
|
environment: production
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
with:
|
|
fetch-depth: 0
|
|
|
|
- name: Sync deploy branch
|
|
run: |
|
|
git config user.name "github-actions[bot]"
|
|
git config user.email "github-actions[bot]@users.noreply.github.com"
|
|
|
|
# Fetch the remote deploy branch (may not exist on first run).
|
|
git fetch origin deploy:deploy 2>/dev/null || true
|
|
|
|
# If deploy branch exists, check it out; otherwise create it.
|
|
if git rev-parse --verify deploy >/dev/null 2>&1; then
|
|
git checkout deploy
|
|
else
|
|
git checkout --orphan deploy
|
|
git rm -rf --cached . > /dev/null 2>&1 || true
|
|
fi
|
|
|
|
# Remove all tracked files so we start clean each time.
|
|
git rm -rf . > /dev/null 2>&1 || true
|
|
|
|
# Checkout only the plugin runtime files from main.
|
|
git checkout main -- \
|
|
admin/ \
|
|
includes/ \
|
|
languages/ \
|
|
public/ \
|
|
peptide-news-plugin.php
|
|
|
|
# Commit only if there are actual changes.
|
|
git add -A
|
|
if git diff-index --quiet HEAD -- 2>/dev/null; then
|
|
echo "No changes to deploy."
|
|
else
|
|
git commit -m "Deploy ${GITHUB_SHA::7} from main"
|
|
fi
|
|
|
|
# Normal push (fast-forward).
|
|
git push origin deploy
|
|
|
|
# -- Deploy to Hostinger via SSH --
|
|
# SSH into Hostinger and git pull the deploy branch directly into
|
|
# the correct plugin directory (peptide-news-plugin).
|
|
deploy:
|
|
name: Deploy to Hostinger
|
|
runs-on: ubuntu-latest
|
|
needs: sync-deploy-branch
|
|
environment: production
|
|
steps:
|
|
- name: Pull deploy branch on Hostinger
|
|
uses: appleboy/ssh-action@v1
|
|
with:
|
|
host: ${{ secrets.SSH_HOST }}
|
|
username: ${{ secrets.SSH_USERNAME }}
|
|
key: ${{ secrets.SSH_PRIVATE_KEY }}
|
|
port: ${{ secrets.SSH_PORT }}
|
|
script: |
|
|
# Ensure GitHub host key is trusted (one-time setup).
|
|
mkdir -p ~/.ssh
|
|
ssh-keyscan github.com >> ~/.ssh/known_hosts 2>/dev/null
|
|
|
|
PLUGIN_DIR="$HOME/domains/peptiderepo.com/public_html/wp-content/plugins/peptide-news-plugin"
|
|
REPO_URL="git@github.com:peptiderepo/peptide-news-plugin.git"
|
|
|
|
if [ -d "$PLUGIN_DIR/.git" ]; then
|
|
# Existing clone -- just pull latest.
|
|
cd "$PLUGIN_DIR"
|
|
git fetch origin deploy
|
|
git reset --hard origin/deploy
|
|
else
|
|
# First deploy -- clone the deploy branch.
|
|
rm -rf "$PLUGIN_DIR"
|
|
git clone -b deploy "$REPO_URL" "$PLUGIN_DIR"
|
|
fi
|
|
|
|
echo "Deployed to $PLUGIN_DIR"
|
|
ls -la "$PLUGIN_DIR"
|
|
|
|
- name: Purge LiteSpeed Cache
|
|
if: success()
|
|
run: |
|
|
# Trigger a cache purge via the WordPress REST API.
|
|
if [ -n "${{ secrets.WP_APP_PASSWORD }}" ]; then
|
|
curl -s -X POST \
|
|
-u "${{ secrets.WP_ADMIN_USER }}:${{ secrets.WP_APP_PASSWORD }}" \
|
|
"${{ secrets.WP_SITE_URL }}/wp-json/peptide-news/v1/cache-purge" \
|
|
--max-time 15 || echo "Cache purge skipped (endpoint not available)"
|
|
else
|
|
echo "No WP_APP_PASSWORD set -- skipping remote cache purge."
|
|
fi
|
|
|
|
- name: Deploy summary
|
|
run: |
|
|
echo "## Deployment Complete" >> $GITHUB_STEP_SUMMARY
|
|
echo "" >> $GITHUB_STEP_SUMMARY
|
|
echo "- **Branch:** \`${{ github.ref_name }}\`" >> $GITHUB_STEP_SUMMARY
|
|
echo "- **Commit:** \`${GITHUB_SHA::7}\`" >> $GITHUB_STEP_SUMMARY
|
|
echo "- **Method:** SSH direct deploy (git pull on Hostinger)" >> $GITHUB_STEP_SUMMARY
|
|
echo "- **Deployed to:** ${{ secrets.WP_SITE_URL || 'peptiderepo.com' }}" >> $GITHUB_STEP_SUMMARY
|