Jenkins
CI/CD pipeline automation — build, test, deploy อัตโนมัติสำหรับ microservices
What I Can Do
- ตั้งค่า CI/CD pipelines สำหรับ microservices
- เขียน Declarative Jenkinsfile สำหรับ multi-stage builds
- Integrate กับ Docker สำหรับ build และ deploy
- ตั้งค่า webhooks สำหรับ automatic triggers จาก Git
- จัดการ credentials และ secrets อย่างปลอดภัย
Pipeline Structure
Jenkinsfile ที่ใช้ประกอบด้วย stages หลัก:
- Build —
docker buildสร้าง image จาก Dockerfile - Test —
docker runเพื่อ run test suite ใน container - Push — push image ไป container registry
- Deploy — deploy ไป production เมื่อ branch เป็น main
- Post — notify team ผ่าน Slack เมื่อ build fail
ทุก pipeline define ใน Jenkinsfile ที่อยู่ใน repository — version controlled, review ผ่าน PR ได้
Declarative vs Scripted Pipeline
- Declarative — structured syntax ด้วย
pipelineblock, อ่านง่าย, มี built-in validation ใช้สำหรับ pipelines ทั่วไป - Scripted — Groovy script เต็มรูปแบบ, ยืดหยุ่นกว่า, ใช้เมื่อ declarative ไม่พอ
- แนะนำ Declarative เป็น default
Stages & Steps
- Stage — logical grouping ของ steps เช่น Build, Test, Deploy แสดงเป็น visual blocks ใน UI
- Steps — individual commands ภายใน stage เช่น
sh,echo,git - Parallel — run stages พร้อมกันเพื่อลดเวลา
Agents & Nodes
- agent any — run บน node ที่ว่าง
- agent docker — run ใน Docker container ที่ระบุ image
- agent label — run บน node ที่มี label ตรง
- Different stages สามารถใช้ agent ต่างกันได้
Credentials Management
Jenkins เก็บ credentials อย่างปลอดภัย — ใช้ withCredentials block เพื่อ inject secrets เข้า environment variables, รองรับ username/password, SSH key, secret text, certificates ไม่เก็บ secrets ใน Jenkinsfile โดยตรง
Webhooks & Triggers
- GitHub webhook — trigger build เมื่อมี push/PR
- Poll SCM — ตรวจ changes ตาม schedule (ไม่แนะนำ ใช้ webhook แทน)
- Cron — schedule builds ตามเวลา เช่น nightly builds
- Upstream — trigger เมื่อ job อื่น build สำเร็จ
Environment Variables
ใช้ environment block define variables ที่ใช้ทั้ง pipeline หรือเฉพาะ stage — เช่น APP_ENV, VERSION จาก git tags, Docker image names
Shared Libraries
Shared libraries = reusable Groovy code ที่แชร์ข้าม pipelines — ใช้สำหรับ common functions เช่น deploy scripts, notification helpers, build utilities ลด duplication ระหว่าง Jenkinsfiles
Pipeline as Code
Jenkinsfile อยู่ใน repository ร่วมกับ source code — version controlled, review ได้ผ่าน PR, history of pipeline changes ติดไปกับ code
Blue Ocean
Blue Ocean = modern UI สำหรับ Jenkins — visual pipeline editor, better visualization ของ stages, PR integration ช่วยให้ดู pipeline status ได้ง่ายขึ้น
Artifact Management
archiveArtifacts เก็บ build outputs — เช่น binaries, test reports, Docker images ใช้ร่วมกับ container registry สำหรับ Docker images
Build Optimization
- ใช้ Docker layer caching เร่ง builds
- Parallel stages สำหรับ independent tasks
- Skip stages ด้วย
whenconditions - ใช้ shared workspace สำหรับ artifacts ข้าม stages