목록으로 가기

GitHub Actions와 GitLab CI/CD 파이프라인 구성 방법

CI/CD 개념, 파이프라인 구조, 환경 분리, Secrets 관리, Jenkins와 GitLab CI 비교, 파이프라인 디버깅까지 정리했습니다.

GitHub Actions와 GitLab CI/CD 파이프라인 구성 방법 ko posts cicd CI/CD 개념, 파이프라인 구조, 환경 분리, Secrets 관리, Jenkins와 GitLab CI 비교, 파이프라인 디버깅까지 정리했습니다.

개요

Martin Fowler는 Continuous Integration을 팀원 각자가 변경 사항을 최소 하루에 한 번 이상 공유 codebase에 merge하는 개발 방식으로 설명하고, 각 integration을 테스트를 포함한 automated build로 검증해 통합 오류를 가능한 한 빨리 찾는다고 설명합니다 (https://martinfowler.com/articles/continuousIntegration.html). Continuous Delivery는 소프트웨어를 언제든 프로덕션에 release할 수 있도록 만드는 개발 방식입니다 (https://martinfowler.com/bliki/ContinuousDelivery.html).

이 글에서는 도구와 무관한 CI/CD 개념, 파이프라인 구조, 환경 분리, Secrets 관리, Jenkins와 GitLab CI 비교, 파이프라인 디버깅을 정리합니다.

1. CI와 CD는 뭐가 다른가

상세 내용

CI (Continuous Integration)

Martin Fowler의 정의에 따르면 CI에서는 각 integration을 테스트를 포함한 automated build로 검증합니다 (https://martinfowler.com/articles/continuousIntegration.html). 파이프라인에 어떤 검사 단계를 넣을지는 팀이 정하며, 린트나 보안 스캔 단계를 추가하는 구성도 가능합니다.

CD (Continuous Delivery / Continuous Deployment)

AWS 문서는 continuous delivery 파이프라인을 소스 변경이 build, test를 거쳐 non-production 테스트 또는 staging 환경에 배포되는 흐름으로 설명하고, continuous delivery와 continuous deployment의 차이를 프로덕션 배포 전 manual approval이 있는지 여부로 설명합니다 (https://aws.amazon.com/devops/continuous-delivery/).

용어 차이
Continuous Delivery 프로덕션 배포 전에 manual approval이 있음
Continuous Deployment 승인 없이 프로덕션까지 자동 배포

Martin Fowler는 Continuous Deployment를 모든 변경이 파이프라인을 거쳐 자동으로 프로덕션에 배포되는 방식으로 설명합니다. Continuous Delivery는 빈번한 배포를 할 수 있는 상태를 뜻하며, 배포 여부는 팀이 선택할 수 있다고 설명합니다 (https://martinfowler.com/bliki/ContinuousDelivery.html).

흐름 예시

개발자 push
  → CI: 빌드 → 테스트 → 이미지 생성 → Registry push
  → CD: dev 자동 배포 → stg 자동 배포 → prod 수동 승인 후 배포

2. 파이프라인 구조

상세 내용

파이프라인 = 스테이지의 연속

┌──────────┐   ┌──────────┐   ┌──────────┐   ┌──────────┐
│  Build   │ → │   Test   │ → │  Deploy  │ → │  Notify  │
│  (빌드)   │   │  (테스트)  │   │  (배포)   │   │  (알림)   │
└──────────┘   └──────────┘   └──────────┘   └──────────┘

GitLab CI/CD 문서에서 stage는 job을 묶는 단위입니다. stage는 순서대로 실행되고 같은 stage의 job은 병렬로 실행됩니다. stage의 모든 job이 성공하면 파이프라인이 다음 stage로 넘어가고, stage의 job이 하나라도 실패하면 다음 stage는 (보통) 실행되지 않고 파이프라인이 일찍 끝납니다 (https://docs.gitlab.com/ci/pipelines/).

스테이지 구성 예시

스테이지 하는 일 예시
Checkout 코드 가져오기 git clone
Build 빌드/컴파일 npm run build, docker build
Test 테스트 실행 pytest, jest
Lint 코드 품질 검사 eslint, flake8
Scan 보안 취약점 스캔 trivy, snyk
Push 이미지를 Registry에 업로드 docker push
Deploy 환경에 배포 kubectl apply, helm upgrade
Notify 결과 알림 Slack, 이메일

트리거

실행 조건 GitHub Actions GitLab CI/CD
정해진 시간 schedule event pipeline schedule
수동 실행 workflow_dispatch event when: manual로 지정한 manual job (프로덕션 배포 같은 job에 사용할 수 있다고 문서에 설명)

GitHub Actions는 workflow를 trigger하는 event를 on에 지정합니다 (https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows). GitLab CI/CD의 manual job은 사용자가 시작해야만 실행되는 job입니다 (https://docs.gitlab.com/ci/jobs/job_control/). GitLab의 pipeline schedule은 https://docs.gitlab.com/ci/pipelines/schedules/ 에 설명되어 있습니다.

3. 환경 분리 (dev / stg / prod)

상세 내용

환경(environment)

GitHub Actions 문서에서 environment는 production, staging, development 같은 배포 대상을 나타내는 용도입니다. environment를 사용하면 job 진행에 승인을 요구하거나, workflow를 trigger할 수 있는 branch를 제한하거나, secret에 대한 접근을 제한할 수 있습니다 (https://docs.github.com/en/actions/concepts/workflows-and-actions/deployment-environments). required reviewers 같은 deployment protection rule은 사용하는 플랜과 repository 공개 여부에 따라 제공 범위가 다릅니다 (https://docs.github.com/en/actions/how-tos/deploy/configure-and-manage-deployments/manage-environments).

환경 구성 예시

환경 용도 배포 방식
dev 개발 중인 변경 확인 push 시 자동 배포
stg (staging) 프로덕션 배포 전 검증 dev 이후 자동 또는 수동
prod (production) 실제 서비스 수동 승인 후 배포

브랜치 전략 연결 예시

feature/* → develop 브랜치 → dev 환경 자동 배포
develop → main(또는 release) 브랜치 → stg 환경 배포
main + 태그 → prod 환경 배포 (수동 승인)

환경별 설정 분리 예시

방법 1: 환경변수로 분리
  - dev: DB_HOST=dev-db.internal
  - prod: DB_HOST=prod-db.internal

방법 2: 설정 파일 분리
  - config/dev.yaml
  - config/prod.yaml

방법 3: Helm values 분리 (K8s)
  - values-dev.yaml
  - values-prod.yaml

Helm은 helm install의 --values(-f) 옵션으로 values 파일을 지정할 수 있습니다 (https://helm.sh/docs/helm/helm_install/).

4. Secrets 관리

상세 내용

Secrets = DB 비밀번호, API 키, 인증서처럼 민감한 값

Kubernetes 문서는 Secret을 password, token, key 같은 소량의 민감한 데이터를 담는 오브젝트로 설명하고, Secret을 쓰면 애플리케이션 코드에 기밀 데이터를 넣지 않아도 된다고 설명합니다 (https://kubernetes.io/docs/concepts/configuration/secret/).

저장소에 secret을 커밋한 경우 GitHub 문서는 먼저 해당 secret을 revoke하거나 rotate하라고 안내합니다. revoke 또는 rotate한 secret은 더 이상 접근에 쓸 수 없고, 이것으로 충분할 수 있다고 설명합니다. 히스토리를 재작성해 제거하는 방법에는 재오염(recontamination) 위험 등 여러 부작용이 있다고 설명합니다 (https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository).

도구별 Secrets 관리

도구 방법
Jenkins Jenkins에 저장한 credentials를 Pipeline에서 사용. 전역, 특정 Pipeline 프로젝트, 특정 사용자 단위로 사용할 수 있음 (https://www.jenkins.io/doc/book/using/using-credentials/)
GitLab CI CI/CD variable로 등록. variable을 mask하면 job log에 값이 출력되지 않고 [MASKED]로 대체됨 (https://docs.gitlab.com/ci/variables/)
GitHub Actions repository, environment, organization 수준에서 secret 생성 (https://docs.github.com/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions)
Kubernetes Secret 오브젝트

GitHub Actions에서는 ${{ secrets.NAME }}으로 secret을 참조합니다. 워크플로가 fork된 repository에서 trigger되면 GITHUB_TOKEN을 제외한 secret은 runner로 전달되지 않습니다. 클라우드 제공자에 접근해야 하는 경우 OpenID Connect(OIDC)를 사용하면 workflow가 클라우드 제공자로부터 short-lived token을 직접 받아 자격증명을 long-lived secret으로 저장하지 않아도 됩니다 (https://docs.github.com/en/actions/concepts/security/openid-connect).

GitLab 문서는 variable masking이 악의적인 사용자의 variable 값 접근을 막는 보장된 방법은 아니라고 설명합니다 (https://docs.gitlab.com/ci/variables/).

외부 Secrets Manager

도구 특징
AWS Secrets Manager database credential, API key 등의 secret을 수명 주기 전반에 걸쳐 관리, 조회, rotate. 자동 rotation 일정을 설정할 수 있음 (https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html)
AWS Systems Manager Parameter Store standard tier는 계정·Region당 최대 10,000개 parameter, 값 최대 4 KB, 추가 요금 없음. advanced tier는 최대 100,000개, 값 최대 8 KB, 요금 부과 (https://docs.aws.amazon.com/systems-manager/latest/userguide/parameter-store-advanced-parameters.html)
HashiCorp Vault on-premises, 클라우드, 하이브리드 환경에서 privileged access와 secret을 중앙에서 관리 (https://developer.hashicorp.com/vault/docs/about-vault/what-is-vault)

5. 도구 비교 — Jenkins vs GitLab CI

상세 내용
항목 Jenkins GitLab CI/CD
파이프라인 정의 파일 Jenkinsfile .gitlab-ci.yml
작업 실행 주체 agent가 controller의 지시에 따라 작업 실행 runner
기능 확장 plugin 설치 CI/CD components 등

Jenkins glossary에서 agent는 보통 Jenkins controller에 연결되어 controller의 지시에 따라 작업을 실행하는 머신 또는 컨테이너이고, controller는 설정을 저장하고 plugin을 로드하며 UI를 제공하는 중앙 조정 프로세스입니다. built-in node는 Jenkins 2.307 이전에 “master node"로 불렸고, 같은 문서는 build를 built-in node가 아닌 agent에서 실행하길 권장합니다 (https://www.jenkins.io/doc/book/glossary/).

Jenkinsfile은 Pipeline 정의를 담는 파일이며 Declarative Pipeline과 Scripted Pipeline 두 문법을 사용합니다 (https://www.jenkins.io/doc/book/pipeline/jenkinsfile/). GitLab runner는 job을 실행하는 주체이고 GitLab 문서는 runner 구성을 https://docs.gitlab.com/ci/runners/ 에서, CI/CD component를 https://docs.gitlab.com/ci/components/ 에서 설명합니다.

6. 파이프라인 구성

상세 내용

파이프라인 실행 시간

실패 알림

빌드 환경

이미지 태그

GitLab은 predefined variable CI_COMMIT_SHA(빌드 대상 commit revision)와, 그 앞 8자인 CI_COMMIT_SHORT_SHA를 제공합니다 (https://docs.gitlab.com/ci/variables/predefined_variables/). 이 값을 이미지 태그에 쓰면 태그로 빌드된 commit을 식별할 수 있습니다.

이미지 태그 예시:
  myapp:abc123f9  (commit SHA 앞 8자, GitLab CI_COMMIT_SHORT_SHA 형식)
  myapp:v1.2.3    (릴리즈 태그)

시크릿과 이미지

7. 파이프라인 디버깅

상세 내용

디버깅 방법

관련 포스트

Git 브랜치 전략과 merge/rebase 정리 merge와 rebase 차이, fast-forward, 브랜치 전략(Git Flow, GitHub …