-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGITCheckout.groovy
76 lines (68 loc) · 2.06 KB
/
GITCheckout.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!groovy
/**
* Jenkins file for ROCC Pipeline
* Author: nsharma43@sapient.com,ssi257
*/
/**
* Import the package for using the class functions
*/
package main.com.rocc.stages.impl
/**
* Function: Checkout GIT repository code at workspace
* @Params: workspace and branch name mandatory
*/
def gitCheckout( def wspace) {
try {
echo "Running job at workspace " + wspace
def str = wspace[-1..-1]
if (str.isNumber()) {
ws = wspace[0..-3]
file_path = ws+"@libs/DevOps/resources/config/account.properties"
echo "$file_path"
branch = getBranchName( "${ws}")
} else {
file_path = wspace+"@libs/DevOps/resources/config/account.properties"
echo "$file_path"
branch = getBranchName( "${wspace}")
}
Properties props = new Properties()
File propsFile = new File("$file_path")
props.load(propsFile.newDataInputStream())
def git_url = props.getProperty('url')
def git_pass = props.getProperty('password')
echo 'Starting code checkout at workspace ..'
checkout(
[$class: 'GitSCM',
branches: [[name: branch]],
doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CloneOption', depth: 0, noTags: true, reference: '', shallow: true, timeout: 20]],
submoduleCfg: [],
userRemoteConfigs: [
[credentialsId: git_pass,
url: git_url]
]
]
)} catch (Exception groovyEx) {
println groovyEx.getMessage()
println groovyEx.getCause()
throw groovyEx
}
}
/**
* Function: get GIT repository URL from workspace
* @Params: workspace and branch name mandatory
*/
def getRepo( def workspace) {
try {
def gitRepoURL = sh(returnStdout: true, script: "cd ${workspace}; git config --get remote.origin.url").trim()
return gitRepoURL
}
catch (Exception error) {
echo "Failed to get the Git repository URL..."
throw error
}
}
def getBranchName( def wrkspace) {
def gitBranch = sh(returnStdout: true, script: "cd ${wrkspace}@script; git describe --contains --all HEAD | sed -e 's/^remotes\\/origin\\///'").trim()
println gitBranch
return gitBranch
}