GitLab CI/CD Pipeline ๊ตฌ์ถ•

Sep 01, 2023
GitLab CI/CD Pipeline ๊ตฌ์ถ•
 

๋ชฉ์ฐจ


  1. CI/CD ํŒŒ์ดํ”„๋ผ์ธ์ด๋ž€?
  1. Gitlab-Runner ์„ค์น˜ ๋ฐ ๋“ฑ๋ก
  1. gitlab-ci.yml ์ž‘์„ฑ
 
 
์ด๋ฒˆ ํฌ์ŠคํŒ…์€ Gitlab Pipeline(CI / CD)๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ๋‹ค.
๋“ค์–ด๊ฐ€๊ธฐ์— ์•ž์„œ ๊ธฐ๋ณธ์ ์ธ ๊ฐœ๋… ์ •๋ฆฌ๋ฅผ ํ•ด๋ณด์ž.

1. CI/CD ํŒŒ์ดํ”„๋ผ์ธ์€ ๋ฌด์—‡์ผ๊นŒ?


CI (Continuous Integration) ์ง€์†์  ํ†ตํ•ฉ - Application ์ฝ”๋“œ์˜ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ์ •๊ธฐ์ ์œผ๋กœ ๋นŒ๋“œ ๋ฐ ํ…Œ์ŠคํŠธ๋ฅผ ๊ฑฐ์ณ ๊ณต์œ  Repository์— ๋ณ‘ํ•ฉ๋˜๋Š” ๊ณผ์ •์˜ ์ž๋™ํ™” ํ”„๋กœ์„ธ์Šค
 
CD (Continuous Delivery, Continuous Deploayment) ์ง€์†์  ์ œ๊ณต/๋ฐฐํฌ - ๊ฐœ๋ฐœ์ž์˜ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„
Repository์—์„œ Production ํ™˜๊ฒฝ๊นŒ์ง€ ๋ฐฐํฌํ•˜๋Š” ๊ณผ์ •์˜ ์ž๋™ํ™” ํ”„๋กœ์„ธ์Šค
 
์œ„์˜ ์ž๋™ํ™” ํ”„๋กœ์„ธ์Šค ๊ณผ์ •์„ โ€œCI/CD ํŒŒ์ดํ”„๋ผ์ธโ€์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.
                                                                                CI/CD ํŒŒ์ดํ”„๋ผ์ธ
CI/CD ํŒŒ์ดํ”„๋ผ์ธ

2. Gitlab-Runner ์„ค์น˜ ๋ฐ ๋“ฑ๋ก


# Download $ curl -LJO "https://gitlab-runner-downloads.s3.amazonaws.com/latest/deb/gitlab-runner_amd64.deb" # Install $ sudo dpkg -i gitlab-runner_amd64.deb
 
ํŒจํ‚ค์ง€ Download ๋ฐ Install ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด gitlab-runner๋ฅผ ์„ค์น˜ํ•œ๋‹ค.
 
์ •์ƒ์ ์œผ๋กœ ์„ค์น˜๋˜์—ˆ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
 
$ gitlab-runner --version Version: 16.3.0 Git revision: 8ec04662 Git branch: 16-3-stable GO version: go1.20.5 Built: 2023-08-20T17:57:26+0000 OS/Arch: linux/amd64
 
โ€ป ๋งŒ์•ฝ Git์„ ์„ค์น˜ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด Git๋ถ€ํ„ฐ ์„ค์น˜ํ•˜์ž.
 
gitlab-runner๋ฅผ ์„ค์น˜ํ•˜์˜€๋‹ค๋ฉด, Pipeline์„ ์—ฐ๊ฒฐํ•  GitLab ํ”„๋กœ์ ํŠธ์— gitlab-runner๋ฅผ ๋“ฑ๋ก ์‹œ์ผœ์ฃผ์–ด์•ผ ํ•œ๋‹ค.
 
notion image
notion image
 
โ€ป ์ด ๊ณผ์ •์€ ํ”„๋กœ์ ํŠธ์˜ Maintainer ๋˜๋Š” Owner ๊ถŒํ•œ์ด ์žˆ๋Š” ๊ณ„์ •์œผ๋กœ ์ง„ํ–‰ ๊ฐ€๋Šฅํ•˜๋‹ค.
 
Settings - CI/CD์— Runners ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ํด๋ฆญํ•˜๋ฉด ์œ„์™€ ๊ฐ™์€ ํ™”๋ฉด์ด ๋‚˜์˜จ๋‹ค.
์—ฌ๊ธฐ์„œ runner๋ฅผ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ โ‘  GitLab URL ๊ณผ โ‘ก Registration token์„ ํ™•์ธํ•˜๊ณ  gitlab-runner๊ฐ€ ์„ค์น˜๋œ ์„œ๋ฒ„์—์„œ ๋“ฑ๋ก์„ ์ง„ํ–‰ํ•˜๋ฉด ๋œ๋‹ค.
 
$ sudo gitlab-runner register Runtime platform arch=amd64 os=linux pid=9412 revision=8ec04662 version=16.3.0 Running in system-mode. Enter the GitLab instance URL (for example, https://gitlab.com/): {GITLAB_URL} Enter the registration token: {REGISTRATION_TOKEN} Enter a description for the runner: {DESCRIPTION} Enter tags for the runner (comma-separated): {TAG} Enter optional maintenance note for the runner: {NOTE} Enter an executor: custom, shell, instance, virtualbox, docker-autoscaler, docker+machine, kubernetes, docker, docker-windows, parallels, ssh: {EXECUTOR}
 
๋“ฑ๋ก์ด ์™„๋ฃŒ๋˜์—ˆ์œผ๋ฉด Runners ์นดํ…Œ๊ณ ๋ฆฌ์— ์ด์™€๊ฐ™์ด Runner๊ฐ€ ๋“ฑ๋ก๋œ๋‹ค.
notion image
 
์—ฌ๊ธฐ๊นŒ์ง€๊ฐ€ gitlab-runner ์„ค์น˜ ๋ฐ ๋“ฑ๋ก ๊ณผ์ •์ด๋‹ค.

3. gitlab-ci.yml ์ž‘์„ฑ


์ด์ œ Gitlab CI/CD ํŒŒ์ดํ”„๋ผ์ธ ์— ๋Œ€ํ•œ ์„ธ๋ถ€ ์ง€์นจ์„ ๊ตฌ์„ฑํ•˜๋Š” .gitlab-ci.yml ์ž‘์„ฑ์ด ๋‚จ์•˜๋‹ค.
 
stages: - build - deploy build: stage: build script: - bash mvnw clean install - echo "Successfully build" tags: - spring artifacts: paths: - target/*.jar expire_in: 10 min deploy: stage: deploy needs: - build script: - at now < start.sh - echo "Successfully deploy" tags: - spring
 
์œ„์™€ ๊ฐ™์ด stages ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ํŒŒ์ดํ”„๋ผ์ธ์˜ ๋‹จ๊ณ„, ์ด๋ฆ„, ์ˆœ์„œ๋ฅผ ์ •ํ•˜๊ณ 
ํ•ด๋‹น ๋‹จ๊ณ„์— ๋งž๊ฒŒ ์‹คํ–‰ํ•  script๋ฅผ ์ •์˜ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
์—ฌ๊ธฐ์„œ Java Spring, Maven์œผ๋กœ ๊ตฌ์„ฑ๋œ Web Application์„ ์˜ฌ๋ฆฌ๊ธฐ ์œ„ํ•ด
build ๋‹จ๊ณ„๋ฅผ ํ†ตํ•ด jar๋ฅผ ์ƒ์„ฑํ•˜๊ณ  deploy ๋‹จ๊ณ„์—์„œ ์ƒ์„ฑ๋œ jar๋ฅผ ๋ฐฐํฌํ•˜๋Š” shell script๋ฅผ ์ด์šฉํ•˜์˜€๋‹ค.
์ข€ ๋” ๋‹ค์–‘ํ•œ ํ‚ค์›Œ๋“œ๋ฅผ ํ™•์ธํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด Gitlab_Docs ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
 
์—ฌ๊ธฐ๊นŒ์ง€ ์„ค์ •ํ•˜์˜€๋‹ค๋ฉด ๊ฐœ๋ฐœ์ž๊ฐ€ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๊ณ  Push๋ฅผ ํ•  ๋•Œ๋งˆ๋‹ค ์•„๋ž˜์™€ ๊ฐ™์€ ํŒŒ์ดํ”„๋ผ์ธ์ด
๊ตฌ์„ฑ๋˜์–ด .gitlab-ci.yml ์—์„œ ์„ค์ •ํ•œ Job์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
 
notion image
 
์„ฑ๊ณต์ ์œผ๋กœ Job์ด ์ˆ˜ํ–‰๋˜์—ˆ์œผ๋ฉด ์ˆ˜์ •ํ•œ ์ฝ”๋“œ๊ฐ€ ์„œ๋ฒ„์— ๋ฐ˜์˜๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š”๊ฒƒ ์žŠ์ง€๋ง์ž.
 

๋งˆ๋ฌด๋ฆฌ


CI/CD ํŒŒ์ดํ”„๋ผ์ธ์„ ์ง์ ‘ ํ•ด๋ณด๋ฉด์„œ ์‚ฌ์šฉํ•ด๋ณด์ง€ ๋ชปํ•œ gitlab-ci.yml ํ‚ค์›Œ๋“œ๋ถ€ํ„ฐ Docker์™€ ๊ฐ™์€ ์ปจํ…Œ์ด๋„ˆ ํ™˜๊ฒฝ ๋ฐ ํด๋ผ์šฐ๋“œ ํ™˜๊ฒฝ์—์„œ์˜ ํŒŒ์ดํ”„๋ผ์ธ์„ ํ™œ์šฉํ•˜๋ฉด ์ข€ ๋” ๋Šฅ๋™์ ์ธ ๊ตฌ์„ฑ์ด ๊ฐ€๋Šฅํ•  ๊ฒƒ์ด๋‹ค.
 

์ฐธ๊ณ 


 
Share article

MYDATA Tech Blog ๐Ÿ’ป