Post

jenkinsEcr업로드실습

jenkinsEcr업로드실습

ECR UPLOAD 실습

  • 완료
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    
    pipeline {
      agent any
      environment {
         ECR_REPO = "727646484333.dkr.ecr.ap-northeast-2.amazonaws.com"
         aws_CREDENTIALS="aws-login"
         ECR_NAME = "petclinic"
         REGION = "ap-northeast-2"
         IAM_ROLE_NAME = "arn:aws:iam::727646484333:role/ecr-registry-full-access"
         ROLE_ACCOUNT = "developer"
      }
    
      stages {
          stage('git clone') {
              steps {
                  git branch: 'main', url: 'https://github.com/237summit/petclinic.git'
              }
          }
          stage('Maven Build') {
              steps {
                  sh './mvnw clean package'
              }
          }
          stage('ECR Upload') {
              steps{
                  script{
                      try {
                          withaws(region: "${REGION}",credentials: "${aws_CREDENTIALS}", role: "${IAM_ROLE_NAME}", roleAccount: "${ROLE_ACCOUNT}", externalId: 'externalId') {
                              sh 'aws ecr get-login-password --region ${REGION} | docker login --username aws --password-stdin  ${ECR_REPO}'
                              sh 'docker build -t ${ECR_NAME} .'
                              sh 'docker tag ${ECR_NAME}:latest ${ECR_REPO}/${ECR_NAME}:v$BUILD_NUMBER'
                              sh 'docker push ${ECR_REPO}/${ECR_NAME}:v$BUILD_NUMBER'
                          }
                      }
                      catch(error){
                          print(error)
                          currentBuild.result = 'FAILURE'
                      }
                  }
              }
              post {
                  success {
                      echo "The ECR Upload stage successfully."
                  }
                  failure {
                      echo "The ECR Upload stage failed."
                  }
              }
          }
      }
      post {
          success {
              slackSend(tokenCredentialId: 'slack_jenkins_token'
                  , channel: '#cicd'
                  , color: 'good'
                  , message: "빌드성공")
          }
          failure {
              slackSend(tokenCredentialId: 'slack_jenkins_token'
                  , channel: '#cicd'
                  , color: 'danger'
                  , message: "빌드실패")
          }
      }
    }
    

webhook 테스트.

This post is licensed under CC BY 4.0 by the author.