---
id: cicd-cloud-build
title: "Cloud Build"
shortTitle: "Cloud Build"
track: cicd
trackTitle: "CI/CD"
order: 7
level: normal
status: published
estimatedMinutes: 25
prerequisites:
  - cicd-circleci
next: cicd-teamcity
reviewedAgainst: 2026-10-12
sources:
  - title: "Cloud Build configuration"
    url: https://cloud.google.com/build/docs/build-config-file-schema
  - title: "Build configuration file"
    url: https://cloud.google.com/build/docs/configuring-builds/create-basic-configuration
---

# Cloud Build

Cloud Build là dịch vụ build trên Google Cloud. File thường là `cloudbuild.yaml`. Mỗi bước là một container.

## Để làm gì

Bạn đọc một file build hai bước và biết bước sau nhìn thấy file ở `/workspace` nếu cùng worker.

## Gồm những gì

- `steps`, `name` là image của bước, `args`.
- Volume `/workspace`.
- Trigger gắn repo: chỉ cần biết nó tồn tại. Bài này không tạo trigger tính phí.

Lab không đẩy image lên Artifact Registry.

## Dùng khi nào

Tổ chức đang build trên GCP thì mới đụng Cloud Build. Đừng mở thêm Cloud Build bên cạnh GitHub Actions.

Đừng để key JSON của service account trong repo để `gcloud` trong bước build. Trigger nên chạy bằng service account của Cloud Build, quyền hẹp trên project đó.

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

Mỗi phần tử trong `steps` có `name`, là image chứa công cụ, và `args` là lệnh. Bước không kế thừa shell của bước trước. Chúng dùng chung thư mục `/workspace`, là snapshot mã nguồn. Ghi file ra đó thì bước sau đọc được.

`images` ở cuối file là danh sách image Cloud Build sẽ push, nếu bước build đã tag đúng tên. Thiếu quyền push thì build fail ở cuối, dù test đã xanh.

Timeout của build và timeout của bước là hai cấu hình. Bước treo thì cả build chiếm worker đến khi hết hạn.

## Lệnh và code mẫu

`cloudbuild.yaml`:

```yaml
steps:
  - name: node:24
    entrypoint: node
    args: ["-e", "console.log('ok')"]
  - name: gcr.io/cloud-builders/gcloud
    args: ["version"]
```

Không chạy `gcloud builds submit`. Lệnh đó gửi source lên project và có thể tốn phí.

## Thực hành

1. Chỉ image của bước thứ nhất.
2. Giải thích vì sao bước hai không tự có `node` trong PATH.
3. Không submit build.

## Kết quả mong đợi

Bước một dùng `node:24`. Bước hai là container `gcloud` khác, chỉ thấy file nếu bước một ghi vào `/workspace`.

## Checklist

- [ ] Không có JSON key trong repo.
- [ ] Service account của build chỉ có quyền nó cần.
- [ ] Không `gcloud builds submit` từ máy học lên project thật.

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

**`args: ["npm", "test"]` với image không có npm trong entrypoint mặc định.** Image `node` dùng entrypoint `docker-entrypoint.sh`. Khi lệch, đặt `entrypoint: npm` và `args: ["test"]`, đúng như docs bước build.

**Tưởng bước sau giữ biến `export` của bước trước.** Mỗi bước một container. Ghi file hoặc dùng `env` của bước.

## Nguồn

1. [Build config file schema](https://cloud.google.com/build/docs/build-config-file-schema) — `steps`, `images`, timeout.
2. [Create a basic configuration](https://cloud.google.com/build/docs/configuring-builds/create-basic-configuration) — mẫu YAML.

## Tiếp theo

[TeamCity](./09-teamcity.md)
