Jenkins pipeline sample

By | 2021년 9월 18일
Table of Contents

Jenkins pipeline sample

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

ECR 을 이용한 배포

추가 플러그인 : Docker Pipeline, Amazon ECR, GitHub Integration

node {
    stage("Get Source") {
        git url: "https://github.com/skyer9/HelloWorldWithActuator.git",
            branch: "master"
    }

    stage("Build") {
        sh "chmod 700 gradlew"
        sh "./gradlew clean"
        sh "./gradlew bootJar"
    }

    stage("Dockerize") {
        sh "docker build -t helloworld:0.1.${build_number} ."
        sh "docker build -t helloworld:latest ."
    }

    stage("Push to ECR") {
        // ecr:<Your region>:<Your Jenkins credential ID>
        // myrepo : 이미지명과 ECR 리포지토리 이름은 일치해야 한다.
        docker.withRegistry('https://1234567890.dkr.ecr.ap-northeast-2.amazonaws.com', 'ecr:ap-northeast-2:ecr_user') {
            docker.image('helloworld:latest').push()
        }
    }

    stage("Run Nomad") {
        // Nomad 원격 서버 아이피
        // env.NOMAD_ADDR = "http://nb.skyer9.pe.kr:4646"

        // 클라이언트 노드 프라이빗 아이피
        def local_ipv4 = sh(script: 'wget -qO- http://instance-data/latest/meta-data/local-ipv4', returnStdout: true)
        env.NOMAD_ADDR = "http://$local_ipv4:4646"

        sh "sed -i 's/XXXXXXXXXX/1234567890/g' private_ecr_hello_world.nomad"
        sh "/usr/local/bin/nomad job run private_ecr_hello_world.nomad"
    }
}

답글 남기기