การใช้งาน Azure Repos เบื้องต้น
Azure Repos คือบริการจัดเก็บ Source Code ของ Azure DevOps ที่ให้ทีมพัฒนาสามารถใช้ระบบควบคุมเวอร์ชัน (Version Control) ได้ โดยรองรับทั้ง:
- Git (Distributed version control system)
- TFVC (Team Foundation Version Control) (Centralized version control system)
แนะนำให้เลือกใช้ Git
พูดง่ายๆ ก็คือ Azure Repos = Git repository ที่อยู่บน Azure นั่นเอง ซึ่งคุณสามารถใช้เก็บ Code, ดู History, แตก Branch และ ควบคุมคุณภาพด้วย Pull Requests ได้สะดวกมากๆ
Features หลักๆของ Azure Repos
- Unlimited private Git repos (ฟรี ไม่จำกัดจำนวน)
- Pull requests & Code review: รองรับการ Review Code ก่อน merge
- Branch policies: กำหนดเงื่อนไขการ merge เช่น ต้องผ่านการ review หรือ build ก่อน
- CI/CD Integration: เชื่อมต่อกับ Azure Pipelines ได้เลย
- Web-based editor: แก้ Code ผ่านหน้าเว็บได้ทันที
ทำงานกับ Azure Repos ด้วย VS Code
การทำงานโดยส่วนใหญ่เราจะอยู่บน VS Code เป็นหลักดังนั้นลองมาดูกันว่าเราจะเชื่อม VS Code เข้ากับ Azure Repos ได้ยังไง
สิ่งที่ต้องเตรียมก่อนเริ่ม
- ติดตั้ง Git: ดาวน์โหลดและติดตั้ง Git จาก git-scm.com
- ติดตั้ง VS Code: ดาวน์โหลดและติดตั้ง Visual Studio Code จาก code.visualstudio.com
- มี Azure DevOps Repository: สร้างหรือเข้าถึง repository ที่คุณต้องการใช้งาน
- สร้าง Personal Access Token (PAT): สำหรับการยืนยันตัวตนเมื่อเชื่อมต่อผ่าน HTTPS
ขั้นตอนการเชื่อมต่อ Azure Repos กับ VS Code
-
คัดลอก URL ของ Repository
- ไปที่หน้า Azure DevOps > Repos
- คลิกที่ปุ่ม Clone แล้วคัดลอก URL ของ repository (สามารถเลือกใช้ HTTPS หรือ SSH ได้)
-
เปิด VS Code และ Clone Repository
- เปิด VS Code
- ไปที่เมนู View > Command Palette หรือกด
Ctrl+Shift+P
- พิมพ์คำสั่ง
Git: Clone
แล้วกด Enter - วาง URL ของ repository ที่คัดลอกมา แล้วกด Enter
- เลือกโฟลเดอร์ปลายทางสำหรับเก็บโค้ดบนเครื่องของคุณ
-
Authentication(เฉพาะครั้งแรกเท่านั้น)
- หากใช้ HTTPS: ระบบจะขอให้คุณกรอก username และ Personal Access Token (PAT) แทนรหัสผ่าน
- หากใช้ SSH: เราต้อง Generate Keypair ด้วย OpenSSL และนำ Public key ขึ้นไปไว้บน Azure DevOps ด้วยการตั้งค่า SSH Key
-
Open Folder หลังจาก clone เสร็จสิ้น VS Code จะถามว่า “ต้องการเปิดโปรเจกต์ไหม” ให้คลิก Open
ทำงานร่วมกับ Azure Boards
ข้อดีของ Azure DevOps คือการมีทั้ง Azure Boards และ Azure Repos อยู่ในที่เดียวกัน เราจึงควรใช้ประโยชน์จากทั้ง 2 Services นี้อย่างเต็มที่ ซึ่งเราจะมีวิธีการเชื่อมต่อดังนี้
Commit
วิธีนี้ใช้บ่อยที่สุด โดยคุณแค่กำหนดรูปแบบของ commit message ที่ถูกต้องเพื่อให้สามารถ Link กลับไปยัง Work Item ที่ต้องการได้ เราจะได้รู้ว่า Commit นี้ทำอะไรหรือแก้ Bug ส่วนไหน
ยกตัวอย่างเช่น ถ้าคุณมี Work Item (อาจเป็นงานใหม่ หรือแก้ Bug ก็ได้) บน Azure Boards หมายเลข #123
เวลาคุณ commit โค้ดบน Azure Repos ให้เขียนแบบนี้
git commit -m "Fix login issue. #123"
ผลลัพธ์:
- ใน Azure Boards จะมีลิงก์แสดงว่า commit นี้เกี่ยวข้องกับ Task #123
- บนหน้า Work Item #123 จะเห็น commit ถูกแนบมาอัตโนมัติ
หรือเราจะใส่รูปแบบของ Commit Message ต่างๆเหล่านี้
Commit Message | ความหมาย |
---|---|
Fix bug in login page. Fixes #123 |
ปิดงาน #123 เมื่อ pull request ถูก merge |
Resolve user auth issue. Resolves #456 |
ปิดงาน #456 เมื่อ merge เสร็จ |
Working on UI enhancements. Related to #789 |
แค่เชื่อมโยง ไม่ปิดงานอัตโนมัติ |
Pull Request
เมื่อคุณสร้าง Pull Request แล้วต้องการ Link กับ Work Item บน Azure Boards ให้ทำตามขั้นตอนนี้
- ไปที่หน้า Pull Request
- ในช่อง “Linked Work Items” ให้กดปุ่ม “Add link”
- ใส่หมายเลข Task หรือค้นหาชื่องาน เช่น
#123
เวลา commit โค้ดผ่าน VS Code ก็สามารถพิมพ์ #123
เข้าไปได้เลย
สรุป
ถ้าคุณใช้ Azure Boards และ Azure Repos อยู่ใน Project เดียวกัน = เชื่อมกันอัตโนมัติ แค่คุณใส่
#workitemID
ใน commit หรือ pull request ก็เชื่อมได้เลย