0

🚀 Chapter 2 — Automation: Thiết lập CI với CodeBuild

Kiến trúc

                 Developer
                     │
                  git push
                     │
                     ▼
                ┌─────────┐
                │ GitHub  │
                └────┬────┘
                     │
                     │ Source
                     ▼
              ┌─────────────┐
              │ AWS CodeBuild│
              └──────┬──────┘
                     │
              ┌──────┴──────┐
              │             │
            Test       Docker Build
              │             │
              └──────┬──────┘
                     ▼
                  SUCCESS

Step 1 — Kiểm tra Chapter 1

Bạn đang có EC2 chạy Todo App.

SSH vào EC2:

ssh -i ./chapter1-key.pem ubuntu@<EC2_PUBLIC_IP>

Kiểm tra:

cd todo-list-app
docker compose ps

Kết quả tương tự:

NAME                    STATUS
todo-list-app-app-1     Up
todo-list-app-mysql-1   Up

Test:

curl http://localhost

Sau đó mở:

http://<EC2_PUBLIC_IP>

Todo App phải đang chạy.

Chapter 1 hiện tại của bạn có flow:

GitHub
   ↓
SSH
   ↓
EC2
   ↓
git pull
   ↓
docker compose build
   ↓
docker compose up -d

Step 2 — Tạo branch cho Chapter 2

Trên máy Mac:

cd ./todo-list-app

Kiểm tra:

git status

Tạo branch:

git checkout -b chapter-2-automation

Step 3 — Tạo buildspec.yml

Ở root project:

touch buildspec.yml

Cấu trúc:

todo-list-app/
├── compose.yaml
├── Dockerfile
├── package.json
├── yarn.lock
├── buildspec.yml
└── ...

Mở file:

nano buildspec.yml

Đầu tiên chúng ta làm CI đơn giản:

version: 0.2

phases:
  build:
    commands:
      - echo "Running tests..."
      - docker build -t todo-list-app .

Lưu file.

buildspec.yml mặc định phải nằm ở root repository nếu bạn không chỉ định location khác.


Step 4 — Push lên GitHub

git add buildspec.yml
git commit -m "add CodeBuild configuration"
git push -u origin chapter-2-automation

Kiểm tra GitHub.

Bạn sẽ thấy:

chapter-2-automation
        │
        └── buildspec.yml

Step 5 — Vào AWS CodeBuild

AWS Console:

AWS Console
    ↓
CodeBuild
    ↓
Build projects
    ↓
Create build project

Step 6 — Project configuration

Project name:

todo-list-ci

Step 7 — Source

Ở:

Source provider

Chọn:

GitHub

Chọn:

Connect using OAuth

Sau đó:

Connect to GitHub

Đăng nhập GitHub.

Cho phép AWS CodeBuild truy cập repository.


Step 8 — Chọn repository

Chọn:

Repository in my GitHub account

Repository:

dockersamples/todo-list-app

Nếu bạn đang dùng fork/personal repo thì chọn repository của bạn.

Ví dụ:

Juu-dev/todo-list-app

Step 9 — Source version

Ở:

Branch

nhập:

chapter-2-automation

Như vậy CodeBuild sẽ build branch Chapter 2.


Step 10 — Environment

Environment image:

Managed image

Operating system:

Ubuntu

Runtime:

Standard

Image:

aws/codebuild/standard:7.0

Compute:

EC2

hoặc lựa chọn compute nhỏ nhất phù hợp trong console để tiết kiệm chi phí.

AWS cung cấp các managed Docker images cho CodeBuild.


Step 11 — Bật Privileged

Đây là bước quan trọng vì chúng ta sẽ build Docker image.

Tìm:

Additional configuration

Bật:

☑ Enable this flag if you want to build Docker images

Tức:

Privileged
    ☑ Enabled

AWS yêu cầu privileged mode khi CodeBuild cần tương tác với Docker daemon để build image.


Step 12 — Service role

Chọn:

Create a new service role

Ví dụ:

codebuild-todo-list-service-role

AWS sẽ tạo IAM role cho CodeBuild.


Step 13 — Buildspec

Chọn:

Use a buildspec file

File:

buildspec.yml

Không cần nhập nội dung trực tiếp vì file đã nằm trong GitHub.


Step 14 — Logs

Bật:

CloudWatch Logs

Ví dụ log group:

/codebuild/todo-list-ci

Step 15 — Create project

Nhấn:

Create build project

Bạn sẽ có:

todo-list-ci

Step 16 — Start build lần đầu

Trong CodeBuild:

Build projects
    ↓
todo-list-ci
    ↓
Start build

Kiểm tra source:

Source
GitHub

Branch
chapter-2-automation

Nhấn:

Start build

Step 17 — Theo dõi build

CodeBuild sẽ chạy:

DOWNLOAD_SOURCE
        ↓
INSTALL
        ↓
PRE_BUILD
        ↓
BUILD
        ↓
POST_BUILD

Trong BUILD bạn sẽ thấy:

Running tests...

và:

docker build -t todo-list-app .

Cuối cùng:

BUILD SUCCEEDED

Step 18 — Kiểm tra Docker build

Trong log sẽ thấy Docker build:

[+] Building ...

và cuối cùng image được tạo:

todo-list-app

Ở Chapter 2 chưa push image lên ECR.

Chúng ta chỉ cần chứng minh:

GitHub
   ↓
CodeBuild
   ↓
Docker Build
   ↓
SUCCESS

Step 19 — Test tự động bằng cách sửa code

Bây giờ chúng ta kiểm tra Automation.

Trên Mac:

git checkout chapter-2-automation

Sửa một file application.

Ví dụ thay một text hiển thị trong app.

Sau đó:

git add .
git commit -m "update todo app"
git push

Step 20 — Start Build

Hiện tại chúng ta chưa cấu hình webhook nên tạm thời:

CodeBuild
    ↓
todo-list-ci
    ↓
Start build

CodeBuild lấy commit mới nhất từ GitHub.

Bạn sẽ thấy:

DOWNLOAD_SOURCE
       ↓
BUILD
       ↓
docker build
       ↓
SUCCEEDED

Step 21 — Thêm Test thật

Bây giờ mới nâng cấp buildspec.yml.

Trước tiên kiểm tra project hiện tại sử dụng package manager nào:

cat package.json

Và:

ls

Repo hiện tại là Node.js Todo application và có Docker setup, nên không nên tự giả định npm test nếu package scripts thực tế không có test command. ([GitHub][5])

Sau khi kiểm tra package.json, thêm test command phù hợp.

Ví dụ nếu project có:

"scripts": {
  "test": "..."
}

thì:

version: 0.2

phases:
  install:
    commands:
      - yarn install --frozen-lockfile

  build:
    commands:
      - yarn test
      - docker build -t todo-list-app .

Nếu project dùng npm thì tương ứng:

version: 0.2

phases:
  install:
    commands:
      - npm ci

  build:
    commands:
      - npm test
      - docker build -t todo-list-app .

Không dùng đoạn này ngay nếu chưa kiểm tra package.json.


Step 22 — Test Pipeline khi code lỗi

Đây là phần rất quan trọng của Chapter 2.

Cố tình tạo test fail hoặc lỗi build.

git push
    ↓
CodeBuild
    ↓
Test
    ↓
❌ FAILED

Khi test fail:

Docker Build
    ↓
KHÔNG CHẠY

Build phải kết thúc:

BUILD FAILED

Step 23 — Sửa lỗi

Sửa code:

git add .
git commit -m "fix tests"
git push

Start build lại.

Kết quả:

GitHub
   ↓
CodeBuild
   ↓
Install
   ↓
Test
   ↓
Docker Build
   ↓
BUILD SUCCEEDED

Step 24 — Bật GitHub Webhook

Sau khi manual build hoạt động ổn, mới bật automation thật.

Trong CodeBuild:

todo-list-ci
    ↓
Edit
    ↓
Source

Tìm:

Primary source webhook events

Bật:

☑ Rebuild every time a code change is pushed to this repository

hoặc cấu hình webhook tương đương trong giao diện hiện tại.

Mục tiêu:

git push
   ↓
GitHub webhook
   ↓
CodeBuild
   ↓
Build automatically

Step 25 — Test Automation hoàn chỉnh

Bây giờ không nhấn Start build nữa.

Chỉ cần:

git add .
git commit -m "test automatic build"
git push

Sau đó vào:

AWS
 ↓
CodeBuild
 ↓
todo-list-ci
 ↓
Build history

Bạn sẽ thấy build mới tự động xuất hiện.


Kết quả cuối Chapter 2

Chapter 2:

Developer
    ↓
git push
    ↓
GitHub
    ↓
Webhook
    ↓
AWS CodeBuild
    ↓
Install
    ↓
Test
    ↓
Docker Build
    ↓
SUCCESS

Đây chính là điểm chuyển từ Manual → Automation.

Chưa cần:

❌ ECR
❌ ECS
❌ SSM
❌ CodeDeploy
❌ Kubernetes
❌ ArgoCD
❌ Approval

Những thành phần đó để dành cho các Chapter sau.

Flow Chapter 2 hoàn chỉnh

                  ┌──────────────┐
                  │  Developer   │
                  └──────┬───────┘
                         │
                      git push
                         │
                         ▼
                  ┌──────────────┐
                  │    GitHub    │
                  └──────┬───────┘
                         │
                      Webhook
                         │
                         ▼
              ┌─────────────────────┐
              │     AWS CodeBuild   │
              │                     │
              │  Install            │
              │      ↓              │
              │  Test               │
              │      ↓              │
              │  Docker Build       │
              └──────────┬──────────┘
                         │
                  ┌──────▼──────┐
                  │   SUCCESS   │
                  └─────────────┘

All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí