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

如何在Jenkins声明式流水线中获取SVN版本号

How to Get SVN Revision Number in Jenkins Declarative Pipeline

Got it, let's tackle this problem head-on since Declarative Pipeline has some key differences from Freestyle or Scripted Pipelines when it comes to accessing the SVN revision number.

First, you’re right that ${SVN_REVISION} works out of the box in Freestyle jobs, and the Scripted Pipeline approach uses the checkout step's returned variables. For Declarative Pipeline, we just need to wrap that script-style logic in a script block (since Declarative enforces a structured syntax but allows escape hatches for scripted code).

Solution 1: Explicit SVN Checkout with Variable Capture

If you’re defining your SVN repo directly in the pipeline, use this approach to checkout and capture the revision:

pipeline {
    agent any
    stages {
        stage('Checkout SVN & Capture Revision') {
            steps {
                script {
                    // Execute SVN checkout and store SCM-related variables
                    def scmVars = checkout([
                        $class: 'SubversionSCM',
                        locations: [[
                            remote: 'https://your-svn-repo-url/trunk',
                            local: '.'
                        ]],
                        // Add any additional SVN config here (credentials, ignore rules, etc.)
                        additionalCredentials: [],
                        ignoreDirPropChanges: false,
                        workspaceUpdater: [$class: 'UpdateUpdater']
                    ])
                    // Save the revision to an environment variable for global use
                    env.SVN_REVISION = scmVars.SVN_REVISION
                }
            }
        }
        stage('Use the SVN Revision') {
            steps {
                echo "Current SVN Revision: ${env.SVN_REVISION}"
                // Use the revision number for tasks like packaging, tagging, or logging
                sh "echo 'Building revision ${env.SVN_REVISION}'"
            }
        }
    }
}

Solution 2: Using checkout scm (Works for Configured SCMs, Not Just Multibranch)

You mentioned thinking checkout scm only works for Multibranch Pipelines, but that’s not true—this works whenever your pipeline has an SCM configured (either via the job's UI or in the pipeline definition). You can still capture the revision like this:

pipeline {
    agent any
    options {
        skipDefaultCheckout(true) // Skip auto-checkout to do it manually and capture variables
    }
    stages {
        stage('Checkout & Get Revision') {
            steps {
                script {
                    def scmVars = checkout scm
                    env.SVN_REVISION = scmVars.SVN_REVISION
                }
            }
        }
        stage('Validate Revision') {
            steps {
                echo "Checked out SVN revision: ${env.SVN_REVISION}"
            }
        }
    }
}

Key Notes:

  • The script block lets you use Scripted Pipeline syntax within Declarative, which is necessary for capturing the scmVars object returned by checkout.
  • By assigning scmVars.SVN_REVISION to env.SVN_REVISION, you make the revision number available across all stages in your pipeline.
  • Unlike Freestyle jobs, Declarative Pipeline doesn’t auto-inject the SVN_REVISION environment variable, so this explicit capture is required.

内容的提问来源于stack exchange,提问作者Marcel M.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:54:18