본문 바로가기

리눅스

[리눅스] Spring Boot(Gradle) 빌드 및 배포 구현(github, jenkins, dockerhub)

반응형

Spring Boot(Gradle) 빌드 및 배포 구현(github, jenkins, dockerhub)

1. 개발자 : github main branch에 push
2. Jenkins : github에 push된 spring boot 프로젝트를 가져와서 도커 컨테이너에서 빌드
3. Jenkins : 빌드된 도커 이미지를 docker hub에 push
4. 서버 : docker hub에 올라가 있는 도커 이미지를 다운로드(pull) 받아서 도커 컨테이너 실행

 

spring boot 프로젝트 생성(gradle)

- VSCode에서 스프링 부트 프로젝트(Spring Initializr Java Support)를 생성하였습니다.

$ tree -L 2
.
├── HELP.md
├── bin
│   ├── main
│   └── test
├── build.gradle
├── gradle
│   └── wrapper
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    └── test

github 저장소(repository) 생성

echo "# playground2" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/anti1346/playground2.git
git push -u origin main

playground2 프로젝트를 github 저장소에 push합니다.

$ touch .gitignore

$ echo "# playground2" >> README.md

$ git init

$ git add .

$ git commit -m "first commit"

$ git branch -M main

$ git remote add origin https://github.com/anti1346/playground2.git

$ git push -u origin main


Jenkins 구현(for docker-compose)

$ cat docker-compose.yml
version: '3.7'
services:

  jenkins:
    image: jenkins/jenkins:lts-jdk11
    container_name: jenkins
    restart: always
    privileged: true
    user: root
    environment:
      TZ: "Asia/Seoul"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./jenkins_home:/var/jenkins_home
    ports:
      - 8080:8080
      - 50000:50000
$ docker-compose up -d
docker-compose ps
 Name                Command               State                                           Ports
-----------------------------------------------------------------------------------------------------------------------------------------
jenkins   /sbin/tini -- /usr/local/b ...   Up      0.0.0.0:50000->50000/tcp,:::50000->50000/tcp, 0.0.0.0:8080->8080/tcp,:::8080->8080/tcp

jenkins default password(admin :  /var/jenkins_home/secrets/initialAdminPassword)

$ docker-compose exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword
216b5922b0775ca8904cfc6a2b46b667

github, jenkins 연동

[github] github token 생성

 

[jenkins] github token 등록

 


[jenkins] jenkins item 생성

 

 

echo "github hook trigger"

docker build -t anti1346/playground2 .

docker push anti1346/playground2:latest

 

$ pwd
jenkins_home/workspace/playground2/build/libs

$ ls -l
total 17144
-rw-r--r-- 1 root root 17553012 Dec 28 01:15 playground2-0.0.1-SNAPSHOT.jar

[github] github 저장소(repository)에서 Webhooks 추가

- Payload URL : http://{jenkins server url}:8080/github-webhook/

- Content type : application/json

 

[jenkins] 빌드 유발 >GitHub hook trigger for GITScm polling


[소스] 프로젝트에서 controller 패키지에 HelloController.java를 생성합니다.

##demo=프로젝트 폴더
cd playground2

$ cd src/main/java/com/example/playground2/

$ mkdir controller

$ vim controller/HelloController.java
package com.example.playground2.controller;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class HelloController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        return "Hello, World! #1";
    }
}

소스 코드 github에 push합니다.


Ddockerfile 생성

$ vim Ddockerfile
FROM openjdk:18-jdk-alpine AS builder
COPY gradlew .
COPY gradle gradle
COPY build.gradle .
COPY settings.gradle .
COPY src src
RUN chmod +x ./gradlew
RUN ./gradlew bootJAR


FROM openjdk:18-jdk-alpine
COPY --from=builder build/libs/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app.jar"]

 


github, slack 연동

- GitHub


jenkins, slack 연동

[slack]

- Jenkins CI

 

[jenkins]

- Slack Notification 플러그인 설치

 

 


 

참고URL

- https://velog.io/@hind_sight/Docker-Jenkins-%EB%8F%84%EC%BB%A4%EC%99%80-%EC%A0%A0%ED%82%A8%EC%8A%A4%EB%A5%BC-%ED%99%9C%EC%9A%A9%ED%95%9C-Spring-Boot-CICD

- https://medium.com/hgmin/jenkins-github-webhook-3dc13efd2437

 

728x90
반응형