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

Jenkins Pipeline配置:SCM变更时更新任务但禁止自动构建

Alright, let's walk through exactly how to set up your Jenkinsfile to hit all your requirements—SCM polling enabled but no auto-builds, manual parameterized triggers only, and automatic parameter sync without accidental runs. Here's a complete, tested solution:

Jenkinsfile Configuration: SCM Polling + Manual Parameterized Builds Only

Full Jenkinsfile Example

pipeline {
    agent any

    // Define build parameters—new params added here auto-sync to Jenkins
    parameters {
        string(name: 'TARGET_ENV', defaultValue: 'staging', description: 'Environment to deploy to')
        booleanParam(name: 'RUN_INTEGRATION_TESTS', defaultValue: true, description: 'Run integration tests pre-deployment?')
        choice(name: 'DEPLOY_TYPE', choices: ['full', 'incremental'], description: 'Type of deployment to run')
        // Add new parameters directly here—Jenkins will pick them up automatically
    }

    // Enable SCM polling (adjust cron to your desired frequency)
    triggers {
        pollSCM('H/15 * * * *') // Poll every 15 minutes
    }

    options {
        disableConcurrentBuilds() // Optional: Prevent overlapping builds
        buildDiscarder(logRotator(numToKeepStr: '10')) // Optional: Clean up old builds
    }

    stages {
        stage('Checkout Code') {
            // Only run if build was triggered manually (not by SCM poll)
            when {
                allOf {
                    triggeredBy 'UserIdCause' // Covers manual clicks
                    not { triggeredBy 'SCMTrigger' } // Exclude SCM-triggered runs
                }
            }
            steps {
                echo "Starting manual build with params: TARGET_ENV=${params.TARGET_ENV}, DEPLOY_TYPE=${params.DEPLOY_TYPE}"
                checkout scm
            }
        }

        stage('Run Tests') {
            when {
                allOf {
                    triggeredBy 'UserIdCause'
                    not { triggeredBy 'SCMTrigger' }
                    expression { return params.RUN_INTEGRATION_TESTS }
                }
            }
            steps {
                echo "Running integration tests..."
                sh './run-tests.sh' // Replace with your test command
            }
        }

        stage('Deploy') {
            when {
                allOf {
                    triggeredBy 'UserIdCause'
                    not { triggeredBy 'SCMTrigger' }
                }
            }
            steps {
                echo "Deploying ${params.DEPLOY_TYPE} build to ${params.TARGET_ENV}..."
                sh "./deploy.sh --env ${params.TARGET_ENV} --type ${params.DEPLOY_TYPE}" // Replace with your deploy command
            }
        }
    }

    post {
        success {
            when {
                triggeredBy 'UserIdCause'
                not { triggeredBy 'SCMTrigger' }
            }
            echo "Manual build completed successfully!"
        }
        failure {
            when {
                triggeredBy 'UserIdCause'
                not { triggeredBy 'SCMTrigger' }
            }
            echo "Manual build failed—check logs for details!"
        }
    }
}

Key Breakdown of Each Requirement

1. SCM Polling Enabled (No Auto-Builds)

  • The triggers { pollSCM('H/15 * * * *') } line tells Jenkins to check your SCM for changes every 15 minutes (tweak the cron expression to match your needs—e.g., H * * * * for hourly checks).
  • The when conditions in every stage ensure that only manually triggered builds run the pipeline steps. Any build triggered by an SCM poll will skip all stages and exit immediately, acting as a harmless "check-only" run.

2. Manual Parameterized Triggers Only

  • The parameters block defines all your build inputs. When you add new parameters here, Jenkins will automatically update the job's "Build with Parameters" interface the next time it loads the pipeline—no manual job configuration needed.
  • The triggeredBy 'UserIdCause' condition ensures that only builds started by a user (via the Jenkins UI or API) will execute steps. SCM-triggered runs are explicitly excluded with not { triggeredBy 'SCMTrigger' }.

3. Auto-Sync New Parameters (No Auto-Runs)

  • Since we've locked pipeline execution to manual triggers only, adding a new parameter won't trigger an automatic build. Jenkins will simply update the parameter list for the next time you manually start a build.

Pro Tips

  • Cron Expression Cheat Sheet: Adjust the pollSCM cron string to fit your workflow:
    • 0 9 * * 1-5 = Every weekday at 9 AM
    • H/30 * * * * = Every 30 minutes
    • 0 0 * * * = Daily at midnight
  • Avoid Extra Triggers: Double-check your Jenkins job's configuration (outside the Jenkinsfile) to ensure you haven't enabled other auto-triggers like "GitHub hook trigger for GITScm polling"—these would bypass our pipeline conditions.
  • Parameter Types: Use other parameter types (like text, file, or password) as needed—Jenkins will sync all of them automatically when you update the parameters block.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:27:57