Artifact Repository: Nexus และ Artifactory บน Cloud VPS
ในยุคของการพัฒนาซอฟต์แวร์สมัยใหม่ การจัดการ Artifact (สิ่งประดิษฐ์) เป็นส่วนสำคัญของ DevOps Pipeline ไม่ว่าจะเป็น Library, Application Package, หรือ Container Image Artifact Repository ช่วยให้ทีม Development สามารถจัดเก็บ จัดการ และดึง Artifact ได้อย่างมีประสิทธิภาพ ซึ่งเป็นหัวใจของการ Build และ Deployment สมัยใหม่
Artifact Repository คืออะไร ทำไมต้องมี
Artifact Repository คือพื้นที่เก็บรักษา (Repository) สำหรับเก็บผลิตภัณฑ์ของการพัฒนา เช่น JAR, WAR, DLL, NPM Package, Docker Image ฯลฯ ที่สร้างขึ้นจากการ Build ระบบนี้ทำหน้าที่เป็น Central Store ที่มีประสิทธิภาพ โปร่งใส และปลอดภัย
เหตุผลที่ต้องใช้ Artifact Repository:
- ความเป็นส่วนกลาง (Centralization): ทีม Development ทั้งหมดสามารถเข้าถึง Artifact เวอร์ชันเดียวกันได้
- เวอร์ชัน Management: การติดตามและจัดการเวอร์ชันต่างๆ ของ Artifact ได้อย่างมีระเบียบ
- ความเร็วในการ Deploy: การดึง Artifact จากแหล่งเดียวช่วยเพิ่มความเร็วของ CI/CD Pipeline
- ความปลอดภัย (Security): ควบคุมการเข้าถึง จัดการสิทธิ์ และตรวจสอบความสมบูรณ์ของ Artifact
- ประหยัดแบนด์วิดท์: Cache ของ Artifact จากแหล่งภายนอกช่วยลดการใช้ Bandwidth
- Traceability: สามารถเห็นว่า Artifact ไหนถูกใช้ในโปรเจกต์ไหน และเมื่อไร
ประเภทของ Artifact ที่ Artifact Repository รองรับ
Artifact Repository สมัยใหม่รองรับหลากหลายประเภทของ Artifact ขึ้นอยู่กับภาษาการเขียนโปรแกรมและเทคโนโลยีที่ใช้:
- Maven (Java): JAR, WAR, POM files สำหรับโปรเจกต์ Java
- npm (JavaScript/Node.js): JavaScript Package และ Modules
- Docker: Container Images ที่พร้อมสำหรับการ Deploy
- PyPI (Python): Python Packages และ Wheels
- NuGet (.NET): Libraries และ Packages สำหรับ .NET Framework
- RubyGems (Ruby): Ruby Libraries และ Gems
- CocoaPods (Objective-C/Swift): iOS/macOS Dependencies
- Gradle (Java/Kotlin): Build Artifacts สำหรับ Gradle-based Projects
Nexus Repository Manager – คุณสมบัติและการติดตั้ง
Nexus Repository Manager จาก Sonatype เป็นหนึ่งในตัวเลือกยอดนิยมที่สุดสำหรับ Artifact Management โดยมีความสามารถที่หลากหลาย
คุณสมบัติสำคัญของ Nexus
- รองรับ Repository Types หลากหลาย (Hosted, Proxy, Group)
- มี Web UI ที่ใช้งานง่าย สำหรับการจัดการ Artifact
- Cleanup Policies สำหรับการลบ Artifact เก่า
- Security Scanning เพื่อหา Vulnerabilities
- Component Search ด้วย Advanced Filters
- Integration กับ CI/CD Tools (Jenkins, GitLab CI เป็นต้น)
- High Availability (HA) Configuration
- REST API สำหรับการ Automation
ขั้นตอนการติดตั้ง Nexus บน Cloud VPS
การติดตั้ง Nexus บน Cloud VPS ของ ผู้ให้บริการโฮสติ้ง ทำได้ง่าย ๆ ดังนี้:
1. ข้อกำหนดระบบ (System Requirements):
- RAM อย่างน้อย 2GB (แนะนำ 4GB ขึ้นไป)
- CPU อย่างน้อย 2 Core
- Storage อย่างน้อย 20GB (ขึ้นอยู่กับขนาด Artifact)
- Java 8 ขึ้นไป
2. ติดตั้ง Java:
sudo apt update sudo apt install -y openjdk-11-jdk-headless java -version
3. ดาวน์โหลดและติดตั้ง Nexus:
cd /opt sudo wget https://download.sonatype.com/nexus/3/latest-unix.tar.gz sudo tar -xzf latest-unix.tar.gz sudo mv nexus-* nexus sudo useradd -r -s /bin/false nexus sudo chown -R nexus:nexus /opt/nexus /opt/sonatype-work
4. สร้าง Systemd Service:
sudo nano /etc/systemd/system/nexus.service
เพิ่มเนื้อหาดังนี้:
[Unit] Description=Sonatype Nexus Repository Manager After=network.target [Service] Type=forking ExecStart=/opt/nexus/bin/nexus start ExecStop=/opt/nexus/bin/nexus stop User=nexus Restart=on-abort [Install] WantedBy=multi-user.target
5. เปิดใช้งาน Service:
sudo systemctl daemon-reload sudo systemctl enable nexus sudo systemctl start nexus
6. เข้าถึง Nexus Web UI:
เมื่อ Service เริ่มทำงาน สามารถเข้าถึง Nexus ได้ที่ http://your-ip:8081 Username เริ่มต้นคือ admin และหาพาสเวิร์ดจาก
sudo cat /opt/sonatype-work/nexus3/admin.password
JFrog Artifactory – คุณสมบัติและการติดตั้ง
JFrog Artifactory เป็น Enterprise Solution อีกตัวเลือกที่มีความสามารถระดับ Enterprise ด้วย Features ที่ครบครัน
คุณสมบัติสำคัญของ Artifactory
- รองรับ 30+ Package Types (Universal Repository Support)
- Federation และ Multi-Site Replication
- Advanced Security Features (Access Control, Encryption)
- Xray Integration สำหรับ Security Scanning
- Smart Artifacts Search และ Query
- Build Information สำหรับ Full Traceability
- REST API ที่ครบถ้วนสำหรับ Integration
- High Performance Caching
- Support สำหรับ Helm Charts, Terraform Modules
เปรียบเทียบ Nexus vs Artifactory
| ตัวชี้วัด | Nexus Repository Manager | JFrog Artifactory |
| ราคา | Open Source (Free) + Pro | Enterprise (Paid) |
| ประเภท Repository รองรับ | ประมาณ 20+ types | 30+ types (มากกว่า) |
| ความเร็ว Performance | ดี | ดีมาก (Optimized) |
| Security Scanning | ต้องติดตั้ง Extension เพิ่มเติม | มี Xray Built-in |
| High Availability | Support ใน Pro Version | Support ใน Pro Edition |
| Ease of Setup | ค่อนข้างง่าย | ต้องการการกำหนดค่ามากขึ้น |
| Community Support | Community ที่ใหญ่ | ส่วนใหญ่ Enterprise Support |
| Cost of Ownership | ต่ำ (Free option) | สูง (Enterprise pricing) |
การเชื่อมต่อ Artifact Repository กับ CI/CD Pipeline
Artifact Repository สามารถรวมเข้ากับ CI/CD Pipeline เพื่อให้การ Build และ Deployment ทำงานได้อัตโนมัติ
ตัวอย่างการรวม Jenkins + Nexus
Jenkinsfile สำหรับ Upload Artifact:
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean package'
}
}
stage('Upload to Nexus') {
steps {
nexusArtifactUploader(
nexusVersion: 'nexus3',
protocol: 'http',
nexusUrl: 'your-ip:8081',
groupId: 'com.example',
version: '1.0.0',
repository: 'releases',
credentialsId: 'nexus-credentials',
artifacts: [
[artifactId: 'my-app',
classifier: '',
file: 'target/my-app.jar',
type: 'jar']
]
)
}
}
}
}
ตัวอย่างการรวม GitLab CI + Artifactory
stages:
- build
- upload
build:
stage: build
script:
- npm install
- npm run build
upload_to_artifactory:
stage: upload
script:
- curl -u ${ARTIFACTORY_USER}:${ARTIFACTORY_PASSWORD}
-T dist/my-app.tar.gz
"${ARTIFACTORY_URL}/artifactory/releases/my-app-${CI_COMMIT_SHA}.tar.gz"
only:
- main
Best Practices สำหรับ Artifact Management
- ใช้ Hosted Repository สำหรับ Artifacts ของทีมเอง
- ใช้ Proxy Repository เพื่อแคช External Dependencies
- ใช้ Group Repository เพื่อรวมหลาย Repositories ให้ Clients ใช้ Endpoint เดียว
- ใช้ Semantic Versioning (MAJOR.MINOR.PATCH) สำหรับ Artifacts
- ใช้ Strong Passwords และ Role-Based Access Control (RBAC)
- ทำการ Scan Vulnerabilities โดยใช้ Security Tools
- ตั้งค่า HTTPS สำหรับการเชื่อมต่อ Repository
- ทำการ Backup Repository อย่างสม่ำเสมอ
สรุป
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการตั้งค่า Artifact Repository บน Cloud VPS โปรดติดต่อทีม Support ของ ผู้ให้บริการโฮสติ้ง ได้ที่ https://de.co.th
