๋ฐ˜์‘ํ˜•
๋ชฉ์ 

์ž‘์—… ๊ฒฐ๊ณผ๋ฌผ(Springboot jar file, gradle ์ด์šฉ)์„ AWS EC2์— ๋ฐ˜์˜ํ•ด๋ณด๊ธฐ
โ€ฃ jenkins ์„œ๋ฒ„์— ์ž‘์—… ๊ฒฐ๊ณผ๋ฌผ์„ ๋นŒ๋“œ / ๋ฐฐํฌํ•˜๋Š” ์…ˆ.
โ€ฃ ์ž‘์—… ๊ฒฐ๊ณผ๋ฌผ์€ ๊ฐœ์ธ github ์—์„œ ๊ฐ€์ ธ์˜ด! (github ์˜ webhook ๊ธฐ๋Šฅ ์ด์šฉ) 

 

์ž‘์—…์ˆœ์„œ

1๏ธโƒฃ EC2 ์ƒ์„ฑ ๋ฐ ํ„ฐ๋ฏธ๋„ ์ ‘์†
โ€ฃ EC2 ์ƒ์„ฑ๋ฐฉ๋ฒ•์€ ์ƒ๋žตํ•จ. (t2.micro ์ด์šฉ (๋ฉ”๋ชจ๋ฆฌ 1GB..jenkins ๋ฐฐํฌ๋Š” ๋˜๊ธดํ•จ!..), aws linux ์ด์šฉ)
โ€ฃ pem key ์†Œ์ค‘ํ•˜๊ฒŒ ๋ณด๊ด€ํ•˜๋„๋กํ•˜๊ธฐ (ssh ํด๋”์— ์ด๋™์‹œํ‚ค๋Š” ๋“ฑ)

1-1. EC2 ๋ณด์•ˆ๊ทธ๋ฃน ์„ค์ •ํ•˜๊ธฐ (์ธ๋ฐ”์šด๋“œ ๊ทœ์น™ ์„ค์ •)

EC2 > ์ธ์Šคํ„ด์Šค ํ™”๋ฉด
๋ณด์•ˆ๊ทธ๋ฃน > ์ธ๋ฐ”์šด๋“œ ๊ทœ์น™
์ธ๋ฐ”์šด๋“œ ๊ทœ์น™ ์„ค์ • (ํฌํŠธํ—ˆ์šฉ ์„ค์ •)



1-2. EC2 ์ธ์Šคํ„ด์Šค ์„œ๋ฒ„ ์ ‘์† ์‹œ๋„ (2๊ฐ€์ง€)

- pem key ์„ .ssh ํด๋”๋กœ ์˜ฎ๊ฒจ์ฃผ๋Š” ์ž‘์—…์„ ์ถ”๊ฐ€ํ•จ.

$ sudo mv {pem key ์œ„์น˜๊ฒฝ๋กœ}/jenkins.pem ~/.ssh

โญ๏ธ ์ฐธ๊ณ ๋กœ ์‹ค์Šต์˜ ์ƒํ™ฉ์ด๊ธฐ์— EC2 ์„œ๋ฒ„๋ฅผ ๋‚ด๋ฆฌ๊ณ  ์˜ฌ๋ฆฌ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด
[1. ssh ์œผ๋กœ ์ ‘์†ํ•˜๋Š” ๋ฐฉ๋ฒ•] ์œผ๋กœ ํ•˜๋Š”๊ฒƒ์ด ๋งž์„๋“ฏ. (IPv4๊ฐ€ ์„œ๋ฒ„ ์žฌ์‹œ์ž‘์‹œ ๋ฐ”๋€œ)

  1-2-1. ssh ์œผ๋กœ ์ ‘์†ํ•˜๋Š” ๋ฐฉ๋ฒ•

# ssh -i {pem key ๊ฒฝ๋กœ} {ec2 ์ธ์Šคํ„ด์Šค ๊ณ„์ •ID}@{ํผ๋ธ”๋ฆญ IPv4 DNS or ํผ๋ธ”๋ฆญ IPv4 IP}
$ ssh -i ~/.ssh/jenkins.pem ec2-user@ec2-2-3-4-5.ap-northeast-2.compute.amazonaws.com

# root ๊ถŒํ•œ์ด ์•„๋‹Œ ๊ฒฝ์šฐ 
$ sudo ssh -i ~/.ssh/jenkins.pem ec2-user@ec2-2-3-4-5.ap-northeast-2.compute.amazonaws.com

 

  1-2-2. ~/.ssh/config file ์— ์ ‘์†์ •๋ณด ์ถ”๊ฐ€ ํ›„, Host ๋ช…์œผ๋กœ ์ ‘์†ํ•˜๋Š” ๋ฐฉ๋ฒ•

$ vi ~/.ssh/config 

# ์—ด์–ด์„œ ์•„๋ž˜๋‚ด์šฉ ์ถ”๊ฐ€
Host test_jenkins

 User ec2-user
 HostName ec2-2-3-4-5.ap-northeast-2.compute.amazonaws.com
 IdentityFile ~/.ssh/jenkins.pem
 
# ๊ทธ๋ฆฌ๊ณ  ์ €์žฅ (:wq!)
 
# ์ ‘์†์‹œ๋„๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ํ•จ. (root ๊ถŒํ•œ ์•„๋‹Œ ๊ฒฝ์šฐ sudo ssh ~)
$ ssh test_jenkins

 

๐Ÿ’ฅ ๊ทธ๋Ÿฐ๋ฐ, Host ๋ช…์œผ๋กœ ์ ‘์† ์‹œ๋„์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค๋ฉด ? 

> pem key ๊ถŒํ•œ์„ ๋ณ€๊ฒฝํ•ด์ฃผ๋‹ˆ ์ ‘์†์ด ๊ฐ€๋Šฅํ•จ! (chmod 400 ~ ๋‚˜์—๊ฒŒ๋งŒ ์ฝ๊ธฐ ๊ถŒํ•œ ์„ค์ •) 

 

2๏ธโƒฃ EC2 ์„œ๋ฒ„ ์ ‘์† ํ›„, ํ”„๋กœ๊ทธ๋žจ ์„ค์น˜ ๋ฐ ์„œ๋ฒ„ ์…‹ํŒ…

2-1. java ์„ค์น˜
   โ€ฃ ๋‚˜์˜ ๊ฒฝ์šฐ, aws linux์„ ์‚ฌ์šฉํ–ˆ๊ธฐ์— ํ•ด๋‹น๋ฌธ์„œ ← ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์„ค์น˜ํ•จ 

2-2. ํƒ€์ž„์กด ๋ณ€๊ฒฝ (ํ•œ๊ตญ ์‹œ๊ฐ„)

$ sudo rm /etc/localtime
$ sudo ln -s /usr/share/zoneinfo/Asia/Seoul /etc/localtime

# ๋‚ ์งœ ํ™•์ธ
$ date


2-3. ํ˜ธ์ŠคํŠธ๋„ค์ž„ ๋ณ€๊ฒฝ

$ sudo hostnamectl set-hostname mungmang-jenkins
$ sudo reboot

# ๋ช‡๋ถ„ ํ›„ ๋‹ค์‹œ์ ‘์†

# ํ™•์ธ
$ hostname


2-4. jenkins ์„ค์น˜

# jenkins repository ์— ์„ค์ •ํŒŒ์ผ ์ƒ์„ฑ 
$ 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.key

# jenkins ์„ค์น˜
$ sudo yum install jenkins

# jenkins ์‹คํ–‰
$ sudo systemctl start jenkins

# ๋งŒ์•ฝ ๋ถ€ํŒ…์‹œ ๊ฐ€๋Šฅํ•˜๊ฒŒ ์„ค์ •ํ•˜๋ ค๋ฉด ๋‹ค์Œ๋„ ์ถ”๊ฐ€
$ sudo systemctl enable jenkins

 

 

2-5. jenkins ์ ‘์† ๋ฐ ํ”Œ๋Ÿฌ๊ทธ์ธ ์„ค์น˜

๋นจ๊ฐ„๋ฐ•์Šค ๊ฒฝ๋กœ์˜ ๊ฐ’์„ ํ™•์ธํ•˜์—ฌ ์ž…๋ ฅํ•ด์ฃผ๊ธฐ
ํ•œ๊ธ€๋กœ ๋ฒˆ์—ญํ•จ. ์ถ”์ฒœ ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์„ค์น˜ํ•˜์˜€์Œ. (๋ญ๊ฐ€ ํ•„์š”ํ•œ์ง€๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ!)


.. ํ”Œ๋Ÿฌ๊ทธ์ธ ์„ค์น˜ ํ›„,
jenkins ์œ ์ € ๋“ฑ๋ก ํ™”๋ฉด์—์„œ ๋กœ๊ทธ์ธํ•  ๊ณ„์ •์ •๋ณด ์ž…๋ ฅ ↓  ↓ (๊ณ„์ •๋ช… ๋ฐ ์•”ํ˜ธ๋กœ jenkins ๋กœ๊ทธ์ธ ๊ฐ€๋Šฅ!)

 


2-6. gradle ์„ค์น˜ https://docs.gradle.org/6.9.1/userguide/installation.html ์ฐธ๊ณ ํ•จ

$ mkdir /opt/gradle
$ unzip -d /opt/gradle gradle-6.9.1-bin.zip
$ ls /opt/gradle/gradle-6.9.1

$ export PATH=$PATH:/opt/gradle/gradle-6.9.1/bin
$ gradle -version

 

2-7. git ์„ค์น˜ https://gamoo12.tistory.com/205 ์ฐธ๊ณ ํ•จ

$ sudo yum update -y
$ sudo yum install git -y
$ git version

 

3๏ธโƒฃ github ์„ค์ •

3-0. ๋ฐ˜์˜ํ•  ํ”„๋กœ์ ํŠธ๋Š” ์žˆ๋‹ค๊ณ  ๊ฐ€์ • (์ž‘์—… ๊ฒฐ๊ณผ๋ฌผ)
3-1. EC2์—์„œ ssh key ์ƒ์„ฑ

$ cd ~/.ssh
$ ssh-keygen

# id_rsa key ์ƒ์„ฑ



3-2. github project ๋‚ด XXX.pub key ๋“ฑ๋ก ํ•„์š”

    3-2-1. https://github.com/{git๊ณ„์ •}/{gitํ”„๋กœ์ ํŠธ}/settings/keys/new ์ด๋™ 

$ cat ~/.ssh/id_rsa.pub

# ssh-rsa ~ ๋‚ด์šฉ์„ ๋ณต์‚ฌ

github > ํ”„๋กœ์ ํŠธ > Settings > Deploy keys > Add deploy key


3-3. github ํ”„๋กœ์ ํŠธ ๋‚ด webhook ์„ค์ •ํ•„์š”
    3-2-1.https://github.com/{git๊ณ„์ •}/{gitํ”„๋กœ์ ํŠธ}/settings/hooks/new ์ด๋™ 
        • Payload URL : http://{ec2์— jenkins ๋„์šด ์ฃผ์†Œ (ex. ec2~~:8080}/github-webhook/ ์ž…๋ ฅ
        • Content type :  application/json

jenkins์—์„œ build ๋ฒ„ํŠผ ํด๋ฆญ์‹œ, github์˜ webhook ๊ธฐ๋Šฅ ์ด์šฉํ•˜์—ฌ ์ž‘์—… ๊ฒฐ๊ณผ๋ฌผ ํ”„๋กœ์ ํŠธ๋ฅผ ๋•ก๊ฒจ๊ฐ€๋„๋ก ํ• ๊ฒƒ์ž„

 

4๏ธโƒฃ jenkins ์„ค์ •

1. Credentials ๋“ฑ๋ก ํ•„์š” (github ๐Ÿ”› jenkins ์—ฐ๊ฒฐ์œ ๋„)

Jenkins ๊ด€๋ฆฌ > Manage Credentials > Stores scoped Jenkins ์˜ Jenkins ํด๋ฆญ > Global credentials
 
Kind : SSH Username with private key ์„ ํƒ
Private Key: .ssh/id_rsa ์„ ์ž…๋ ฅ

 

2. ๋นŒ๋“œ ๋ฐ ์‹คํ–‰ (Dashboard > ๋Œ€์ƒ ํ”„๋กœ์ ํŠธ > ๊ตฌ์„ฑ) 

๋นŒ๋“œ์œ ๋ฐœ
GitHub hook trigger for GITScm polling ์„ ํƒ 

[ํ”Œ๋Ÿฌ๊ทธ์ธ ์„ค์น˜ ํ•„์š”] post build task (์•„๋ž˜์˜ ๋‚ด์šฉ ๊ธฐ์ž…, ์ž‘์—… ๊ฒฐ๊ณผ๋ฌผ jar ํŒŒ์ผ๋กœ ๋งŒ๋“  ๊ฒƒ์„ ์‹คํ–‰)
JAR=$(ls -tr /var/lib/jenkins/workspace/{jenkins working directory ๋ช…}/build/libs | grep jar | tail -n 1)
echo $JAR
nohup java -jar /var/lib/jenkins/workspace/{jenkins working directory ๋ช…}/build/libs/$JAR &

$(ls -tr /var/lib/jenkins/workspace/{jenkins working directory ๋ช…}/build/libs | grep jar | tail -n 1) : jar ํŒŒ์ผ๋ช… ์ถ”์ถœ ์œ„ํ•จ.
nohup java -jar /var/lib/jenkins/workspace/{jenkins working directory ๋ช…}/build/libs/$JAR & : jar ์‹คํ–‰์œ„ํ•จ, nohup ์„ ๋ถ™์ž„์œผ๋กœ์จ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰! (ํ„ฐ๋ฏธ๋„ ์ข…๋ฃŒ ํ•˜๋”๋ผ๋„ ์ž‘์—… ๊ฒฐ๊ณผ๋ฌผ์€ ๊ณ„์† ์„œ๋ฒ„์— ์‹คํ–‰๋˜์–ด ์žˆ๋„๋ก ํ•˜๊ธฐ์œ„ํ•จ)

 

 

๋Š๋‚€์ 

โ€ฃ ์‚ฝ์งˆ์„ ์ค„์ผ ํ•„์š”์„ฑ์ด์žˆ๊ฒ ๋‹ค.
โ€ฃ ํŒŒ์ดํ”„๋ผ์ธ ์ž‘์„ฑ์„ ์ด์šฉํ•œ ๋ฐฐํฌ๋„ ํ•œ๋ฒˆ ํ•ด๋ณด์•„์•ผ ํ• ๊ฒƒ๊ฐ™๋‹ค. 
โ€ฃ docker..kubernetes ๋“ฑ์„ ์ด์šฉํ•œ ๋ฐฐํฌ๋ฐฉ๋ฒ•๋„ ํ•ด๋ณด์•„์•ผ ํ• ๊ฒƒ๊ฐ™๋‹ค. + ๋‹ค๋ฅธ ์„œ๋ฒ„์— ๋ฐฐํฌํ•˜๋Š” ๋ฐฉ๋ฒ• ๋“ฑ 

 


์ฐธ๊ณ 

์ฑ…: ์Šคํ”„๋ง ๋ถ€ํŠธ์™€ AWS๋กœ ํ˜ผ์ž ๊ตฌํ˜„ํ•˜๋Š” ์›น ์„œ๋น„์Šค

 

์Šคํ”„๋ง ๋ถ€ํŠธ์™€ AWS๋กœ ํ˜ผ์ž ๊ตฌํ˜„ํ•˜๋Š” ์›น ์„œ๋น„์Šค - YES24

๊ฐ€์žฅ ๋น ๋ฅด๊ณ  ์‰ฝ๊ฒŒ ์›น ์„œ๋น„์Šค์˜ ๋ชจ๋“  ๊ณผ์ •์„ ๊ฒฝํ—˜ํ•œ๋‹ค. ๊ฒฝํ—˜์ด ์‹ค๋ ฅ์ด ๋˜๋Š” ์ˆœ๊ฐ„!์ด ์ฑ…์€ ์ œ๋ชฉ ๊ทธ๋Œ€๋กœ ์Šคํ”„๋ง ๋ถ€ํŠธ์™€ AWS๋กœ ์›น ์„œ๋น„์Šค๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. JPA์™€ JUnit ํ…Œ์ŠคํŠธ, ๊ทธ๋ ˆ์ด๋“ค, ๋จธ์Šคํ…Œ์น˜, ์Šคํ”„๋ง

www.yes24.com

๋งํฌ: https://lemontia.tistory.com/656

 

[Linux] centos7 ์  ํ‚จ์Šค ์„ค์น˜

1. jenkins repository๋ฅผ ์„ค์ •ํŒŒ์ผ์„ ์ƒ์„ฑ wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo 2. /etc/yum.repos.d/jenkins.repo ๊ฒฝ๋กœ์— ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์•˜๋Š”์ง€ ํ™•์ธ..

lemontia.tistory.com

 

๋ฐ˜์‘ํ˜•

+ Recent posts