build pipeline 은 어떻게 구축할 수 있을까?
- 일단 내 경험으로는.. 각 앱별로 독립적인 파이프라인을 둔다.
- 각 파이프라인은, 해당 앱의 변경이 트리거되거나 혹은 공통 라이브러리 등이 트리거될 때 실행이 되는거고!
- 그 외의 파이프라인 설정은 다른 것과 동일 할 것으로 보인다
- 예를 들어 모노레포 내의 하나의 앱의 파이프라인 설정은 아래와 같을 것 같다.
- 일반 레포지토리와 다른점은 해당 앱의 코드에 '변경이 있는지' 확인하는 부분이겠지...
예시 - S3 배포
pipeline {
// 깃 클론을 받는다.
stages {
stage('Git Clone') {
steps {
script {
try {
git branch: 'develop', credentialsId: 'github', url: 'https://github.com/저장소주소'
env.cloneResult=true
} catch (error) {
print(error)
env.cloneResult=false
currentBuild.result = 'FAILURE'
}
}
}
}
// 변경사항을 감지한다.
stage('Check Changes') {
steps {
script {
def paths = [
// review
'apps/myapp/**',
'env/myapp/**',
// 공통 참조
'libs/shared/**',
'libs/design-system/**',
'public/**',
'babel.config.json',
'index.d.ts',
'nx.json',
'package.json',
'tsconfig.base.json',
'workspace.json',
'yarn.lock'
].collect {
it.replaceAll(/(\*\*)$/, '*').replace('*', '.*')
}
println "ChangeSets: ${currentBuild.changeSets}"
def changeSetsEmpty = currentBuild.changeSets.isEmpty()
// 변경사항이 하나라도 있는지 확인한다.
shouldBuild = changeSetsEmpty || currentBuild.changeSets.flatten().items.any { entry ->
entry.affectedFiles.any { file ->
def filePathMatches = paths.any { path ->
def match = file.path ==~ path
if (match) {
println "Match found: File Path ${file.path} matches with Path ${path}"
} else {
println "No match: File Path ${file.path} does not match with Path ${path}"
}
return match
}
if (filePathMatches) {
println "File Path Matches found for file: ${file.path}"
}
return filePathMatches
}
}
println "Should Build: ${shouldBuild}"
}
}
}
// 빌드한다 (변경사항이 있으면)
stage('Build') {
when {
expression { return shouldBuild }
}
steps {
sh 'rm -rf dist'
sh 'yarn'
sh 'yarn build myapp dev'
echo "Running ${currentBuild.number} on ${env.JENKINS_URL}"
}
post {
success {
echo 'Build Success !'
}
failure {
echo 'Build Failure !'
}
}
stage('S3 Deploy') {
when {
expression { return shouldBuild }
}
steps{
script{
try {
sh 'aws s3 sync dist/apps/myapp/exported s3://기존위치 --delete'
} catch (error) {
print(error)
currentBuild.result = 'FAILURE'
}
}
}
post {
success {
echo "The S3 Deploy stage successfully."
}
failure {
echo "The S3 Deploy stage failed."
}
}
}
stage('Clean Up') {
steps {
sh 'rm -rf dist'
}
}
stage('Finish') {
when {
expression { return shouldBuild }
}
}
}
}