본문 바로가기

개발 도구/Bitbucket

[ Bitbucket ] pipeline 설정하기

반응형

 

 

Bitbucket의 bitbucket-pipelines.yml 파일은 CI/CD 파이프라인의 실행 흐름을 정의하는 핵심 구성 파일입니다. 이 파일은 루트 디렉토리에 위치해야 하며, YAML 형식으로 작성됩니다.

아래에 Bitbucket Pipelines 설정 방법을 단계별로 설명드리겠습니다.


1. 기본 구조

image: node:18  # 사용할 Docker 이미지

pipelines:
  default:
    - step:
        name: Build and Test
        caches:
          - node
        script:
          - npm install
          - npm test

2. 주요 항목 상세 설명

1. image

  • 파이프라인에서 사용할 Docker 이미지 지정
  • Docker Hub의 이미지를 기본으로 사용
image: python:3.10

2. pipelines

  • 파이프라인 트리거를 정의
  • 종류:
    • default: 모든 브랜치에 대해 기본 실행
    • branches: 특정 브랜치에만 실행
    • pull-requests: PR 생성 시 실행
    • tags: 태그 기준 실행
    • custom: 수동 실행 (트리거 버튼)
pipelines:
  branches:
    main:
      - step:
          name: Deploy to Production
          script:
            - ./deploy.sh

3. step

  • 실제 실행 단계를 정의
  • 하나의 step은 독립된 Docker 컨테이너에서 실행
- step:
    name: Run Linter
    script:
      - pip install flake8
      - flake8 .

4. script

  • 실행할 쉘 명령어를 나열
  • 순차 실행됨
script:
  - echo "Build started"
  - make build

5. caches

  • 디펜던시 캐싱
  • 속도 향상 목적
caches:
  - pip

사전 정의된 캐시: node, pip, composer, gradle, maven, dotnetcore


6. services

  • step과 병렬로 실행할 서비스 컨테이너
  • 예: DB, Redis 등
definitions:
  services:
    postgres:
      image: postgres:13

pipelines:
  default:
    - step:
        services:
          - postgres
        script:
          - psql ...

3. 예제 모음

Node.js 프로젝트

image: node:18

pipelines:
  default:
    - step:
        caches:
          - node
        script:
          - npm ci
          - npm run build

Python + PostgreSQL 예제

image: python:3.10

definitions:
  services:
    postgres:
      image: postgres:14

pipelines:
  default:
    - step:
        services:
          - postgres
        script:
          - pip install -r requirements.txt
          - pytest

커스텀 파이프라인 (수동 실행)

pipelines:
  custom:
    manual-deploy:
      - step:
          name: Manual Deploy
          script:
            - ./deploy.sh

실행 방법: Bitbucket 웹 UI > Pipelines > Run Pipeline > manual-deploy 선택


4. 고급 옵션

조건부 실행 (tag, 브랜치 필터)

pipelines:
  branches:
    release/*:
      - step:
          script:
            - echo "Release 브랜치에서 실행"
  tags:
    v1.*:
      - step:
          script:
            - echo "v1 태그에서 실행"

병렬 실행 및 의존성 순서 지정

pipelines:
  default:
    - parallel:
        - step:
            name: Test A
            script: ...
        - step:
            name: Test B
            script: ...
    - step:
        name: Deploy
        script: ...

deployment 단계 명시

pipelines:
  branches:
    main:
      - step:
          deployment: production
          script:
            - ./deploy.sh

이 경우, Bitbucket 웹에서 배포 환경 구성이 가능하며, 승인 절차 등을 추가할 수 있습니다.


📁 파일 위치

bitbucket-pipelines.yml 파일은 프로젝트 루트 디렉토리에 위치해야 하며, 파일명이 정확해야 인식됩니다.


 

반응형