Jenkins任务启动后与jira之间有通信、状态变更
properties([
parameters([
string(defaultValue: '', description: '触发类型', name: 'Type', trim: true),
string(defaultValue: '', description: '模块类型', name: 'ModuleType', trim: true),
string(defaultValue: '', description: 'git commit id , GIT推送的唯一标识', name: 'GitCommitId', trim: true),
string(defaultValue: '', description: '任务对应ISSUE ID', name: 'IssueId', trim: true),
string(defaultValue: '', description: 'git tag, GIT 推送的标签', name: 'GitTag', trim: true),
string(defaultValue: '', description: 'GIT 模块,git项目地址', name: 'GitModule', trim: true)
])
])
// 推进状态
def jiraTransition(String action) {
jiraTransition(action, null);
}
// 带错误处理
def jiraTransition(String action, Exception e){
echo action;
// 切换项目状态
String actionId = getTransitionCode(action);
// 如果有actionId为null 则终止运行
if( actionId == null ){ return; }
def transition = [
'transition': [
'id': actionId
]
]
echo "transition: ${transition};"
jiraTransitionIssue idOrKey: "${params.IssueId}", input: transition, site: 'GALAXY-JIRA'
if(e != null){
failure(e)
}
}
String getTransitionCode(action){
String actionId = null;
switch(action) {
// case "compileInit":
// actionId = "291";
// break;
// 取得代码阶段
case "startFetchCode":
actionId = "301";
break;
case "fetchCodeSuccess":
actionId = "91";
break;
case "fetchCodeFailure":
actionId = "311";
break;
// 编译阶段
// case "startCompile":
// actionId = "";
// break;
case "compileSuccess":
actionId = "21";
break;
case "compileFailure":
actionId = "81";
break;
// 归档阶段
// case "startArchive":
// actionId = "";
// break;
case "archiveSuccess":
actionId = "121";
break;
case "archiveFailure":
actionId = "131";
break;
}
return actionId;
}
// 添加日志
def addComment(info){
def comment = [ 'body': info ]
jiraAddComment idOrKey: "${params.IssueId}", input: comment, site: 'GALAXY-JIRA', auditLog: false
}
// 错误处理
def failure(e){
addComment("编译失败${e.message}")
currentBuild.result = "FAILURE";
}
node('master') {
ws("/Users/wenhe/.jenkins/workspace/module/${params.GitModule}"){
try{
stage('初始化空间'){
echo "Hello World, ${params.Type},${params.IssueId},GitCommitId:${params.GitCommitId},GitTag:${params.GitTag},GitModule:${params.GitModule},"
// 项目开始编译
jiraTransition("compileInit")
sh script: "sleep 30";
addComment("查看日志接口:http://127.0.0.1:8090/jenkins/job/development/job/build-task/${env.BUILD_ID}/console" )
}
stage('取得代码') { // for display purposes
jiraTransition("startFetchCode")
try{
// git credentialsId: 'get-code-private-key', url: "git@git.localhost.com:${params.GitModule}.git"
sh script: "sleep 30";
jiraTransition("fetchCodeSuccess")
stage('编译阶段') { // for display purposes
sh script: "sleep 30";
jiraTransition("startCompile")
try{
if(params.ModuleType == "VUE"){
echo "编译阶段";
}
sh script: "sleep 30";
// 项目编译成功
jiraTransition("compileSuccess")
stage('归档阶段'){
sh script: "sleep 30";
jiraTransition("startArchive")
try{
if(params.ModuleType == "VUE"){
echo "归档阶段";
}
sh script: "exit 0";
// 项目归档成功
jiraTransition("archiveSuccess")
}catch(Exception e){
// 项目归档失败
jiraTransition("archiveFailure", e)
}
}
}catch(Exception e){
// 项目编译失败
jiraTransition("compileFailure", e)
}
}
}catch(Exception e){
// 项目取得代码失败
jiraTransition("fetchCodeFailure", e)
}
}
} catch(Exception e){
failure(e)
} catch(Error e){
failure(e)
}
}
}