주간 랩업/[SCC] Docker 기반 CI&CD 파이프라인 구축하기

2-10. CI/CD 구성 살펴보기

hooooolly 2025. 4. 1. 09:30
✨ GitLab 파이프라인 구성

 

🔵 .gitlab-ci.yml 은 프로젝트의 CI / CD 파이프라인 설정 정보를 담고 있는 파일이다. 이 파일에 각 스테이지별 작업 스크립트를 작성한다.

🔵 Makefile은 make (소프트웨어 빌드 프로세스를 자동화하기 위해 사용하는 프로그램)가 사용하는 빌드 규칙이 담겨있는 파일이다.

 

 

 

.gitlab-ci.yml 을 다음과 같이 수정해서 개발자가 직접 명령을 내리지 않고 자동으로 push까지 하도록 만든다.

stages:          # List of stages for jobs, and their order of execution
  - build
  - test
  - sec
  - deploy

build-job:       # This job runs in the build stage, which runs first.
  stage: build
  script:
    - echo "Compiling the code..."
    - make build
    - echo "Compile complete."

# doc-job:
#   stage: build
#   script:
#     - echo "Compiling the code..."
#     - echo "Compile complete."

push-job:
  stage: build
  script:
    - echo "Docker push Start"
    - make push
    - echo "Docker push complete."


unit-test-job:   # This job runs in the test stage.
  stage: test    # It only starts when the job in the build stage completes successfully.
  script:
    - echo "Running unit tests... This will take about 60 seconds."
    - sleep 5
    - echo "Unit tests complete."

lint-test-job:   # This job also runs in the test stage.
  stage: test    # It can run at the same time as unit-test-job (in parallel).
  script:
    - echo "Linting code... This will take about 10 seconds."
    - sleep 5
    - echo "No lint issues found."

security-check-job:
  stage: sec
  script:
    - echo "Security check Start...End."
    
deploy-job:      # This job runs in the deploy stage.
  stage: deploy  # It only runs when *both* jobs in the test stage complete successfully.
  environment: production
  script:
    - echo "Deploying application..."
    - echo "Application successfully deployed."

 

 

위 파일에서 나오는 make build, make push 명령은 Makefile에서 정의한다.

# 프로젝트 및 ECR 정보
PRJ_NAME=teamjoinc/test-app
ECR_URI=090375680422.dkr.ecr.ap-northeast-2.amazonaws.com
VERSION=$(shell git rev-parse --short HEAD)

# 빌드
build:
	docker build -t $(PRJ_NAME):$(VERSION) .

# 푸시
push:
	aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin $(ECR_URI)
	docker tag $(PRJ_NAME):$(VERSION) $(ECR_URI)/$(PRJ_NAME):$(VERSION)
	docker tag $(PRJ_NAME):$(VERSION) $(ECR_URI)/$(PRJ_NAME):latest
	docker push $(ECR_URI)/$(PRJ_NAME):$(VERSION)
	docker push $(ECR_URI)/$(PRJ_NAME):latest

# 전체 실행
deploy: push

# 클린업
clean:
	docker rmi $(PRJ_NAME):$(VERSION) $(ECR_URI)/$(PRJ_NAME):$(VERSION) || true

 

 

로컬 파일의 변경사항을 깃랩으로 푸시하고 모든 파이프라인이 오류없이 실행되는 것을 확인