@Library('apim-cicd-base')

// Define Varaibles
def imageName = "apim-swagger-editor-${env.BRANCH_NAME}".toLowerCase()
def buildName = "${imageName}-${BUILD_ID}"
def fullImageName = "${DOCKER_REGISTRY}/${PROJECT}/${imageName}:${BUILD_ID}"
def builderPodLabel = "jenkins=${buildName}"
def dockerBuildName = "${imageName}-${BUILD_ID}"
def dockerImageName = "${fullImageName}"
def objectLabel = "jenkins=${imageName}-${BUILD_ID}"
def jira = [:]
def label = "api-builder-${BUILD_ID}"
def commit_message =""


// Setting Email and Slack Notifications
def config = [:]
config.scm = [ type: 'git', checkoutDir: '.' ]
config.notifications =  [ email: ' ',slackChannel: 'dt-dataint-apim-internal-cicd1',
                          params: [ attachLog: true ]
]
def b = new org.cicd.Build(this, config)
defaultTemplate
        {
            podTemplate( label: label, inheritFrom: 'default', serviceAccount: 'jenkins', serviceAccountName: 'jenkins',
                            containers: [ containerTemplate(name: 'jnlp', image: 'openshift3/jenkins-slave-base-rhel7:v3.11', args: '${computer.jnlpmac} ${computer.name}'),
                     containerTemplate(name: 'tools', image: "${CICD_COMMON_TOOLS_IMAGE}", alwaysPullImage: true, ttyEnabled: true, command: 'cat', resourceRequestMemory: '256Mi', resourceLimitMemory: '256Mi', resourceRequestCpu: '100m', resourceLimitCpu: '100m'),
                    ]
            )

                    {
                        node(label) {
                            // Setting Jenkins Global Enviornment Varaibles
                            
                            // Stage: Prepare workspace
                            b.prepareWorkspace()

                            // Stage: Get latest code
                            b.getLatestCode()

                            // Stage: Prepare for build
                            b.prepareForBuild()

                            // Stage: Build
                            b.build()

                            // Stage: Unit tests
                            b.unitTests()

                            b.staticCodeAnalysis()
                            // Stage: Code Security analysis
                            b.codeSecuirityAnalysis()

                            // Stage: Upload artifacts
                            b.uploadArtifacts()

                            // Stage: Prepare for Docker Image
                            b.prepareForDockerImage()

                            // Stage: Build and Push Docker Image
                            b.buildPushDockerImage(
                                    body: {
                                 
                                if("${PROJECT}"=='qa-apimgt' && BRANCH_NAME=='develop'){
                                            dockerImageName= "${DOCKER_REGISTRY}/sandbox-apimgt/${imageName}:${BUILD_ID}"
                                            env.PROJECT='sandbox-apimgt'
                                            container("tools"){
                                                sh """
                                        oc project sandbox-apimgt
                                                    """
                                            }
                                }

                                        createBuildConfig (
                                                buildName: dockerBuildName,
                                                label: objectLabel,
                                                fullImageName: dockerImageName,
                                                dockerfilePath: "./Dockerfile",
                                                resources: [cpu:"1",memory:"2Gi"]
                                        )

                                        startBuild(buildName: dockerBuildName, contextDirPath: "./")

                                       
                                        if(BRANCH_NAME=='develop'){
                                            container("tools"){
                                                sh """
                                        oc tag ${imageName}:${BUILD_ID} apim-swagger-editor-develop:latest
                                       """

                                            }
                                        }
                                        if(BRANCH_NAME=='master') {
                                            container("tools"){
                                                sh """
                                        oc tag ${imageName}:${BUILD_ID} apim-swagger-editor:latest
                                        oc tag ${imageName}:${BUILD_ID} apim-swagger-editor:prod
                                       """

                                            }
                                        }
                                    },

                                    finBody: {
                                        container('tools') {
                                            sh """
                                        oc delete all -l ${objectLabel}
                                       """
                                        }

                                        //Setting context back to qa after image push to sandbox for dev and other branch
                                        if("${PROJECT}"=='qa-apimgt' && BRANCH_NAME!='master'){
                                            container('tools') {
                                                sh """
                                                    oc project  qa-apimgt
                                                    """
                                            }
                                        }
                                        // Slack and Email notification


                                        //notifyStatus(message: 'SUCCESS', email: config.notifications.email, slackChannel: config.notifications.slackChannel, slackConfig: config.notifications.slackConfig)
                                    }
                            )
                        }
                    }

        }