본문 바로가기

DevOps/JIRA & API Gateway

JIRA & API GW를 통해 Jenkins 배포 승인체계 구성하기 - 5. Jenkins 세팅

마지막입니다.

 

Job의 Script는 아래와 같습니다.

아래에서 맨 위에 있는 값들만 세팅하셔서 사용하시면 됩니다. (issue_id는 빈값으로 냅두시면 됩니다.)

 

def API_KEY = ""
def AGW_URL = ""
def JIRA_URL = ""
def issue_id = ""

pipeline {
    environment {        
        PATH = "$PATH:/usr/local/bin/"
      }
    agent any   
    
    stages {
        stage('Approval') {
            steps {
                script {
                    try {
                        cmd = "curl -X POST -H \"X-API-KEY: ${API_KEY}\" -H \"BuildInfo: ${env.JOB_NAME}/${env.BUILD_NUMBER}\" -H \"Content-type: application/json\" ${AGW_URL}/create-issue"
                        issue_id = executeCmdReturn(cmd)
                        print(issue_id)
                        timeout(time:30, unit:'MINUTES') {
                            def APPROVE = input message: 'Deploy to Production', ok: 'Proceed', submitterParameter: 'approver',
                                parameters: [choice(name: 'APPROVE', choices: 'YES\nNO', description: 'Approve Deploy?')]
                            if (APPROVE['APPROVE'] == 'YES'){
                                if (APPROVE['approver'] == 'approver') {
                                    env.APPROVAL = true
                                    echo 'Manager Approves this Deploy!'
                                currentBuild.result = 'SUCCESS'
                                } else {
                                    env.APPROVAL = false
                                    echo 'You\'re not a Manager'
                                    currentBuild.result = 'FAILURE'
                                }
                            } else {
                                env.APPROVAL = false
                                echo 'Manager Aborts this Deploy'
                                sh "curl -X POST -H \"X-API-KEY: ${API_KEY}\" -H \"x-issue-id: ${issue_id}\" -H \"x-transition-id: 41\" -H \"BuildInfo: ${env.JOB_NAME}/${env.BUILD_NUMBER}\" -H \"Content-type: application/json\" ${AGW_URL}/done-issue"
                                currentBuild.result = 'FAILURE'
                            }
                        }
                    } catch (error) {
                        print(error)
                        print('Manager Aborts this Deploy or Timeout has been reached!\nDeploy automatically aborted')
                        env.APPROVAL = false
                        currentBuild.result = 'FAILURE'
                    }
                }
            }
        }
        stage('Modify Issue Status') {
            when {
                expression {
                    return env.APPROVAL ==~ /(?i)(Y|YES|T|TRUE|ON|RUN)/
                }
            }              
            steps {            
                script {
                    try {
                        sh "curl -X POST -H \"X-API-KEY: ${API_KEY}\" -H \"x-issue-id: ${issue_id}\" -H \"x-transition-id: 31\" -H \"BuildInfo: ${env.JOB_NAME}/${env.BUILD_NUMBER}\" -H \"Content-type: application/json\" ${AGW_URL}/done-issue"
                    }
                    catch(Exception e) {
                        print(e)
                        currentBuild.result = 'FAILURE'
                    }
                    finally {
                        cleanWs()   
                    }
                }
            }
        }
    }
}

def executeCmdReturn(cmd){
  return sh(returnStdout: true, script: cmd).trim()
}

 

자 그러면, 배포를 진행해보도록 하겠습니다.

 

 

Approval stage에서 30분 동안 대기를 하며, 아래와 같이 이슈를 만듭니다.

 

여기서 승인 버튼을 누르면, 아래와 같이 상태가 바뀌는 것을 볼 수 있습니다.

이와 동시에 JIRA Web Hook이 트리거가 됩니다.

 

 

JIRA Web Hook이 아주 성공적으로 작동하여서 아래 젠킨스 파이프라인이 진행된 것을 볼 수 있습니다.

 

 

그리고 Modify Issue API Gateway를 콜하여서 아래 이슈 상태가 완료되며 Close 되는 것을 볼 수 있습니다.

 

 

JIRA - Jenkins Rest API 연동을 API Gateway와 Lambda를 통해 구성해보았습니다.

전체 파이프라인을 만드시고, 중간에 활용하시면 좋을 것 같습니다.