Design pipeline - SilverSky9/DevToolNo1 GitHub Wiki
Design pipeline
ใช้ Jenkins
ที่เป็น Software (Tool) ที่ใช้ทำ CI/CD (Continuous Integration/Continuous Delivery) เพื่อให้เราสามารถที่จะผลิตและส่งมอบ Software ไปยังผู้ใช้ได้อย่างต่อเนื่อง เกิดความราบรื่น และลดต้นทุนด้านเวลาในระหว่างการพัฒนา Software โดยอาศัยหลักการของ Automation การทำทุกอย่างให้เป็นไปอย่างอัตโนมัติ ดังนี้
- การ Build Source Code
- การทดสอบ (Test) Source Code
- การ Release Source Code
- การ Deploy Source Code ไปยัง Environment ต่าง ๆ
โดยเราออกแบบการเขียน steps ต่างๆ ใน pipeline ไว้ดังนี้
- Pull code
- Download dependency
- Run unit test
- Run component test
- Deploy
- Run E2E test
โดยเราจะเขียน Pipeline ไว้บน Jenkins เลย มีข้อดีและข้อเสีย ดังนี้
ข้อดี
: เหมาะกับการทดลองเขียน Pipeline เพื่อทดสอบดูผลลัพธ์ เนื่องจากเราสามารถแก้ไข และทดสอบคำสั่งจาก Jenkins ได้ทันทีข้อเสีย
: ถ้ามีการย้าย Jenkins Server ต้องทำการเขียน Pipeline บน Jenkins Server ใหม่ทุกครั้ง เพราะไม่ได้ Save เป็น File เก็บไว้
Frontend
- ไม่สามารถรัน Component test และ E2E ได้เนื่องจากใช้ Docker
agent
- any คือ ใช้ executor ใด ๆ ก็ได้
agent {
agent any
}
Pull code
- เป็น stage ที่จะทำการ Pull code จาก GitHub branch main
stage('Pull code') {
steps {
git branch: 'main', url: 'https://github.com/SilverSky9/DevToolNo1.git'
}
}
Download dependency
- เป็น stage ที่จะทำการ Download dependency ต่างๆ ด้วยคำสั่ง
npm install
stage('Download dependency') {
steps {
dir("frontend") {
sh "pwd"
sh 'ls'
sh 'npm install'
}
}
}
Run unit test
- ทำการทดสอบแบบ Unit Test ด้วยคำสั่ง
npm run test
stage('Run unit test') {
steps {
dir("frontend"){
sh 'npm run test'
}
}
}
Backend
agent
- any คือ ใช้ executor ใด ๆ ก็ได้
agent any
tools
- เป็นการเรียกใช้ tools เพื่อนำมาใช้งาน
tools {
go 'Go 1.18'
dockerTool 'Docker-compose-for-all-student-from-team10'
}
environment
- เป็นการ config environment ของระบบ
environment {
GO114MODULE = 'on'
CGO_ENABLED = 0
GOPATH = "${JENKINS_HOME}/jobs/${JOB_NAME}/builds/${BUILD_ID}"
COMPOSE_FILE = "docker-compose.yml"
}
Pull code
- เป็น stage ที่จะทำการ Pull code จาก GitHub branch main
stage('Pull code') {
steps {
git branch: 'dev', url: 'https://github.com/SilverSky9/DevToolNo1.git'
}
}
Download dependencies
- เป็น stage ที่จะทำการ Download dependency ต่างๆ ด้วยคำสั่ง
go mod tidy
stage('Download dependencies') {
steps {
dir("backend"){
sh 'pwd'
sh 'go mod tidy'
}
}
}
Unit testing
- ทำการทดสอบแบบ Unit Test ของฝั่ง Backend ด้วยคำสั่ง
go test pin-services.go pin-services_test.go -cover
stage('Unit test'){
steps{
dir("backend"){
dir("services"){
sh 'go test pin-services.go pin-services_test.go -cover'
}
}
}
}
Deployment
- ยัง deploy ไม่ได้ เพราะไม่สามารถ run docker ได้
stage('Deployment') {
steps {
echo 'Now we skip deployment becuase we can\'t run docker'
}
}
E2E Testing
- ทำการทดสอบด้วย E2E Testing ไม่ได้ เพราะไม่สามารถ run docker ได้
stage('E2E Testing') {
steps {
echo 'Now we skip E2E because we can\'t run docker'
}
}
Notification
- ทำการแจ้งเตือนไปที่ email [email protected] เมื่อทำ CI/CD สำเร็จ
stage('Noti') {
steps {
emailext body: 'Please check job Team-10-Backend ',
subject: 'Team-10-Backend successful to CI/CD',
to: '[email protected]'
}
}