</>

Technology

Jenkins

Difficulty

Beginner

Interview Question

What are Jenkins build triggers? How do you automatically trigger a Jenkins build?

Answer

Jenkins Build Triggers

Build triggers determine when a Jenkins job runs automatically.

1. SCM Polling (Poll Source Control Management)

Checks Git for changes on a schedule. If new commits found → triggers build.

CODE
H/5 * * * *   → check every 5 minutes
H * * * *     → check every hour

Pros: Simple, no webhook setup needed Cons: Delay (up to 5 min), unnecessary polling

GROOVY
// Jenkinsfile
pipeline {
    triggers {
        pollSCM('H/5 * * * *')  // poll every 5 minutes
    }
    ...
}

2. GitHub/GitLab Webhook (Recommended — Instant)

GitHub calls Jenkins the moment code is pushed.

Setup:

  1. Jenkins: Install "GitHub plugin", generate token
  2. GitHub: Settings → Webhooks → Add webhook → http://jenkins.url/github-webhook/
  3. Jenkins job: Check "GitHub hook trigger for GITScm polling"
GROOVY
// Jenkinsfile
pipeline {
    triggers {
        githubPush()  // triggers on any push to the branch
    }
    ...
}

Pros: Instant build on every push, no polling overhead Cons: Jenkins must be publicly accessible (or use ngrok for local dev)

3. Scheduled Build (Cron Syntax)

Run builds at specific times — nightly regression, weekly reports.

CODE
// Cron syntax: minute hour day-of-month month day-of-week
0 2 * * *     → every day at 2:00 AM
0 22 * * 1-5  → weekdays at 10 PM
H 0 * * 7     → every Sunday at midnight (H = hash to spread load)
GROOVY
pipeline {
    triggers {
        cron('0 2 * * *')  // nightly at 2 AM
    }
    ...
}

4. Upstream Job Trigger

Trigger this job when another job completes.

CODE
Job: Build → triggers → Job: Test → triggers → Job: Deploy
GROOVY
pipeline {
    triggers {
        upstream(upstreamProjects: 'my-build-job', threshold: hudson.model.Result.SUCCESS)
    }
    ...
}

5. Build with Parameters (Manual + API)

Bash
# Trigger via Jenkins API
curl -X POST "http://jenkins/job/my-job/build" \
  --user "admin:token" \
  --data-urlencode json='{"parameter":[{"name":"ENVIRONMENT","value":"staging"}]}'

6. Pull Request Trigger (Jenkins + GitHub PR Builder)

Trigger builds on PR creation/update — run tests before merge.

GROOVY
pipeline {
    triggers {
        pullRequestTrigger(
            events: [openPullRequest(), updatePullRequest()]
        )
    }
}

Summary Table

TriggerSpeedUse Case
WebhookInstantEvery push, PR builds
SCM Poll~5 min delayWhen webhook isn't possible
CronScheduledNightly regression tests
UpstreamPost-buildDeployment chains
ManualImmediateOn-demand builds

Follow AutomateQA

Related Topics