cloud

jenkins pipeline api curl 호출 방법

초이짬 2021. 11. 26. 13:37
728x90

젠킨스로 k8s 배포 파이프라인 만들어 두고 사용하는데 중간에 정적검사 sonarqube 연동할 일이 생겻다. 

 

sonar 연동은 maven plugin 도 가능한데 다른 기능들도 호출해야 되서 api 호출 하는 걸 추가 하는 구문을 만들어 둿다.

 

참고로 해당 응답값이 json 으로 받을경우 해당 json 값 파싱을 위한 플러그인2개를 jenkins에 설치 해줘야 한다.

Pipeline: Groovy

 

Pipeline: Groovy

Pipeline execution engine based on continuation passing style transformation of Groovy scripts.

plugins.jenkins.io

 

Pipeline Utility Steps

 

Pipeline Utility Steps

Utility steps for pipeline jobs.

plugins.jenkins.io

저 2개를 설치 하고 pipeline groovy 는 기본 설치 돼 있었는데 버전업을 해줫다 온라인 상에선 자동 업데이트 되는거 같던데 폐쇄망이라서 2개를 다 설치 해줫다

 

//api 호출
stage('Sonar') {
   steps {
   
          script {
                   final String url = '"http://192.168.219.100:8082/dm2/getHello?id=c&email=shlee0882@gmail.com"'
                   //token 사용 안하는 api 호출이면 아래 --header 부분부턴 필요 없다

                   final String authToken = '"authorization:basic [token값]"'

                    //api get 호출
                    final String response = sh(script: "curl -s --header $authToken $url", returnStdout: true).trim()
                                     
                    echo response
                    def props = readJSON text: response
                   //넘어오는 json 값 확인용 

                   props.each { key, value ->
                        echo "Walked through key $key and value $value"
                    }
                    //호출 에러시
                    if (props.status != null) {
                    // Use SUCCESS FAILURE or ABORTED
                        currentBuild.result = "FAILURE"
                        throw new Exception("sonar server internal error~~~~~~~~~")
                        // do not use the following, as it does not trigger post steps (i.e. the failure step)
                        // error "your reason here"

                    }
                    //결과값 판단
                    if (props.id == "b") {
                    // Use SUCCESS FAILURE or ABORTED
                        currentBuild.result = "FAILURE"
                        throw new Exception("정적 검사 fail~~~~~~~~~~~~~")
                        // do not use the following, as it does not trigger post steps (i.e. the failure step)
                        // error "your reason here"

                    }
                    

}
}
//해당 결과 확인
post{
    failure{
         echo "old version offaile"
    }
    
    success{
         echo "sucsess"
    }
}
}

728x90