steps:
  - name: Deploy
    image: alpine
    environment:
      SSH_PRIVATE_KEY:
        from_secret: DEPLOY_SSH_KEY
      DEPLOY_PATH:
        from_secret: DEPLOY_PATH
      DEPLOY_HOST:
        from_secret: DEPLOY_HOST
      DEPLOY_USER:
        from_secret: DEPLOY_USER
      DEPLOY_BRANCH:
        from_secret: DEPLOY_BRANCH
    when:
      event:
        - push
        - manual
    commands:
      - apk add --no-cache openssh
      - mkdir -p ~/.ssh

      # 🛡️ Set up SSH key and known_hosts
      - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_ed25519
      - chmod 600 ~/.ssh/id_ed25519
      - ssh-keyscan "$DEPLOY_HOST" >> ~/.ssh/known_hosts

      # 🔍 Verify SSH access
      - |
        echo "🔍 Testing SSH connection..."
        if ! ssh -o BatchMode=yes "$DEPLOY_USER@$DEPLOY_HOST" 'echo "✅ SSH OK"'; then
          echo "❌ SSH connection failed"
          exit 1
        fi
# ---------------------------------------------------------------------------------
#         Start Customization
# ---------------------------------------------------------------------------------

      # 🚀 Pull latest code on target host
      - >
        ssh "$DEPLOY_USER@$DEPLOY_HOST" "cd '$DEPLOY_PATH' &&
          git fetch origin &&
          git reset --hard origin/$DEPLOY_BRANCH &&
          git clean -fd"

# ---------------------------------------------------------------------------------
#         Stop Customization
# ---------------------------------------------------------------------------------
  - name: Notify_apprise
    image: curlimages/curl:latest
    environment:
      APPRISE_URL:
        from_secret: APPRISE_URL
    when:
      status: [ success, failure ]
      event: [ push, manual ]
    commands:
      - |
        # ✅/❌ Determine pipeline status
        if [ "$CI_PREV_PIPELINE_STATUS" = "success" ]; then
          STATUS="✅ success"
        else
          STATUS="❌ failure"
        fi

        # 🌐 Generate fallback-safe commit URL
        COMMIT="${CI_COMMIT:-$CI_COMMIT_SHA}"
        REPO_URL="https://git.inxs.me/$CI_REPO_OWNER/$CI_REPO_NAME"
        COMMIT_URL="$REPO_URL/commit/$COMMIT"
        TIME="$(date -u '+%Y-%m-%d %H:%M UTC')"
        TITLE="Woodpecker CI • $CI_REPO_NAME"

        # 📝 Assemble body with click-through link
        BODY="$STATUS
        📦 Repo: \`$CI_REPO_NAME\`
        🌿 Branch: \`$CI_COMMIT_BRANCH\`
        🔢 Commit: [$COMMIT]($COMMIT_URL)
        👤 Author: $CI_COMMIT_AUTHOR
        🕒 Time: $TIME
        📝 Message: $CI_COMMIT_MESSAGE"

        echo "🔍 Commit URL = $COMMIT_URL"

        # 📬 Notify Apprise (Telegram, etc.)
        curl -s -X POST "$APPRISE_URL" \
          -F "title=$TITLE" \
          -F "body=$BODY"