Home 시대를 역행하여 Jenkins로 CI/CD 파이프라인 구축하기
Post
Cancel

시대를 역행하여 Jenkins로 CI/CD 파이프라인 구축하기

구축하고자 하는 환경

CI

ci-flow

CD

cd-flow


1. 설치

1
2
3
4
5
sudo wget -O /etc/yum.repos.d/jenkins.repo \
    https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
sudo yum install jenkins
sudo systemctl daemon-reload

2. 접속 후 플러그인 설치 (+ Jenkins 관리 -> System)

2.1 플러그인 설치

Plugin

SSH

SSH Plugin

Docker

Docker Plugin

Git

Git Plugin


2.2 젠킨스 접속 경로 등록 (Jenkins Location 탭)

Jenkins-System

Jenkins-Location


2.3 원격 서버 정보 등록 (SSH Server 탭)

Jenkins-System SSH Server


3. Credentials 등록

Jenkins-Credentials


4. GitHub Webhook 설정

GitHub Webhook


5. 파이프라인 구축

Jenkins-Pipeline Setting 1

Jenkins-Pipeline Setting 2

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
pipeline {
    agent any
    environment {
        HARBOR_URL = 'masking'
        HARBOR_PROJECT = 'masking'
        API_IMAGE_NAME = 'masking'
        IMAGE_TAG = '0.1'
        LATEST_IMAGE_TAG = 'latest'
        DOCKER_REGISTRY_CREDENTIALS_ID = credentials('harbor')
        NEXUS_CREDENTIALS = credentials('nexus')
        NEXUS_USERNAME = "${NEXUS_CREDENTIALS_USR}"
        NEXUS_SECRET = "${NEXUS_CREDENTIALS_PSW}"
    }
    triggers {
        githubPush()
    }
    stages {
        stage('개발 브랜치 접근') {
            steps {
                checkout([
                        $class           : 'GitSCM',
                        branches         : [[name: 'develop']],
                        userRemoteConfigs: [[url: 'masking', credentialsId: 'masking']]
                ])
            }
        }
        stage('프로젝트 빌드') {
            steps {
                sh 'chmod +x gradlew'
                sh "./gradlew clean bootJar"
            }
        }
        stage('API 모듈 이미지 빌드 및 업로드') {
            steps {
                script {
                    def buildAndPushImage = {
                        def fullImageName = "${HARBOR_URL}/${HARBOR_PROJECT}/${API_IMAGE_NAME}:${IMAGE_TAG}"
                        def latestFullImageName = "${HARBOR_URL}/${HARBOR_PROJECT}/${API_IMAGE_NAME}:${LATEST_IMAGE_TAG}"

                        sh "docker build -t ${fullImageName} ./masking/"
                        sh "docker tag ${fullImageName} ${latestFullImageName}"

                        docker.withRegistry("https://${HARBOR_URL}", 'masking') {
                            docker.image(fullImageName).push()
                            docker.image(latestFullImageName).push()
                        }
                    }
                    buildAndPushImage('masking', API_IMAGE_NAME, HARBOR_PROJECT)
                }
            }
        }

        stage('API 모듈 배포 스크립트 실행') {
            steps {
                script {
                    sshPublisher(
                            publishers: [
                                    sshPublisherDesc(
                                            configName: "SSH Server 탭에 등록한 Name",
                                            transfers: [
                                                    sshTransfer(
                                                            execCommand: "masking.sh"
                                                    )
                                            ]
                                    )
                            ]
                    )
                }
            }
        }
    }
    post {
        success {
            echo '배포 성공'
        }
        failure {
            echo '배포 실패'
        }
    }
}
This post is licensed under CC BY 4.0 by the author.