이번 포스트 시리즈는 Ec2 인스턴스를 컨테이너로 구성해서 CI/CD 파이프라인을 구축할 것이다.
완성된 구성도는 아래와 같다.
테스트에 사용한 EC2 용도는 아래와 같다.
CI/CD 파이프 라인을 진행할때 Clair, Jenkins 환경의 컨테이너가 필요하며
컨테이너 정보 및 이미지는 아래와 같다
젠킨스 파일로 정의한 CI/CD 파이프 라인은 아래와 같다.
# 깃허브 코드를 가져오는 작업
stage('Checkout and Pull Codes from Github Repository'){
steps{
checkout scm
}
}
stage('Build Base Image by Docker') {
steps {
sh """
cd ${mainDir}/base-image
echo '${nexusPW}' | docker login -u '${nexusID}' --password-stdin ${nexusUrl}
echo '${nexusPW}' | docker login -u '${nexusID}' --password-stdin ${nexusProxyUrl}
docker build -t ${nexusUrl}/${repository}:${baseImageTag} .
docker push ${nexusUrl}/${repository}:${baseImageTag}
"""
}
}
stage('Scan Static Codes Quality by Jacoco and SonarQube') {
steps {
sh """
cd ${mainDir}
./gradlew jacocoTestCoverageVerification --info
./gradlew jacocoTestReport --info
./gradlew sonarqube --info
"""
}
}
stage('Clean and Build Codes by Gradle') {
steps {
sh """
cd ${mainDir}
./gradlew clean build --info
"""
}
}
stage('Build Docker Image by Jib & Push to Nexus Custom Repository') {
steps {
sh """
cd ${mainDir}
./gradlew jib -Djib.to.image=${nexusUrl}/${repository}:${appImageTag} -DsendCredentialsOverHttp=true -Djib.console='plain'
"""
}
}
stage('Build Docker Image by Jib & Push to AWS ECR Repository') {
steps {
withAWS(region:"${region}", credentials:"aws-key") {
ecrLogin()
sh """
curl -O https://amazon-ecr-credential-helper-releases.s3.us-east-2.amazonaws.com/0.4.0/linux-amd64/${ecrLoginHelper}
chmod +x ${ecrLoginHelper}
mv ${ecrLoginHelper} /usr/local/bin/
cd ${mainDir}
./gradlew jib -Djib.to.image=${ecrUrl}/${repository}:${appImageTag} -Djib.console='plain'
"""
}
}
}
stage('Scan Security CVE at Clair Scanner') {
steps {
script {
try {
jenkins_ip = sh(script: "docker inspect -f '{{ .NetworkSettings.IPAddress }}' jenkins", returnStdout: true).trim()
clair_ip = sh(script: "docker inspect -f '{{ .NetworkSettings.IPAddress }}' clair", returnStdout: true).trim()
sh """
apt update
apt install -y wget
docker pull ${nexusUrl}/${repository}:${appImageTag}
wget https://github.com/arminc/clair-scanner/releases/download/v12/clair-scanner_linux_amd64
chmod +x clair-scanner_linux_amd64
mv clair-scanner_linux_amd64 /usr/local/bin/clair-scanner
"""
sh "clair-scanner --ip ${jenkins_ip} --clair='http://${clair_ip}:6060' --log='clair.log' \
--report='report.txt' ${nexusUrl}/${repository}:${appImageTag}"
} catch (err) {
echo err.getMessage()
}
}
echo currentBuild.result
}
}
stage('Deploy Nexus Repository to AWS EC2 VM'){
steps{
sshagent(credentials : ["deploy-key"]) {
sh "ssh -o StrictHostKeyChecking=no ubuntu@${deployHost} \
'echo \"${nexusPW}" | docker login -u ${nexusID} --password-stdin ${nexusUrl}; \
docker run -d -p 81:8080 -t ${nexusUrl}/${repository}:${appImageTag};'"
}
}
}
stage('Deploy AWS ECR Repository to AWS EC2 VM'){
steps{
sshagent(credentials : ["deploy-key"]) {
sh "ssh -o StrictHostKeyChecking=no ubuntu@${deployHost} \
'aws ecr get-login-password --region ${region} | docker login --username AWS --password-stdin ${ecrUrl}/${repository}; \
docker run -d -p 82:8080 -t ${ecrUrl}/${repository}:${appImageTag};'"
}
}