You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置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

  • develop branch: Auto-triggers builds whenever code changes, and automatically deploys to the staging environment
  • master branch: 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

  1. Automatic Trigger Lockdown:

    • The disableAutomaticTrigger(branch: 'master') option ensures Jenkins will never start the master branch pipeline on its own—only manual builds are permitted.
    • For develop, the pollSCM trigger 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).
  2. Branch-Specific Execution Gates:

    • when { branch 'develop' } restricts the staging deployment stage to only run on the develop branch.
    • The input step for master forces human approval before production deployment—this adds a critical safety check for production changes.
  3. 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).

内容的提问来源于stack exchange,提问作者JimmyD

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:00:47