๐Ÿ“’ Jenkins์— GitLab ์ €์žฅ์†Œ ์—ฐ๋™

์–‘์•„ยท2021๋…„ 11์›” 24์ผ
3

JENKINS

๋ชฉ๋ก ๋ณด๊ธฐ
1/1
post-thumbnail
post-custom-banner

์•ž์„œ ์„ค๋ช…ํ•œ EC2์— Jenkins์™€ Docker๋ฅผ ์„ค์น˜ํ•œ ํ›„ GitLab๊นŒ์ง€ ์—ฐ๋™์‹œ์ผœ์„œ ์ž๋™์œผ๋กœ ๋ฐฐํฌ ๋ฐ ๋นŒ๋“œ๊ฐ€ ๊ฐ€๋Šฅํ•˜๊ฒŒ๋” ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์ด ๋ถ€๋ถ„๊นŒ์ง€ ์™„์„ฑ๋˜๋ฉด ์ •๋ง ๋! ๐Ÿ™Œ

[โฌ‡๏ธ AWS EC2์— Jenkins ์„œ๋ฒ„ ๊ตฌ์ถ• ๋ธ”๋กœ๊ทธ ๊ธ€ ์ฐธ๊ณ  ๋งํฌ]
https://velog.io/@hmyanghm/AWS-EC2์—-Jenkins-์„œ๋ฒ„-๊ตฌ์ถ•
[โฌ‡๏ธ AWS EC2์— Docker ์„ค์น˜ ๋ธ”๋กœ๊ทธ ๊ธ€ ์ฐธ๊ณ  ๋งํฌ]
https://velog.io/@hmyanghm/AWS-EC2์—-Docker-์„ค์น˜


๐Ÿ“ GitLab repository์™€ Jenkins ์—ฐ๋™


๐Ÿ”ธ ย  EC2 ์ ‘์†

$ ssh -i your-pem-file.pem ec2-user@your-ec2-dns-์ฃผ์†Œ
  • your-pem-file.pem: EC2 ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ์‹œ ์„ค์ •ํ•œ ํ‚ค ํŽ˜์–ด ์ด๋ฆ„
  • your-ec2-dns-์ฃผ์†Œ: ec2 ํผ๋ธ”๋ฆญIPv4 DNS ์ฃผ์†Œ

๐Ÿ”ธ ย  git ์„ค์น˜

$ sudo yum install git

๐Ÿ”ธ ย  SSH ์ ‘์†์„ ์œ„ํ•œ Key ์ƒ์„ฑ

GitLab์€ SSH Public Key๋กœ ์ธ์ฆ ํ›„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— GitLab์„ ์—ฐ๋™์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ๋Š” SSH ์ ‘์†์„ ์œ„ํ•œ ํ‚ค ์ƒ์„ฑ์ด ํ•„์š”ํ•˜๋‹ค.

$ ssh-keygen -t rsa -C "your-gitlab-email"
  • your-gitlab-email: ์—ฐ๊ฒฐํ•˜๊ณ ์ž ํ•˜๋Š” GitLab ๊ณ„์ • ์ด๋ฉ”์ผ ์ฃผ์†Œ

๐Ÿ’ฌ ย  ํ•˜๋‹จ ์ด๋ฏธ์ง€ ์„ค๋ช…

  • 1๋ฒˆ: Key ํŒŒ์ผ ์ €์žฅ ์œ„์น˜๋ฅผ ๋ฌป๋Š” ๋‚ด์šฉ์œผ๋กœ ๋””ํดํŠธ๋กœ ์ •ํ•ด์ค€ ์œ„์น˜์— ์ €์žฅํ• ๊ฑฐ๋ฉด ๊ทธ๋ƒฅ ์—”ํ„ฐโŽ!
    ย  ย  ย  ย  ๋งŒ์•ฝ ์œ„์น˜๋ฅผ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด ์›ํ•˜๋Š” ์œ„์น˜ ๊ฒฝ๋กœ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋œ๋‹ค.
  • 2๋ฒˆ: ํ•ด๋‹น Key ํŒŒ์ผ์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜๋ผ๋Š” ๋‚ด์šฉ์ด๋‹ค. (์—ฐ๋™ ์‹œ ํ•„์š”ํ•˜๋‹ˆ ๊ผญ ๊ธฐ์–ตํ•ด๋‘๊ธฐโ—๏ธ)
    ย  ย  ย  ย  ๋งŒ์•ฝ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ์„ ์›ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ทธ๋ƒฅ ์—”ํ„ฐโŽ!
  • 3๋ฒˆ: 2๋ฒˆ์—์„œ ์ž…๋ ฅํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ

๐Ÿ”ธ ย  SSH Key ๊ถŒํ•œ ๋ถ€์—ฌ

SSH Key ์ƒ์„ฑ์ด ์™„๋ฃŒ๋˜๋ฉด 'cd ~/.ssh' ์œ„์น˜์— id_rsa, id_rsa.pub ๋‘ ๊ฐ€์ง€ ํŒŒ์ผ๋กœ ์ €์žฅ์ด ๋œ๋‹ค.
ํ•ด๋‹น ํŒŒ์ผ๋“ค์€ ์ฝ๊ธฐ, ์“ฐ๊ธฐ, ์‹คํ–‰ ๋ชจ๋“  ๊ถŒํ•œ์„ ์‚ฌ์šฉ์ž๋งŒ ๊ฐ€์ ธ์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•ด์ค€๋‹ค.

$ chmod 700 ~/.ssh/id_rsa*

๐Ÿ”ธ ย  pub key ๊ฐ’ ๋ณต์‚ฌ

SSH Key ์ƒ์„ฑ์ด ์™„๋ฃŒ๋˜๋ฉด 'cd ~/.ssh' ์œ„์น˜์— id_rsa, id_rsa.pub ๋‘ ๊ฐ€์ง€ ํŒŒ์ผ๋กœ ์ €์žฅ์ด ๋œ๋‹ค.
ํ•ด๋‹น ํŒŒ์ผ๋“ค์€ ์ฝ๊ธฐ, ์“ฐ๊ธฐ, ์‹คํ–‰ ๋ชจ๋“  ๊ถŒํ•œ์„ ์‚ฌ์šฉ์ž๋งŒ ๊ฐ€์ ธ์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•ด์ค€๋‹ค.

$ cat ~/.ssh/id_rsa.pub

๐Ÿ‘‰ ย  ssh-rsa ๋ถ€ํ„ฐ ์ž…๋ ฅํ–ˆ๋˜ GitLab ๊ณ„์ • ์ด๋ฉ”์ผ ์ฃผ์†Œ๊นŒ์ง€ ๋ชจ๋‘ Key ๊ฐ’์ด๊ธฐ ๋•Œ๋ฌธ์— ์ „๋ถ€ ๋‹ค ๋ณต์‚ฌโ—๏ธ

๐Ÿ”ธ ย  GitLab ๋กœ๊ทธ์ธ ํ›„ SSH Keys ๋“ฑ๋ก

  1. GitLab ๋กœ๊ทธ์ธ

  2. GitLab ํ™”๋ฉด ์šฐ์ธก ์ƒ๋‹จ ํ”„๋กœํ•„ ์„ ํƒ ํ›„ Edit profile๋กœ ์ด๋™

  3. SSH Keys ๋ฉ”๋‰ด ์ด๋™

  4. ๋ณต์‚ฌํ•œ pub key ๊ฐ’ ๋ถ™์—ฌ๋„ฃ์€ ํ›„ Add key ๋ฒ„ํŠผ ํด๋ฆญ

๐Ÿ”ธ ย  Jenkins์— git plugin ์„ค์น˜

  1. Jenkins ๋กœ๊ทธ์ธ

  2. Jenkins ๊ด€๋ฆฌ > ํ”Œ๋Ÿฌ๊ทธ์ธ ๊ด€๋ฆฌ

  3. ์„ค์น˜ ๊ฐ€๋Šฅ ํƒญ์—์„œ git plugin ๊ฒ€์ƒ‰ ํ›„ ํ•ด๋‹น ํ”Œ๋Ÿฌ๊ทธ์ธ ์„ค์น˜
    (๋‚˜๋Š” ์ด๋ฏธ ์„ค์น˜๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์˜ˆ์‹œ ์ด๋ฏธ์ง€๋Š” ์„ค์น˜๋œ ํ”Œ๋Ÿฌ๊ทธ์ธ ๋ชฉ๋ก ํƒญ์ด๋‹ค.)
    ์„ค์น˜ ์™„๋ฃŒ ํ›„ Jenkins ์žฌ์‹œ์ž‘ (์„ค์น˜ ํŽ˜์ด์ง€ ๋งจ ํ•˜๋‹จ์— '์„ค์น˜๊ฐ€ ๋๋‚˜๊ณ  ์‹คํ–‰์ค‘์ธ ์ž‘์—…์ด ์—†์œผ๋ฉด Jenkins ์žฌ์‹œ์ž‘' ๋ฌธ๊ตฌ์— ์ฒดํฌํ•˜๋ฉด ์„ค์น˜ ์™„๋ฃŒ ํ›„ ์ž๋™ ์žฌ์‹œ์ž‘)

๐Ÿ”ธ ย  jenkins ์ƒˆ๋กœ์šด Item ์ƒ์„ฑ

์ƒˆ๋กœ์šด Item์„ ์„ ํƒ ํ›„ ์›ํ•˜๋Š” Project๋ช…์„ ์ž…๋ ฅ ํ›„ Freestyle project๋กœ ์ƒ์„ฑ

๐Ÿ”ธ ย  Jenkins Project ์„ค์ •

  1. ์†Œ์Šค ์ฝ”๋“œ ๊ด€๋ฆฌ์—์„œ Git ์„ ํƒ ๋ฐ ๋นŒ๋“œ ์›ํ•˜๋Š” Branch ์„ค์ •
    GitLab repository ์ค‘ ๋นŒ๋“œ ์›ํ•˜๋Š” repository๋ฅผ ์„ ํƒํ•˜์—ฌ SSH ์ฃผ์†Œ ๋ณต์‚ฌ

  2. Jenkins ์†Œ์Šค ์ฝ”๋“œ ๊ด€๋ฆฌ ํ™”๋ฉด์—์„œ Git์— ์ฒดํฌ ํ›„ Repository URL์— ๋ณต์‚ฌํ•œ SSH ์ฃผ์†Œ ๋ถ™์—ฌ๋„ฃ๊ธฐ
    ๊ทธ ํ›„ ์•„๋ž˜์— Branch Specifier ๋ถ€๋ถ„์—๋Š” ๋นŒ๋“œ ํ•  GitLab Branch๋ช…์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.
    ๊ธฐ๋ณธ ์„ค์ •์€ master๋กœ ๋˜์–ด์žˆ๋Š”๋ฐ ๋‚˜๋Š” develop ๋ธŒ๋žœ์น˜๋ฅผ ์‚ฌ์šฉํ•  ์˜ˆ์ •์ด๋ผ ์ˆ˜์ •ํ•ด์คฌ๋‹ค.

  3. Credentials(private key) ์ƒ์„ฑ

  • Kind: Credentials ์ข…๋ฅ˜ ์„ ํƒ (์—ฌ๊ธฐ์—์„œ๋Š” SSH Key๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— SSH Username with private key ์„ ํƒ)
  • ID: Credentials๋ช…์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ์ด๋ฆ„ ์ž…๋ ฅ
  • Description: Credentials ์„ค๋ช…์ด ํ•„์š”ํ•˜๋‹ค๋ฉด ์ด ๋ถ€๋ถ„์— ์ž…๋ ฅ
  • Username: Credentials ์‚ฌ์šฉ์ž ์ž…๋ ฅ (๋‚˜๋Š” ํ•ด๋‹น SSH Key๋ฅผ GitLab์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •์ด๋‹ˆ ๋‚˜์ค‘์— ์•Œ์•„๋ณด๊ธฐ ์‰ฝ๋„๋ก GitLab ๊ณ„์ • ์ž…๋ ฅ)
  1. ์•„๋ž˜๋กœ ์Šคํฌ๋กค ํ•ด๋ณด๋ฉด Enter derectly๊ฐ€ ๋ณด์ผ ๊ฒƒ์ด๋‹ค. ์ด ๋ถ€๋ถ„ ์ฒดํฌ ํ›„ Add ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ SSH Private Key ๊ฐ’์„ ๋„ฃ์„ ์ค€๋น„๋ฅผ ํ•œ๋‹ค.

  2. AWS EC2 ์„œ๋ฒ„์—์„œ ๋ฐœ๊ธ‰ ๋ฐ›์€ SSH Private Key ๊ฐ’ ์ž…๋ ฅ

//SSH Private Key ๊ฐ’ ๊ฐ€์ง€๊ณ  ์˜ค๋Š” ๋ช…๋ น์–ด
$ cat ~/.ssh/id_rsa
  • '----BEGIN RSA PRIVATE KEY----' ๋ถ€ํ„ฐ '----END RSA PRIVATE KEY----' ๊นŒ์ง€ ์ „๋ถ€ ๋ณต์‚ฌ

  • ๋ณต์‚ฌํ•œ SSH Private Key ๊ฐ’ ๋ถ™์—ฌ๋„ฃ๊ธฐ

  1. ์ƒ์„ฑํ•œ Credentials ์„ ํƒ
    ์•ž์—์„œ ์ƒ์„ฑํ•œ Credentials Key๋ฅผ ์„ ํƒํ•œ๋‹ค.
    ๋งŒ์•ฝ ์œ„์—์„œ Repository URL์„ ์ž…๋ ฅํ–ˆ์„ ๋•Œ Failed to connect to repository : Command ~~ returned status code 128: ~~~ Permission denied (publickey) ์ด๋Ÿฐ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๊ฐ€ ๋‚˜ํƒ€๋‚ฌ์—ˆ๋‹ค๋ฉด Credentials Key ์„ ํƒ ํ›„ ๋ช‡ ์ดˆ ๊ธฐ๋‹ค๋ฆฌ๋ฉด ํ•ด๋‹น ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋Š” ์‚ฌ๋ผ์งˆ ๊ฒƒ์ด๋‹ค.

๋‚˜๋Š” ์—ฌ๊ธฐ์— GitLab Webhook ์—ฐ๋™๊นŒ์ง€ ๋„ฃ์„ ์˜ˆ์ •์ด๊ธฐ์— ์ด์–ด์„œ ํ•ด๋‹น ์„ค์ • ๋ถ€๋ถ„๊นŒ์ง€ ์ž‘์„ฑํ–ˆ๋‹ค.
๋งŒ์•ฝ GitLab Webhook ์—ฐ๋™์ด ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค๋ฉด ์—ฌ๊ธฐ๊นŒ์ง€ ์„ค์ • ํ›„ Jenkins ์„ค์ • ํ™”๋ฉด์—์„œ Build ํƒญ ๋ถ€๋ถ„์— ๊ฐ์ž ํ”„๋กœ์ ํŠธ์— ๋งž๋Š” ๋นŒ๋“œ ๋ฐฉ๋ฒ•๋งŒ ์ถ”๊ฐ€ํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค!๐Ÿ‘


๐Ÿ“ Jenkins์— GitLab Webhook ์„ค์ •


๐Ÿ”น ย  Jenkins ๋นŒ๋“œ ์œ ๋ฐœ ์„ค์ •

  1. 'Build when a change is pushed to GitLab. GitLab webhook ~~' ์ด ๋ถ€๋ถ„์— ์ฒดํฌํ•ด์ฃผ๊ณ  GitLab webhook URL: ๋’ค์— ์ ํ˜€์žˆ๋Š” URL์€ GitLab์—์„œ ์„ค์ • ์‹œ ์‚ฌ์šฉํ•  ์˜ˆ์ •์ด๋‹ˆ ๊ธฐ์–ตํ•ด๋‘”๋‹ค.
    ๊ทธ๋ฆฌ๊ณ  ๊ณ ๊ธ‰ ๋ฒ„ํŠผ ํด๋ฆญ๐Ÿ–ฑ

  2. ์ด์ „ ๋‹จ๊ณ„์—์„œ ๊ณ ๊ธ‰ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋‚˜์˜ค๋Š” ํ™”๋ฉด์œผ๋กœ ์•„๋งˆ ์ฒ˜์Œ์—” ์˜ˆ์‹œ ์ด๋ฏธ์ง€์™€ ๋‹ฌ๋ฆฌ ๊ณต๋ž€์ผ ๊ฒƒ์ด๋‹ค.
    ๊ทธ๋Ÿผ Generate ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ด์„œ ์˜ˆ์‹œ ์ด๋ฏธ์ง€์ฒ˜๋Ÿผ Secret token์„ ์ƒ์„ฑ์‹œํ‚จ๋‹ค.
    (์ด๋ฏธ ํ† ํฐ์ด ์žˆ๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด Generate ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ์ƒˆ๋กœ์šด ํ† ํฐ์œผ๋กœ ๋ณ€๊ฒฝ๋˜๋‹ˆ ์ฃผ์˜ํ•˜์žโ—๏ธ)
    ์ด ํ† ํฐ ์—ญ์‹œ GitLab์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •์ด๋‹ˆ ๊ธฐ์–ตํ•ด๋‘”๋‹ค.

๐Ÿ”น ย  GitLab Webhook ์„ค์ •

  1. Jenkins์™€ ์—ฐ๊ฒฐํ•  GitLab Repository Webhook ์„ค์ • ํ™”๋ฉด์œผ๋กœ ์ด๋™ํ•œ๋‹ค.

  1. Jenkins ๋นŒ๋“œ ์œ ๋ฐœ ์„ค์ • ์‹œ ๊ธฐ์–ตํ•ด๋’€๋˜ URL๊ณผ Secret token ์ •๋ณด ์ž…๋ ฅ ํ›„ ์ €์žฅ
    ๋‚˜๋Š” Jenkins ์†Œ์Šค ์ฝ”๋“œ ๊ด€๋ฆฌ ๋ถ€๋ถ„์—์„œ develop ๋ธŒ๋žœ์น˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๊ณ  ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— push events ๋ถ€๋ถ„๋„ develop์œผ๋กœ ์„ค์ •ํ–ˆ๋‹ค.

  2. ํ…Œ์ŠคํŠธ ์ง„ํ–‰์œผ๋กœ ์„ค์ • ํ™•์ธ
    ์ƒ์„ฑํ•œ Project Hooks์—์„œ Push events ํ…Œ์ŠคํŠธ ์ง„ํ–‰!

    ์ด๋ ‡๊ฒŒ 200 ์ฝ”๋“œ๊ฐ€ ๋ณด์ด๋ฉด ์„ฑ๊ณต!๐Ÿ™Œ

    Jenkins ์ƒ์—์„œ๋Š” ์ด๋ ‡๊ฒŒ Build History๊ฐ€ ์ƒ๊ธด๊ฒƒ์„ ํ™•์ธํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค.๐Ÿคก

profile
Back-end developer
post-custom-banner

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2021๋…„ 12์›” 3์ผ

์ž‘์„ฑํ•ด์ฃผ์‹  ๊ธ€ ๋•๋ถ„์— ๋งŽ์€ ๋„์›€์„ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!!

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ