Springboot Gradle + AWS EC2 + Jenkins + Docker๋กœ ๋ฐฐํฌ๊นŒ์ง€(2) : Jenkins์™€ Github ์—ฐ๋™ํ•˜๊ธฐ

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

์ด์ œ ec2์— jenkins๋ฅผ ์“ธ ์ˆ˜ ์žˆ๋Š” ์ค€๋น„๋ฅผ ์™„๋ฃŒํ–ˆ์œผ๋‹ˆ, ์ด์ œ๋Š” github๊ณผ Jenkins๋ฅผ ์—ฐ๊ฒฐํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฐ๋™ํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  • github ์•„์ด๋””/๋น„๋ฐ€๋ฒˆํ˜ธ๋กœ ์ ‘์†
  • ec2 ssh ๊ณต๊ฐœํ‚ค/๋น„๋ฐ€ํ‚ค๋กœ ์ ‘์†

์—ฌ๊ธฐ์„œ๋Š” ๋ณด์•ˆ ์ƒ์˜ ์ด์œ ๋กœ ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์ธ ssh๋กœ ์—ฐ๊ฒฐํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฐ๋™ํ•˜๋Š” ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

1. EC2์—์„œ ssh ํ‚ค ์ƒ์„ฑ 

2. Github์—์„œ ssh ํ‚ค, webhook ๋“ฑ๋ก

3. Jenkins์— Credential ssh ํ‚ค ๋“ฑ๋ก

4. Gradle ๋ฒ„์ „ ์„ค์ •ํ•˜๊ธฐ

 

1๏ธโƒฃ EC2 ssh ํ‚ค ์ƒ์„ฑํ•˜๊ธฐ

docker-compose ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๋‚˜์„œ ls -a ๋กœ ํด๋”๋ฅผ ๋ณด๋ฉด .ssh๊ฐ€ ์ƒ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. .ssh ํด๋”๋กœ ๋“ค์–ด๊ฐ€์„œ ssh ํ‚ค๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๋Š” ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•ด์ฃผ์„ธ์š”.

๐Ÿ“‚ /home/ec2-user/compose/.ssh

sudo ssh-keygen - rsa

ํ•ด๋‹น ssh ํ‚ค๋Š” /root/.ssh/id_rsa ์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ssh ํ‚ค ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์œผ๋ฉด, cat ํ•ด๋‹น ๊ฒฝ๋กœ๋กœ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

 

2๏ธโƒฃ Github์—์„œ ssh ํ‚ค ๋“ฑ๋ก

์ด์ œ ssh์˜ ๊ณต๊ฐœ ํ‚ค๋ฅผ github์— ์ €์žฅํ•ด๋ด…์‹œ๋‹ค. ๋จผ์ € sudo cat /root/.ssh/id_rsa.pub ์œผ๋กœ ๊ณต๊ฐœํ‚ค ๊ฐ’์„ ์–ป์–ด์˜ต๋‹ˆ๋‹ค.

ํ‚ค ๊ฐ’์„ ๋ณต์‚ฌํ•ด์„œ ํ”„๋กœ์ ํŠธ ๋ ˆํฌ์ง€ํ† ๋ฆฌ settings > Deploy Keys ๋กœ ๋“ค์–ด๊ฐ€์„œ ํ‚ค๋ฅผ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค. ์ €๋Š” Jenkins๋ผ๋Š” ์ œ๋ชฉ์œผ๋กœ ํ‚ค๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  github webhook์œผ๋กœ ์†Œ์Šค์ฝ”๋“œ๋ฅผ master์— push ํ•˜๋ฉด ์ž๋™์œผ๋กœ Jenkins๊ฐ€ ๋นŒ๋“œํ•ด์ฃผ๋„๋ก ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋™์ผํ•œ settings์—์„œ ์ด๋ฒˆ์—” webhook์œผ๋กœ ๊ฐ€์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ•ด์ฃผ์„ธ์š”. 

 

 

3๏ธโƒฃ Jenkins์— Credential ๋“ฑ๋ก

์ด์ œ ๋‹ค์‹œ ec2๋กœ ๋Œ์•„๊ฐ€์„œ, ๋น„๋ฐ€ํ‚ค๋ฅผ ๊ฐ€์ ธ์™€๋ด…์‹œ๋‹ค. sudo cat /root/.ssh/id_rsa ์œผ๋กœ ๋น„๋ฐ€ ํ‚ค๋ฅผ ๊ฐ€์ ธ์™€์„œ, ์ด๋ฒˆ์—๋Š” Jenkins๋กœ ๊ฐ€๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

ssh private key

Jenkins ๊ด€๋ฆฌ > Manage Credentials > Stores scoped to Jenkins > Global Credentials๋กœ ๋“ค์–ด๊ฐ€์„œ Credential์„ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

Credentials ํƒ€์ž…์€ SSH Username with private key์ž…๋‹ˆ๋‹ค. Private Key์— ec2์—์„œ ๋ณต์‚ฌํ–ˆ๋˜ ssh private key๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

 

 

4๏ธโƒฃ Gradle ๋ฒ„์ „ ์„ค์ •ํ•˜๊ธฐ

์ €๋Š” springboot, gradle์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— Jenkins ์„ค์ •> Global Tool Configuration์—์„œ ๋ฏธ๋ฆฌ ์ œ gradle ๋ฒ„์ „์„ ์ €์žฅํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. gradle ๋ฒ„์ „์€ intellij์—์„œ ๐Ÿ“‚gradle > ๐Ÿ“‚wrapper > gradle-wrapper.properties์— ๊ฐ€๋ฉด ์•Œ ์ˆ˜์žˆ์Šต๋‹ˆ๋‹ค.

 

gradle ์„ค์ •

 

 

์ด์ œ ์ƒˆ๋กœ์šด jenkins ์•„์ดํ…œ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์ค€๋น„๊ฐ€ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค! 

๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ๋Š” ์•„์ดํ…œ์„ ์ƒ์„ฑํ•˜๊ณ , github ๋ ˆํฌ์ง€ํ† ๋ฆฌ์— ์žˆ๋Š” springboot ํ”„๋กœ์ ํŠธ๋ฅผ ๋นŒ๋“œํ•ด๋ณด๋Š” ๊ณผ์ •์„ ํ…Œ์ŠคํŠธํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.