6-keem
Gallery
About
© Powered by 6-keem

Jenkins로 CI/CD 구축하기 (2)

Backend
2025년 02월 10일
3분

CI/CD

Series bookmark
  1. Jenkins로 CI/CD 구축하기 (1)
  2. Jenkins로 CI/CD 구축하기 (2)
On this page
  • 들어가며
  • 1. Jenkins 접속
  • 2. Credentials 생성
  • GitHub Credentails
  • Docker Credentails
  • 3. plugins 다운로드
  • 4. GitHub Webhook 설정
  • 5. Pipeline 구축
  • 6. 결과 확인

이전 포스트
Jenkins로 CI/CD 구축하기 (1)
다음 포스트
Spring 개요
thumbnail.png
이전 글에 이어지는 내용입니다.

들어가며

  • 내가 설계한 기본적인 프로세스는 다음과 같다

메인 브랜치로 머지 → Webhook 전달 → Docker 이미지 빌드 → Docker Hub에 푸시 → 결과 알림

본 글에서 Discord 알림을 제외한 모든 프로세스를 다룰 것이다.

1. Jenkins 접속

브라우저에서 ec2 public ip에 8080 포트로 jenkins 서버에 접속한다.

예시) http://0.0.0.0:8080

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

명령어를 통해 출력된 admin 비밀번호로 로그인이 가능하다.

  • Install suggeseted plugins 설치
  • 로그인 계정을 생성하고 젠킨스 접근 URL을 입력한다

2. Credentials 생성

GitHub Credentails

GitHub Access Token 발급

Settings > Developer Settings > Personal access tokens (classic) > Generate new token (classic)

  • repo 관련 권한과 hook 권한을 체크해준다.

GitHub Credentails 등록

jenkins 관리 > Credentials > System > Glabal credentials > Add Credentials

  • Kind: Username with password
  • Scope: Global
  • Username: GitHub username
  • Password: GitHub access token
  • ID: Identifier used in pipeline code (e.g., github-credentials)

Docker Credentails

  • Kind: Username with password
  • Scope: Global
  • Username: Docker Email
  • Password: Docker Password
  • ID: Identifier used in pipeline code (e.g., github-credentials)

3. plugins 다운로드

GitHub API Plugin
Generic Integration
Generic Webhook Trigger
SSH Agent Plugin
Docker
Docker Pipeline

4. GitHub Webhook 설정

Settings > Webhooks > Add webhook

Payload URL 예시 - http://0.0.0.0:8080/github-webhook/ (jenkins 주소)

5. Pipeline 구축

new items > pipeline

사진과 같이 선택하고 GitHub 레포지토리 URL을 넣으면 된다.

pipeline {
    agent any
 
    environment {
        JAVA_HOME = "/usr/lib/jvm/java-17-amazon-corretto"
        GRADLE_HOME = "/opt/gradle/gradle-8.12"
        PATH = "${JAVA_HOME}/bin:${GRADLE_HOME}/bin:${env.PATH}"
        IMAGE_NAME = "도커 닉네임/생성할 이미지 이름"
    }
 
    triggers {
        githubPush()
    }
 
    stages {
 
        stage('git clone') {
            steps {
                git branch: 'main', credentialsId: 'github-credentials', url: '본인 레포지토리'
            }
        }
 
        stage('빌드 실행') {
            steps {
                sh "./gradlew clean build --no-daemon"
            }
        }
 
        stage('이미지 빌드') {
            steps {
                script {
                    dockerImage = docker.build("${env.IMAGE_NAME}:${env.BUILD_NUMBER}")
                }
            }
        }
 
        stage('도커 허브에 푸시') {
            steps {
                script {
                    docker.withRegistry('https://index.docker.io/v1/', 'docker-hub-credential') {
                        dockerImage.push()
                    }
                }
            }
        }
 
        stage('빌드한 이미지 삭제') {
            steps {
                script {
                    sh "docker rmi ${env.IMAGE_NAME}:${env.BUILD_NUMBER}"
                }
            }
        }
    }
 
    post {
        success {
            echo 'Succeed'
        }
        failure {
            echo 'Failed'
        }
    }
}

6. 결과 확인

main 브랜치에 머지되면 이미지 빌드를 수행하고 Docker Hub에 업로드 되는 것을 볼 수 있다.