如何配置Jenkinsfile多分支流水线:分环境自动/手动部署
Multi-Branch Jenkinsfile with Branch-Specific Triggers & Deployment Rules
Got it, let's put together a Jenkinsfile that fits your exact needs. I'll break down the logic first, then share the full code with clear explanations for each key part:
Core Behavior Overview
developbranch: Auto-triggers builds whenever code changes, and automatically deploys to the staging environmentmasterbranch: No automatic pipeline starts (must be triggered manually), plus a mandatory manual approval step before deploying to production- Optional: You can add logic for other branches (like feature branches) if needed—right now they'll just run the checkout and build stages
Complete Jenkinsfile
pipeline { agent any options { // Prevent concurrent builds to avoid deployment conflicts disableConcurrentBuilds() // Critical: Disable automatic triggers for master branch (only manual starts allowed) disableAutomaticTrigger(branch: 'master') } triggers { // Auto-trigger develop branch on SCM changes (adjust cron schedule as needed; Webhooks are more efficient if your SCM supports them) pollSCM('H/15 * * * *', branches: [[name: 'develop']]) } stages { stage('Checkout Latest Code') { steps { checkout scm } } stage('Build Application') { steps { echo 'Running build process...' // Replace with your actual build commands (e.g., mvn clean install, npm run build) sh './scripts/build-app.sh' } } stage('Deploy to Staging') { when { branch 'develop' } steps { echo 'Auto-deploying to staging environment...' // Replace with your staging deployment script/commands sh './scripts/deploy-to-staging.sh' } } stage('Production Deployment Approval') { when { branch 'master' } steps { // Require manual sign-off from authorized users before production deploy input message: 'Approve deployment to PRODUCTION?', submitter: 'dev-leads,ops-team' } } stage('Deploy to Production') { when { branch 'master' } steps { echo 'Deploying to production environment (manual trigger only)...' // Replace with your production deployment script/commands sh './scripts/deploy-to-production.sh' } } } post { success { echo 'Pipeline completed successfully!' // Optional: Add Slack/email alerts for successful runs } failure { echo 'Pipeline failed—check logs for troubleshooting details.' // Optional: Add Slack/email alerts for failed runs } } }
Key Details Explained
Automatic Trigger Lockdown:
- The
disableAutomaticTrigger(branch: 'master')option ensures Jenkins will never start themasterbranch pipeline on its own—only manual builds are permitted. - For
develop, thepollSCMtrigger checks for code changes every 15 minutes (tweak the cron schedule to match your team's workflow; using SCM Webhooks is a faster alternative if available).
- The
Branch-Specific Execution Gates:
when { branch 'develop' }restricts the staging deployment stage to only run on the develop branch.- The
inputstep formasterforces human approval before production deployment—this adds a critical safety check for production changes.
Customizable Build/Deploy Steps:
- Replace the placeholder scripts (
build-app.sh,deploy-to-staging.sh, etc.) with your actual build and deployment commands (e.g., Maven, npm, Ansible, Docker pushes).
- Replace the placeholder scripts (
内容的提问来源于stack exchange,提问作者JimmyD
相关产品推荐
相关产品推荐

