
이번 포스트에서는 CL 도구(Continuous Integration/Delivery) 로서의 Jenkins를 활용하여, Kubernetes 클러스터 위에서 애플리케이션을 자동 배포하고, 테스트 및 코드 품질 검사를 통해 안정적인 개발 파이프라인을 구축해보았습니다.
Jenkins는 오픈소스 기반의 CI/CD 자동화 서버입니다. 다음과 같은 특징이 있습니다:
설치는 아래처럼 진행하였습니다 (Docker 기준):
docker run -d -p 8080:8080 -p 50000:50000 --name jenkins \
-v jenkins_home:/var/jenkins_home \
jenkins/jenkins:lts
보다 유연하고 확장성 있는 CI/CD 시스템을 위해 Jenkins를 Kubernetes 클러스터 상에 Helm Chart로 배포했습니다.
helm repo add jenkins https://charts.jenkins.io
helm repo update
kubectl create namespace jenkins
helm install jenkins jenkins/jenkins -n jenkins -f values.yaml
kubectl port-forward svc/jenkins -n jenkins 8080:8080
Jenkins의 Jenkinsfile을 활용해 GitHub에서 코드를 가져오고, 테스트 및 도커 빌드 후 배포하는 자동화 파이프라인을 구성했습니다.
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git 'https://github.com/your-user/your-repo.git'
}
}
stage('Install Dependencies') {
steps {
sh 'npm install'
}
}
stage('Unit Test') {
steps {
sh 'npm test'
}
}
stage('Build Docker Image') {
steps {
sh 'docker build -t your-dockerhub-username/app-name:${BUILD_NUMBER} .'
}
}
stage('Push to Registry') {
steps {
withCredentials([usernamePassword(credentialsId: 'dockerhub', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
sh '''
echo $PASS | docker login -u $USER --password-stdin
docker push your-dockerhub-username/app-name:${BUILD_NUMBER}
'''
}
}
}
stage('Deploy to Kubernetes') {
steps {
sh 'kubectl apply -f k8s/deployment.yaml'
}
}
}
}
Node.js에서 jest 프레임워크를 활용하여 기본적인 단위 테스트를 구성합니다.
function add(a, b) {
return a + b;
}
module.exports = add;
const add = require('./add');
test('1 + 2 = 3', () => {
expect(add(1, 2)).toBe(3);
});
npm test
Jenkins 파이프라인에서 실패 시 자동으로 빌드 중단되도록 설정됩니다.
npm install --save-dev eslint
npx eslint --init
stage('Lint') {
steps {
sh 'npx eslint .'
}
}
sonar-project.properties 작성withSonarQubeEnv를 이용해 분석 가능실무에서도 위 흐름이 기본이며, 추후 GitHub Actions, GitLab CI로도 확장 가능합니다.