---
id: cicd-azure-pipelines
title: "Azure Pipelines"
shortTitle: "Azure Pipelines"
track: cicd
trackTitle: "CI/CD"
order: 4
level: important
status: published
estimatedMinutes: 30
prerequisites:
  - cicd-gitlab
next: cicd-secrets-oidc
reviewedAgainst: 2026-10-12
sources:
  - title: "Azure Pipelines YAML schema"
    url: https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema/
  - title: "Key concepts for new Azure Pipelines users"
    url: https://learn.microsoft.com/en-us/azure/devops/pipelines/get-started/key-concepts-for-new-azure-pipelines-users
---

# Azure Pipelines

Azure Pipelines là CI trong Azure DevOps. File thường là `azure-pipelines.yml`. Cùng việc với GitHub Actions, chỉ khác tên khóa.

## Để làm gì

Bạn đọc một pipeline YAML của Azure DevOps và chỉ đúng `pool`, không gọi nhầm là `runs-on`.

## Gồm những gì

- `trigger`, `pool`, `steps`, `script`.
- Microsoft-hosted agent và self-hosted agent.
- Variable group nằm ngoài file YAML.

Classic release editor không nằm trong bài này. Pipeline mới viết YAML.

## Dùng khi nào

Tổ chức đã có Azure DevOps. Repo nằm trên Azure Repos hoặc trên GitHub, và đã nối vào một pipeline. Code nằm trên GitHub và chưa có Azure DevOps thì vẫn đi GitHub Actions, như bài trước.

Đừng giữ hai file cùng build một app mà không có lý do. Hai hệ thống, hai chỗ xem log.

## Kiến thức cốt lõi

`trigger` là nhánh nào thì chạy. `pool.vmImage: ubuntu-latest` là agent Microsoft-hosted. Self-hosted thì `pool.name` trỏ pool bạn đã cài agent.

`steps` chứa `script` hoặc `task`. `script` chạy Bash trên agent Linux. Task là bước đóng gói, ví dụ đăng nhập Azure hoặc đẩy image. Version của task ghi trong YAML, `@4` hoặc tương đương. Schema bắt version thì đừng để trống.

Biến bí mật đặt trong pipeline secret hoặc variable group, không commit. `$(secret)` trong một lệnh `script` vẫn có thể lộ nếu bạn cố in. Đừng `echo` biến đó.

Service connection là chỗ Azure DevOps giữ quyền vào subscription. Pipeline dùng tên connection, không nhét client secret vào YAML.

## Lệnh và code mẫu

`azure-pipelines.yml`:

```yaml
trigger:
  - main
pool:
  vmImage: ubuntu-latest
steps:
  - script: node -e "console.log('ok')"
    displayName: test
```

## Thực hành

1. Chỉ khóa nào tương ứng `runs-on` của GitHub Actions. Đó là `pool.vmImage`.
2. Không tạo organization Azure DevOps cho bài này.
3. Nếu công ty đã có pipeline, mở file YAML trên nhánh và đối chiếu ba khóa `trigger`, `pool`, `steps`.

## Kết quả mong đợi

Pipeline mẫu chạy một lệnh Node trên `ubuntu-latest` khi có commit lên `main`.

## Checklist

- [ ] YAML trong repo, không chỉ classic editor.
- [ ] Quyền Azure nằm ở service connection.
- [ ] Self-hosted agent được cập nhật, không dùng máy cá nhân làm agent lâu dài.

## Lỗi thường gặp

**Dán `runs-on` vào file Azure Pipelines.** Schema không có khóa đó, pipeline không chạy. Đổi thành `pool`.

**`ubuntu-latest` đổi version và job vỡ.** Microsoft đổi image gắn với nhãn đó. Pin `vmImage` cụ thể khi job phụ thuộc một tool có sẵn trên image, và nâng có chủ đích.

## Nguồn

1. [YAML schema](https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema/) — `pool`, `steps`, `script`.
2. [Key concepts](https://learn.microsoft.com/en-us/azure/devops/pipelines/get-started/key-concepts-for-new-azure-pipelines-users) — pipeline, stage, job, agent.

## Tiếp theo

[Secrets và OIDC](./06-secrets-oidc.md)
