Springboot Gradle + AWS EC2 + Jenkins + Docker๋กœ ๋ฐฐํฌ๊นŒ์ง€(4) : Jenkins์—์„œ ๋„์ปค ์ด๋ฏธ์ง€ ๋ฐฐํฌํ•˜๊ธฐ

2021. 4. 30. 16:29ใ†๐ŸŒฑ Develop/devOps

  • 1. Dockerfile, docker ๋ฐฐํฌ ํŒŒ์ผ ์ค€๋น„ํ•˜๊ธฐ
  • 2. Jenkins์—์„œ ssh ์„ค์ •ํ•˜๊ธฐ
  • 3. Jenkins ๊ถŒํ•œ ์„ค์ •ํ•˜๊ธฐ

 

์›๋ž˜๋Š” github ์˜ฌ๋ฆผ → Jenkins์—์„œ github์— ์˜ฌ๋ผ์™€์žˆ๋Š” springboot ํ”„๋กœ์ ํŠธ๋ฅผ ๊ฐ€์ ธ์™€์„œ ๋นŒ๋“œ → ๋ฉ”์ธ ์„œ๋ฒ„์— .jar ํ˜•ํƒœ๋กœ ํŒŒ์ผ์„ ์ „์†ก → ์‹คํ–‰ ์„ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์จ์•ผํ•˜์ง€๋งŒ, ์ €๋Š” springboot ์„œ๋ฒ„๋ฅผ docker ์ปจํ…Œ์ด๋„ˆ์—์„œ ์‹คํ–‰ํ•  ๊ฒƒ์ด๋ฏ€๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ”Œ๋กœ์šฐ๋กœ ๊ฐ€๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

github ์˜ฌ๋ฆผ → 
Jenkins์—์„œ github์— ์˜ฌ๋ผ์™€์žˆ๋Š” springboot ํ”„๋กœ์ ํŠธ๋ฅผ ๊ฐ€์ ธ์™€์„œ ๋นŒ๋“œ → 
jenkins ์ปจํ…Œ์ด๋„ˆ์—์„œ ์ด๋ฏธ์ง€๋กœ ๋นŒ๋“œํ•ด์„œ ๋„์ปค hub์— push → 
๋ฉ”์ธ ์„œ๋ฒ„์—์„œ๋Š” docker image๋ฅผ ๋ฐ›์•„์„œ ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰

์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ

 

1๏ธโƒฃ Dockerfile, docker ๋ฐฐํฌ ํŒŒ์ผ ์ค€๋น„ํ•˜๊ธฐ

 

Springboot ์ตœ์ƒ๋‹จ ๊ฒฝ๋กœ์— Dockerfile์„ ํ•˜๋‚˜ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค.

FROM openjdk:8-jdk-alpine 
VOLUME /tmp 
ARG JAR_FILE=build/libs/retrospect-0.0.1-SNAPSHOT.jar 
COPY ${JAR_FILE} app.jar 
EXPOSE 8080 
ENTRYPOINT ["java","-jar","/app.jar"]

Springboot ํ”„๋กœ์ ํŠธ์— Dockerfile ์ƒ์„ฑํ•˜๊ธฐ

 

Jenkins ํ”„๋กœ์ ํŠธ์—์„œ ๋ฐ”๋กœ docker ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์ง€๋งŒ, ํ†ตํ•ฉ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด Jenkins EC2 ์„œ๋ฒ„์— " docker ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ฐ ํ—ˆ๋ธŒ push " ๋ฅผ ํ•˜๋Š” ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์ค์‹œ๋‹ค.

๐Ÿ“‚ home/ec2-user/compose/docker_bp.sh

#bin/bash
#docker image์˜ ์ฒซ tag๋ฅผ ์ผ๊ณ  ๋‹ค์Œ ๋ฒ„์ „์˜ image๋ฅผ ์ƒ์„ฑ
sudo docker build -t ybell1028/yapp-web:$(docker images | awk '($1 == "๋„์ปค ํ—ˆ๋ธŒ ๋ ˆํฌ์ง€ํ† ๋ฆฌ") {print $2 += .01; exit}') .

#docker hub์— push ํ•˜๊ธฐ์œ„ํ•ด login
sudo docker login -u ๋„์ปคํ—ˆ๋ธŒ์•„์ด๋”” -p ๋„์ปคํ—ˆ๋ธŒ๋น„๋ฐ€๋ฒˆํ˜ธ

#docker hub์— push
sudo docker push ๋„์ปค ํ—ˆ๋ธŒ ๋ ˆํฌ์ง€ํ† ๋ฆฌ:$(docker images | awk '($1 == "๋„์ปค ํ—ˆ๋ธŒ ๋ ˆํฌ์ง€ํ† ๋ฆฌ") {print $2 += .01; exit}')

#tag๊ฐ€ "latest"์ธ image๋ฅผ ์ตœ์‹  ๋ฒ„์ „์„ ํ†ตํ•ด ์ƒ์„ฑ
sudo docker tag ๋„์ปค ํ—ˆ๋ธŒ ๋ ˆํฌ์ง€ํ† ๋ฆฌ:$(docker images | awk '($1 == "๋„์ปค ํ—ˆ๋ธŒ ๋ ˆํฌ์ง€ํ† ๋ฆฌ") {p    rint $2; exit}') ๋„์ปค ํ—ˆ๋ธŒ ๋ ˆํฌ์ง€ํ† ๋ฆฌ:latest

#latest๋ฅผ docker hub์— push
sudo docker push ๋„์ปค ํ—ˆ๋ธŒ ๋ ˆํฌ์ง€ํ† ๋ฆฌ:latest

#๋ฒ„์ „ ๊ด€๋ฆฌ์— ๋ฌธ์ œ๊ฐ€ ์žˆ์–ด latest๋ฅผ ์‚ญ์ œ
sudo docker rmi ๋„์ปค ํ—ˆ๋ธŒ ๋ ˆํฌ์ง€ํ† ๋ฆฌ:latest

ํ•ด๋‹น ํŒŒ์ผ์„ ์‹คํ–‰ํ•˜๋ฉด, docker hub์— ๋กœ๊ทธ์ธํ•ด์„œ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค๊ณ  pushํ•˜๋Š” ๊ณผ์ •๊นŒ์ง€ ํ•œ ๋ฒˆ์— ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

2๏ธโƒฃ Jenkins์—์„œ ssh ์„ค์ •ํ•˜๊ธฐ

plugin์—์„œ ssh ๋ฅผ ๋‹ค์šด๋ฐ›์•„์ฃผ๊ณ  ํ™˜๊ฒฝ์„ค์ •์œผ๋กœ ๋“ค์–ด๊ฐ€๋ฉด SSH remote hosts ์„ค์ • ์นธ์ด ์ƒ๊น๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์ด ์žˆ๋Š” Jenkins ์„œ๋ฒ„ ์„ค์ •์„ ์ €์žฅํ•ด์ค๋‹ˆ๋‹ค.

ssh ์„ค์ •ํ•˜๊ธฐ

 

test connection์ด ์„ฑ๊ณตํ•˜๋ฉด ๋‹ค์‹œ ์•„์ดํ…œ ์„ค์ •์œผ๋กœ ๋Œ์•„์˜ต๋‹ˆ๋‹ค. ๋งŒ์•ฝ test connection์—์„œ timeout์ด ๋œฌ๋‹ค๋ฉด, ec2 ์ธ๋ฐ”์šด๋“œ ๊ทœ์น™์—์„œ ssh๊ฐ€ ํŠน์ • IP๋งŒ ํ—ˆ์šฉ๋˜๋Š”์ง€ ํ™•์ธํ•ด์ฃผ์„ธ์š”. ํ˜„์žฌ ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด ์ €๋Š” ์ธ๋ฐ”์šด๋“œ ๊ทœ์น™์— SSH , port 22, ์œ„์น˜ ๋ฌด๊ด€์„ ์„ค์ •ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

 

๋‹ค์‹œ ์•„์ดํ…œ ์„ค์ •์œผ๋กœ ๋Œ์•„์˜ต์‹œ๋‹ค. ๋นŒ๋“œ ํ™˜๊ฒฝ์—์„œ Execute shell script on remote host using ssh ๋ฅผ ์„ ํƒํ•˜๊ณ , ์•„๊นŒ ํ™˜๊ฒฝ์„ค์ •์—์„œ ์ €์žฅํ•œ SSH ์ •๋ณด๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ๋ณธ ์„ค์ •์€ Jenkins๊ฐ€ springboot ํ”„๋กœ์ ํŠธ๋ฅผ ๋นŒ๋“œํ•˜๊ธฐ ์ „ / ํ›„์˜ ์•ก์…˜์„ ์ง€์ •ํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ €๋Š” ๋นŒ๋“œ ํ›„์— "ec2 ์„œ๋ฒ„์— ์žˆ๋Š” .sh ํŒŒ์ผ์„ ์‹คํ–‰ํ•˜๋Š”" ๋ช…๋ น์–ด๋ฅผ ์ €์žฅํ•ด์„œ Jenkins๊ฐ€ ๋„์ปค ์ด๋ฏธ์ง€ ๋นŒ๋“œ ๋ฐ ํ—ˆ๋ธŒ push๊นŒ์ง€ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํƒํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

sudo sh /home/ec2-user/compose/docker_bp.sh

 

 

 

๋งŒ์•ฝ ๋นŒ๋“œ ์‹œ sudo sh~ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์‹คํŒจํ•œ๋‹ค๋ฉด, ๋‹ค์Œ ํฌ์ŠคํŒ…์„ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.

2021.04.30 - [๐ŸŒฑ Develop/devOps] - Jenkins sudo: not found Build step 'Execute shell' marked build as failure ๊ถŒํ•œ ์—๋Ÿฌ ํ•ด๊ฒฐํ•˜๊ธฐ

 

Jenkins sudo: not found Build step 'Execute shell' marked build as failure ๊ถŒํ•œ ์—๋Ÿฌ ํ•ด๊ฒฐํ•˜๊ธฐ

Jenkins๋กœ docker ์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œํ•˜๊ณ  hub์— ํ‘ธ์‹œํ•˜๋Š” ๊ณผ์ •์„ .sh ํŒŒ์ผ ํ•˜๋‚˜๋ฅผ ์‹คํ–‰ํ•ด์„œ ํ•œ๊บผ๋ฒˆ์— ์ˆ˜ํ–‰ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ Jenkins๊ฐ€ ec2 ์„œ๋ฒ„์—์„œ sudo ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ ค๋ฉด, Jenkins์—๊ฒŒ ๋”ฐ๋กœ ๊ถŒํ•œ์„ ์ค˜

tape22.tistory.com

 

 ์Šฌ๋ž™ ์•Œ๋ฆผ๋ฐ›๊ธฐ๋Š” ๋‹ค์Œ ํฌ์ŠคํŠธ์— ์ง„ํ–‰ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!