Search code examples
jenkinsjenkins-pipelinejenkins-pluginsjenkins-groovy

Reference part of the parameter in Jenkins plugin "publish over cifs"


ALL

Below is my jenkinsfile. I defined a parameter "SVN_TAG" for listing SVN tags. The format of SVN tag is "VERSION-Digit.Digit.Digit". Now I can only only reference the whole parameter in the cifsPublisher "RemoteDirectory" settings. But I want only reference the digit part of the parameter(like "2.2.2"), how should I do this? thanks.


// Jenkins Declarative Pipeline
def PRODUCT_VERSION
pipeline {
    
    agent { label 'Windows Server 16 Node' }
    
    options {
        buildDiscarder(logRotator(numToKeepStr: '10', artifactNumToKeepStr: '10'))
    }
    environment {
      
        TAG = '${SVN_TAG.substring(SVN_TAG.indexOf(\'-\')+1)}'
    }
    stages {
        stage('Initialize') {
            steps {
                 script {
                    PRODUCT_VERSION = "3.2.0.1"
                 }
            }
         } 
         stage('Setup parameters') {
            steps {
                script { 
                    properties([
                        parameters([
                             [ $class: 'ListSubversionTagsParameterDefinition', 
                              credentialsId: 'xxxxxxxxxxx',
                              defaultValue: 'trunk', maxTags: '',
                              name: 'SVN_TAG', 
                              reverseByDate: false,
                              reverseByName: true,
                              tagsDir: 'https://svn-pro.xxxx.net:xxxxxx',
                              tagsFilter: ''
                            ],                        
                        ])
                    ])
                }
            }
        }
        stage('Build') {
            steps {           
                 cleanWs()
                 checkoutSource()
                 buildSource()
                 buildInstaller()           
            }
        }
        stage('Deploy') {
            steps {     
                copyArtifacts()
            }  
        }
    }
   
}



def copyArtifacts() {    
             
    cifsPublisher(publishers: [[configName: 'Server', transfers: [[cleanRemote: false, excludes: '', flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'builds\\$JOB_BASE_NAME\\${SVN_TAG}', remoteDirectorySDF: false, removePrefix: '\\unsigned', sourceFiles: '\\unsigned\\*.exe']], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: true]])
    
}

Solution

  • Your environment variable idea is the correct way, just use double quotes ("") instead of single ones ('') to allow string interpolation, is it only works on double quotes in groovy. You can read more in the Groovy String Documentation.

    So just use something like TAG = "${SVN_TAG.split('-')[1]}".
    Then use that tag wherever you need it, you can pass it to relevant functions like copyArtifact or just use it as is: "builds\\$JOB_BASE_NAME\\${TAG}".