CI/CD์˜ ๋ชฉ์ 

์•„๋ž˜์˜ ์ž‘์—…์„ GITLAB์— pushํ•˜๋ฉด ๋ณ€๊ฒฝ๋œ ๋‚ด์šฉ์ด ๋นŒ๋“œ๊นŒ์ง€ ๋ฐ˜์˜๋˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ, ์ฆ‰, ๋นŒ๋“œ์™€ ํ…Œ์ŠคํŠธ ๊ทธ๋ฆฌ๊ณ  ๋ฐฐํฌ๊นŒ์ง€ ์ž๋™ํ™”ํ•ด์ฃผ๋Š” ๊ฒƒ์ด CI/CD์˜ ๋ชฉ์ ์ด๋‹ค.

sudo su -
cd ๋ฐฐํฌํ•˜๊ณ  ์‹ถ์€ ํ”„๋กœ์ ํŠธ ๋“ค์–ด๊ฐ€๊ธฐ
git pull
docker build -t ์ด๋ฆ„ . 
docker images
docker stop ๊ธฐ์กด container_id
docker run <project ์ด๋ฆ„> <new version>

Untitled

Untitled (1)

  • ๋ฌธ์ œ๋Š” Build Server(ex. gitlab-runner)์™€ ์‹ค์ œ ๋ฐฐํฌ๋˜๋Š” ์„œ๋ฒ„(ex. aws ec2)๋Š” ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค.
  • ์ด๋ฅผ ์œ„ํ•ด, Container Registry๋ฅผ ํ†ตํ•ด Docker Image๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.


๐Ÿ—จ๏ธ ์„ ํ–‰ํ•ด์•ผ๋  ์ž‘์—… : Dockerfile์ด ํ”„๋กœ์ ํŠธ์— ์žˆ์–ด์•ผํ•œ๋‹ค.

Dockerfile

FROM gradle:7.4-jdk11-alpine as builder
WORKDIR /build

# ๊ทธ๋ž˜๋“ค ํŒŒ์ผ์ด ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ๋งŒ ์ƒˆ๋กญ๊ฒŒ ์˜์กดํŒจํ‚ค์ง€ ๋‹ค์šด๋กœ๋“œ ๋ฐ›๊ฒŒํ•จ.
COPY build.gradle settings.gradle /build/
RUN gradle build -x test --parallel --continue > /dev/null 2>&1 || true

# ๋นŒ๋” ์ด๋ฏธ์ง€์—์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋นŒ๋“œ
COPY . /build
RUN gradle build -x test --parallel

# APP
FROM openjdk:11.0-slim
WORKDIR /app

# ๋นŒ๋” ์ด๋ฏธ์ง€์—์„œ jar ํŒŒ์ผ๋งŒ ๋ณต์‚ฌ
COPY --from=builder /build/build/libs/*-SNAPSHOT.jar ./app.jar

EXPOSE 8080

# root ๋Œ€์‹  nobody ๊ถŒํ•œ์œผ๋กœ ์‹คํ–‰
USER nobody
ENTRYPOINT [                                                \
    "java",                                                 \
    "-jar",                                                 \
    "-Djava.security.egd=file:/dev/./urandom",              \
    "-Dsun.net.inetaddr.ttl=0",                             \
    "app.jar"              \
]

Untitled


๐Ÿ—จ๏ธ ๋ณ€์ˆ˜์ถ”๊ฐ€ settings - cicd - variables(expand) - add variable

Untitled 1


  • Docker build์—์„œ gradle ๋นŒ๋“œ๋ฅผ ํ–ˆ์„ ๊ฒฝ์šฐ

    .gitlab-ci.yml - docker build๋ฅผ gitlab-runner(๋ฆฌ๋ˆ…์Šค ์„œ๋ฒ„) ๊ฐ€ ๋Œ€์‹ ํ•ด์ค€๋‹ค.

    # stage โ†’ ๋‹จ๊ณ„
    
    stages:
      - dockerbuild-push
    
    package:
      image: docker:latest
      stage: dockerbuild-push
      services:
        - docker:dind
      before_script:
        - docker login registry.gitlab.com -u $GITLAB_USER -p $GITLAB_PASSWORD
      script:
        - docker build -t registry.gitlab.com/$GITLAB_USER/$PROJECT_NAME . 
        - docker push registry.gitlab.com/$GITLAB_USER/$PROJECT_NAME
      after_script:
        - docker logout
    


  • Docker build ์—์„œ gradle build๋ฅผ ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ

    .gitlab-ci.yml

    stages:
      - build
      - dockerbuild-push
    
    build:
      image: openjdk:11.0-slim
      stage: build
      script:
        - ./gradlew clean
        - ./gradlew build -x test
      before_script:
        - chmod +x gradlew
      artifacts:
        paths:
          - build/libs/*.jar
        expire_in: 8 min
    
    package:
      image: docker:latest
      stage: dockerbuild-push
      services:
        - docker:dind
      before_script:
        - docker login registry.gitlab.com -u $GITLAB_USER -p $GITLAB_PW
      script:
        - docker build -t registry.gitlab.com/$GITLAB_USER/<project-name> .
        - docker push registry.gitlab.com/$GITLAB_USER/<project-name>
      after_script:
        - docker logout
    


  • ์‹คํ–‰๊ณผ์ •

    Untitled 2

    ๋ฆฌ๋ˆ…์Šค ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰

    Untitled 3

    Untitled 4


  • ๊ฒฐ๊ณผ

CI/CD > Jobs

Untitled 5

๋ฐฐํฌ ํ™•์ธ ๋ฉ”์ผ

Untitled 6

Leave a comment