Jenkins build task

 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)
    }
  }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

从未、淡定

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值