-
Notifications
You must be signed in to change notification settings - Fork 14
/
config-define.gradle
127 lines (119 loc) · 2.9 KB
/
config-define.gradle
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* ************************************************************
* 文件:config-define.gradle 模块:component 项目:component
* 当前修改时间:2019年04月23日 18:23:20
* 上次修改时间:2019年04月13日 08:44:03
* 作者:Cody.yi https://github.com/codyer
*
* 描述:component
* Copyright (c) 2019
* ************************************************************
*/
def getGitBranch() {
//判断是否处于Jenkins编译环境
boolean flag = isInJenkins()
if (flag) {
ext.env = System.getenv()
ext.gitBranch = env.GIT_BRANCH
String[] stringArray = ext.gitBranch.split("/")
if (stringArray.length > 0) {
return stringArray[stringArray.length - 1]
} else {
return "UnKnown Branch"
}
} else {
return 'git symbolic-ref --short -q HEAD'.execute().text.trim()
}
}
/**
* 获取Git 版本号
*/
def getGitSHA() {
return 'git rev-parse --short HEAD'.execute().text.trim()
}
/**
* 获取Git Tag
*/
def getGitTag() {
return 'git describe --tags'.execute([], project.rootDir).text.trim()
}
/**
* 获取Git 提交次数
*/
def getGitCommitCount() {
return 100 + Interger.parse('git rev-list --count HEAD'.execute([], project.rootDir).text.trim())
}
/**
* 判断是否有jenkins
*/
static boolean isInJenkins() {
Map<String, String> map = System.getenv()
if (map == null) {
return false
}
String str = map.get("Path")
if (str != null) {
//it's windows
return false
} else {
str = ""
Iterator it = map.iterator()
while (it.hasNext()) {
str += it.next()
}
return str.contains("jenkins")
}
}
/**
* 获取jenkins任务名
*/
def getJenkinsName() {
boolean flag = isInJenkins()
if (flag) {
ext.env = System.getenv()
ext.name = env.JOB_URL
String[] stringArray = ext.name.split("/")
if (stringArray.length > 0) {
return stringArray[stringArray.length - 1]
} else {
return "Local"
}
} else {
return "Local"
}
}
/**
* 获取Jenkins Build 号
* @return
*/
def getJenkinsBuildCode() {
boolean flag = isInJenkins()
if (flag) {
ext.env = System.getenv()
ext.buildNumber = env.BUILD_NUMBER?.toInteger()
return "$buildNumber"
} else {
return 0
}
}
/**
* tag or branch contains compiler
* @return
*/
boolean isInScope(String scope) {
return getGitTag().contains(scope) || getGitBranch().contains(scope)
}
/**
* 定义几个变量,在build.gradle里面引用
*/
ext {
gitTag = getGitTag()
gitBranch = getGitBranch()
gitSHA = getGitSHA()
jenkinsRevision = getJenkinsBuildCode()
jenkinsName = getJenkinsName()
isJitPack = isInScope("remote")
isApp = isInScope("app")
isComponent = isInScope("component")
isLocalMaven = isInScope("local")
}