Platform/AWS

AWS Jenkins.pipeline

개발자의 삶이란 2023. 7. 20. 13:31
node{

    stage('scm'){
        checkout scm
    }

    def appSystem = sh(returnStdout: true, script: "yq read ./.pipeline/define.yml app.system").trim()
    def appRole = sh(returnStdout: true, script: "yq read ./.pipeline/define.yml app.role").trim()

    // compile scm
    def buildImageUrl = getBuildImageURL()
    def buildCommand = sh(returnStdout: true, script: "yq read ./.pipeline/define.yml build.cmd").trim()

    // build app container image with built app
    def (argRepoHost,argPath,argTag) = getBaseImageURLComponents()
    def buildTag = "${getECRRepoHost()}/${appSystem}.${appRole}:${env.BUILD_ID}"

    stage('echo env'){
        def infoString = """
        BUILD information dump
        APP SYSTEM : ${appSystem}
        APP ROLE   : ${appRole}
        Build Image URL : ${buildImageUrl}
        Build Command : ${buildCommand}
        App container image build base : ${argRepoHost}/${argPath}:${argTag}
        App container image tag : ${buildTag}
        """
        print infoString
    }

    stage("build app"){
        try{
            stage('compile'){
              print "building ${appSystem}.${appRole} with cmd=${buildCommand}"
              sh 'chmod +x gradlew'
              sh buildCommand
            }
        }catch(exc){
            currentBuild.result = 'ABORTED'
            error('application build failed. check source')
        }
    }

    stage("build image"){

      print "docker container image build tag = ${buildTag}"

      script{
          sh '''
            aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin 061470741083.dkr.ecr.ap-northeast-2.amazonaws.com
          '''
      }

      def builtImage = "docker build -t ${buildTag} ."
      sh builtImage

      def tagImage = "docker tag ${buildTag} ${getECRRepoHost()}/${appSystem}.${appRole}:latest"
      sh tagImage

      def pushImage = "docker push ${buildTag}"
      sh pushImage

      def pushTagImage = "docker push ${getECRRepoHost()}/${appSystem}.${appRole}:latest"
      sh pushTagImage

    }

    stage("deploy"){
        build job: "${appSystem}.${appRole}.deploy-revision",parameters: [ [$class: 'StringParameterValue', name: "BUILT_CONTAINER_IMAGE", value: "${buildTag}"]], wait:true
    }
}

def getECRRepoHost(){
    def zone_master_id = env.ZONE_MASTER_ID
    def repo = "${zone_master_id}.dkr.ecr.ap-northeast-2.amazonaws.com"

    return repo
}

def getBuildImageURL(){

    def repo = getECRRepoHost()

    def image_url

    if(BUILD_IMAGE_NAME_WITH_TAG==""){
        def path=sh(returnStdout: true, script: "yq read ./.pipeline/define.yml build.image.name").trim()
        def tag=sh(returnStdout: true, script: "yq read ./.pipeline/define.yml build.image.tag").trim()
        image_url = "${repo}/${path}:${tag}"
    }else{
        image_url = "${repo}/${BUILD_IMAGE_NAME_WITH_TAG}"
    }
    return image_url
}

def getBaseImageURLComponents(){

    def repo = getECRRepoHost()

    def path
    def tag

    if(BASE_IMAGE_NAME_WITH_TAG==""){
        path=sh(returnStdout: true, script: "yq read ./.pipeline/define.yml base.image.name").trim()
        tag=sh(returnStdout: true, script: "yq read ./.pipeline/define.yml base.image.tag").trim()
    }else{
        (path,tag) = BASE_IMAGE_NAME_WITH_TAG.split(":")
    }
    return [repo,path,tag]
}

Jenkins에서 AWS ECR에 파일 업로드하는 스크립트