def deploy() {
    node('ubuntu') {
        try {
            checkout scm
            
            // Only deploy from main branch
            def branch = env.BRANCH_NAME ?: sh(script: 'git rev-parse --abbrev-ref HEAD', returnStdout: true).trim()
            if (branch != 'main') {
                echo "Skipping deployment: not on main branch (current branch: ${branch})"
                return
            }

            nodejs(nodeJSInstallationName: 'Node23') {
                stage('Install') {
                    sh 'yarn install --frozen-lockfile --production=false'
                }

                stage('Deploy') {
                    // Deploy to Cloudflare Workers using wrangler
                    withCredentials([string(credentialsId: 'th-cloudflare-api-token', variable: 'CLOUDFLARE_API_TOKEN')]) {
                        sh 'yarn deploy'
                    }
                }
            }
        } finally {
            cleanWs()
        }
    }
}

deploy()

