parameters {
    string(name: 'Version', description: 'new version number')			
}

node('words-linux') {         
    try {	
        stage('0:Merge master to release'){
            checkout([$class: 'GitSCM', branches: [[name: '*/release']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'LocalBranch', localBranch: "**"]], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '361885ba-9425-4230-950e-0af201d90547', url: 'https://git.auckland.dynabic.com/words-cloud/words-cloud-php.git']]])
            sh "git config user.email 'jenkins.aspose@gmail.com'"
            sh "git config user.name 'jenkins'"
            sh "git checkout --merge release"
            sh "git reset --hard origin/release"
            sh "git merge --no-ff --allow-unrelated-histories origin/master"
            sh "git diff --name-status"			
            sh 'git commit -am "Merged master branch to release" || exit 0'
            withCredentials([usernamePassword(credentialsId: '361885ba-9425-4230-950e-0af201d90547', passwordVariable: 'gitPass', usernameVariable: 'gitUsername')]) {
                sh "git push https://$gitUsername:$gitPass@git.auckland.dynabic.com/words-cloud/words-cloud-php.git release"
            }
        }

        stage('1:add version tag'){
			checkout([$class: 'GitSCM', branches: [[name: '*/release']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'LocalBranch', localBranch: "**"]], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '361885ba-9425-4230-950e-0af201d90547', url: 'https://git.auckland.dynabic.com/words-cloud/words-cloud-php.git']]])
						
				sh "git config user.email \"jenkins.aspose@gmail.com\""
				sh "git config user.name \"jenkins\""
				sh "git tag -a ${version} -m 'version ${version}' | exit 0"
				
				withCredentials([usernamePassword(credentialsId: '361885ba-9425-4230-950e-0af201d90547', passwordVariable: 'gitPass', usernameVariable: 'gitUsername')]) {
					sh "git push https://$gitUsername:$gitPass@git.auckland.dynabic.com/words-cloud/words-cloud-php.git ${version}"
				}
        }           
    } finally {                       
        deleteDir()
    }
}