CI/CD 파이프라인에서 API 테스트 자동화하기: GitHub Actions + Jenkins 가이드
코드 커밋 후 자동으로 API 테스트를 실행하는 CI/CD 통합 방법을 GitHub Actions와 Jenkins 환경에서 상세히 알아봅니다.
Spacebar AI
2025년 12월 7일
12분

CI/CD 파이프라인에서 API 테스트 자동화하기
왜 CI/CD에서 API 테스트가 필요한가?
매 커밋마다 수동으로 API 테스트를 실행하는 것은 비효율적입니다. CI/CD 파이프라인에 API 테스트를 통합하면:
- 빠른 피드백: 문제를 조기에 발견
- 일관성: 모든 커밋에 동일한 테스트 적용
- 자동화: 사람의 개입 없이 24/7 테스트
핵심 원리: 이벤트 감지 + 명령 실행
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Git Push │ ──▶ │ CI/CD 감지 │ ──▶ │ 테스트 실행 │
└─────────────┘ └─────────────┘ └─────────────┘
- 이벤트 감지: CI/CD 플랫폼이 Git 저장소의 코드 커밋을 감지
- 테스트 실행: 감지 시 자동으로
apidog run명령 실행
GitHub Actions 설정
기본 워크플로우
# .github/workflows/api-test.yml
name: API Tests
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
api-test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install Apidog CLI
run: npm install -g apidog-cli
- name: Run API Tests
run: |
apidog run \
--access-token ${{ secrets.APIDOG_ACCESS_TOKEN }} \
--project-id ${{ secrets.APIDOG_PROJECT_ID }} \
--test-scenario-id ${{ secrets.TEST_SCENARIO_ID }}
env:
APIDOG_ACCESS_TOKEN: ${{ secrets.APIDOG_ACCESS_TOKEN }}
- name: Upload Test Report
uses: actions/upload-artifact@v4
if: always()
with:
name: api-test-report
path: ./apidog-report/
환경별 테스트
jobs:
api-test:
runs-on: ubuntu-latest
strategy:
matrix:
environment: [development, staging, production]
steps:
- name: Run Tests for ${{ matrix.environment }}
run: |
apidog run \
--environment ${{ matrix.environment }} \
--access-token ${{ secrets.APIDOG_ACCESS_TOKEN }}
Jenkins 설정
1. Generic Webhook Trigger 플러그인 설치
Jenkins 관리 → 플러그인 관리 → Generic Webhook Trigger 설치
2. 파이프라인 설정
// Jenkinsfile
pipeline {
agent any
triggers {
GenericTrigger(
genericVariables: [
[key: 'ref', value: '$.ref']
],
causeString: 'Triggered by Git push to $ref',
token: 'your-webhook-token',
printContributedVariables: true,
printPostContent: true
)
}
environment {
APIDOG_ACCESS_TOKEN = credentials('apidog-access-token')
APIDOG_PROJECT_ID = 'your-project-id'
}
stages {
stage('Setup') {
steps {
sh 'npm install -g apidog-cli'
}
}
stage('API Tests') {
steps {
sh '''
apidog run \
--access-token $APIDOG_ACCESS_TOKEN \
--project-id $APIDOG_PROJECT_ID \
--reporter junit
'''
}
}
stage('Publish Results') {
steps {
junit '**/apidog-report/*.xml'
}
}
}
post {
failure {
slackSend(
channel: '#api-alerts',
color: 'danger',
message: "API 테스트 실패: ${env.JOB_NAME} #${env.BUILD_NUMBER}"
)
}
success {
slackSend(
channel: '#api-alerts',
color: 'good',
message: "API 테스트 성공: ${env.JOB_NAME} #${env.BUILD_NUMBER}"
)
}
}
}
3. GitHub Webhook 설정
GitHub 저장소 → Settings → Webhooks:
Payload URL: https://jenkins.example.com/generic-webhook-trigger/invoke?token=your-token
Content type: application/json
Events: Push events, Pull request events
크로스 플랫폼 연동
GitHub에 코드 호스팅 + Jenkins에서 테스트 실행:
GitHub Repository
│
▼ (Webhook)
Jenkins Server
│
▼ (apidog run)
Test Execution
│
▼ (Report)
Slack/Email Notification
GitLab CI 설정
# .gitlab-ci.yml
stages:
- test
api-tests:
stage: test
image: node:20
before_script:
- npm install -g apidog-cli
script:
- apidog run
--access-token $APIDOG_ACCESS_TOKEN
--project-id $APIDOG_PROJECT_ID
artifacts:
reports:
junit: apidog-report/*.xml
when: always
only:
- main
- merge_requests
테스트 결과 리포팅
JUnit 형식 리포트
apidog run --reporter junit --output ./reports/
HTML 리포트
apidog run --reporter html --output ./reports/
Slack 알림 연동
- name: Notify Slack on Failure
if: failure()
uses: 8398a7/action-slack@v3
with:
status: failure
fields: repo,message,commit,author
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
베스트 프랙티스
1. 단계별 테스트 분리
jobs:
smoke-tests:
# 빠른 기본 테스트 (5분 이내)
integration-tests:
needs: smoke-tests
# 상세 통합 테스트
e2e-tests:
needs: integration-tests
# 전체 E2E 테스트
2. 병렬 실행
strategy:
matrix:
test-suite: [auth, users, products, orders]
3. 실패 시 재시도
- name: Run Tests with Retry
uses: nick-invision/retry@v2
with:
timeout_minutes: 10
max_attempts: 3
command: apidog run ...
4. 캐싱 활용
- name: Cache node modules
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
결론
CI/CD에 API 테스트를 통합하면 코드 품질을 자동으로 보장할 수 있습니다. GitHub Actions, Jenkins, GitLab CI 등 어떤 플랫폼을 사용하든 기본 원리는 동일합니다: 이벤트 감지 → 테스트 실행 → 결과 리포팅.