source: update Base rule Agent
This commit is contained in:
@@ -1,54 +1,54 @@
|
||||
---
|
||||
name: GenerateGitHubActions
|
||||
description: Hướng dẫn tạo CI/CD pipeline tự động hóa Build, Test, Docker Build & Deploy bằng GitHub Actions.
|
||||
description: Guide to generating a CI/CD pipeline automating Build, Test, Docker Build & Deploy using GitHub Actions.
|
||||
---
|
||||
|
||||
# GenerateGitHubActions Skill
|
||||
|
||||
Khi user yêu cầu tạo CI/CD Pipelines (ví dụ: GitHub Actions, hoặc tương đương cho GitLab CI / Azure DevOps), bạn cần sinh ra file luồng tự động hóa theo các bước chuẩn sau đây.
|
||||
When a user requests the creation of CI/CD Pipelines (e.g., GitHub Actions, or its equivalent for GitLab CI / Azure DevOps), you must generate an automation workflow file following standard steps below.
|
||||
|
||||
## Mục đích
|
||||
- Tự động hóa quá trình kiểm tra mã nguồn (CI) và triển khai (CD).
|
||||
- Đảm bảo code push lên nhánh `main` luôn hoạt động đúng đắn và sẵn sàng lên production.
|
||||
## Purpose
|
||||
- Automate the source code testing (CI) and deployment (CD) process.
|
||||
- Ensure any code pushed to the `main` branch always functions correctly and is ready for production.
|
||||
|
||||
## Hướng dẫn sinh cấu hình (GitHub Actions)
|
||||
## Configuration Guide (GitHub Actions)
|
||||
|
||||
Tạo file luồng công việc Workflow cho GitHub Actions.
|
||||
Create a Workflow file for GitHub Actions.
|
||||
|
||||
### 1. Đường dẫn và tên file
|
||||
- **Đường dẫn**: `.github/workflows/ci-cd.yml`
|
||||
- (Hoặc theo định dạng của platform tương ứng: `.gitlab-ci.yml` cho GitLab, `azure-pipelines.yml` cho Azure DevOps).
|
||||
### 1. File Path and Name
|
||||
- **Path**: `.github/workflows/ci-cd.yml`
|
||||
- (Or corresponding platform format: `.gitlab-ci.yml` for GitLab, `azure-pipelines.yml` for Azure DevOps).
|
||||
|
||||
### 2. Các bước cấu hình bắt buộc trong file YAML
|
||||
### 2. Mandatory Configuration Steps in the YAML File
|
||||
|
||||
Workflow cần trải qua các luồng chính sau (mẫu dưới dây cho GitHub Actions):
|
||||
The workflow needs to progress through these main flows (example below is for GitHub Actions):
|
||||
|
||||
#### Phân đoạn 1: Build & Test (CI)
|
||||
- **Triggers**: Lắng nghe sự kiện `push` hoặc `pull_request` vào nhánh `main`.
|
||||
- **Setup môi trường**:
|
||||
- Checkout mã nguồn (ví dụ dùng `actions/checkout@v4`).
|
||||
- Cài đặt .NET SDK tương ứng với dự án (ví dụ `actions/setup-dotnet@v4` cho .NET 8.0). **Lưu ý: Bật tính năng cache Nuget để tăng tốc độ build.**
|
||||
#### Phase 1: Build & Test (CI)
|
||||
- **Triggers**: Listen for `push` or `pull_request` events on the `main` branch.
|
||||
- **Environment Setup**:
|
||||
- Checkout source code (e.g., using `actions/checkout@v4`).
|
||||
- Install the .NET SDK matching the project (e.g., `actions/setup-dotnet@v4` for .NET 8.0). **Note: Enable Nuget cache to speed up the build.**
|
||||
- **Run Tests**:
|
||||
- Chạy khối lệnh `dotnet restore`, `dotnet build --no-restore`.
|
||||
- Quan trọng nhất: Chạy `dotnet test --no-build --verbosity normal`. (Chỉ khi Test Xanh (Passed) thì các bước sau mới được chạy tiếp).
|
||||
- Execute `dotnet restore`, `dotnet build --no-restore`.
|
||||
- Most critically: Run `dotnet test --no-build --verbosity normal`. (Only if Tests go Green (Passed) should subsequent steps proceed).
|
||||
|
||||
#### Phân đoạn 2: Docker Build & Push (Bắt đầu CD)
|
||||
- **Cần điều kiện**: Chỉ chạy khi Job Build & Test thành công (`needs: build`).
|
||||
- **Đăng nhập Container Registry**:
|
||||
- Login vào Docker Hub hoặc Azure Container Registry (ACR) sử dụng System Secrets (ví dụ `DOCKER_USERNAME` và `DOCKER_PASSWORD`).
|
||||
#### Phase 2: Docker Build & Push (Start of CD)
|
||||
- **Prerequisite**: Only run if the Build & Test Job was successful (`needs: build`).
|
||||
- **Log into Container Registry**:
|
||||
- Login to Docker Hub or Azure Container Registry (ACR) using System Secrets (e.g., `DOCKER_USERNAME` and `DOCKER_PASSWORD`).
|
||||
- **Build & Push Image**:
|
||||
- Build Image từ Dockerfile (chú ý chỉ đường dẫn thư mục gốc nơi chứa dự án chính để `docker build` có thể truy cập qua các tầng thư mục Clean Architecture).
|
||||
- Gắn tag cho Image (ví dụ: `latest` hoặc theo Commit SHA/phiên bản).
|
||||
- Push Image lên Registry.
|
||||
- Build the Image from the Dockerfile (ensure it points to the root directory containing the main project so `docker build` can access the multiple layers of Clean Architecture).
|
||||
- Tag the Image (e.g., `latest` or via Commit SHA/version).
|
||||
- Push the Image to the Registry.
|
||||
|
||||
#### Phân đoạn 3: Deploy to Server (CD - Webhook / SSH)
|
||||
- Dùng thư viện `appleboy/ssh-action` (hoặc tương tự) để SSH vào Server đích.
|
||||
- Yêu cầu server pull file Image mới nhất từ Registry.
|
||||
- **Quan trọng:** Ưu tiên sử dụng `docker compose` để deploy (pull và up) nếu cấu trúc dự án của user có file `docker-compose.yml`, giúp khởi động lại toàn bộ stack (API, DB, Redis...) thay vì chỉ chạy `docker run` độc lập.
|
||||
#### Phase 3: Deploy to Server (CD - Webhook / SSH)
|
||||
- Use the `appleboy/ssh-action` library (or similar) to SSH into the target Server.
|
||||
- Instruct the server to pull the latest Image file from the Registry.
|
||||
- **Important:** Prioritize using `docker compose` to deploy (pull and up) if the user's project structure includes a `docker-compose.yml` file. This helps restart the entire stack (API, DB, Redis, etc.) rather than just running a standalone `docker run`.
|
||||
|
||||
## Mẫu File Mặc Định (`ci-cd.yml`)
|
||||
## Default Template File (`ci-cd.yml`)
|
||||
|
||||
Dưới đây là khung mẫu bạn cần căn cứ để thiết kế khi sinh file cho user:
|
||||
Below is the template framework you need to base your designs on when generating files for users:
|
||||
|
||||
```yaml
|
||||
name: CI/CD Pipeline
|
||||
@@ -68,7 +68,7 @@ jobs:
|
||||
uses: actions/setup-dotnet@v4
|
||||
with:
|
||||
dotnet-version: '8.0.x'
|
||||
cache: true # Bật cache Nuget, lần sau build nhanh gấp đôi
|
||||
cache: true # Enable Nuget cache; subsequent builds will be twice as fast
|
||||
cache-dependency-path: '**/packages.lock.json'
|
||||
|
||||
- name: Restore dependencies
|
||||
@@ -100,7 +100,7 @@ jobs:
|
||||
context: .
|
||||
file: ./Dockerfile
|
||||
push: true
|
||||
tags: ${{ secrets.DOCKER_USERNAME }}/chi-tiet-ten-project-lowercase:latest
|
||||
tags: ${{ secrets.DOCKER_USERNAME }}/detailed-project-name-lowercase:latest
|
||||
|
||||
deploy:
|
||||
needs: docker-build-push
|
||||
@@ -115,12 +115,12 @@ jobs:
|
||||
username: ${{ secrets.SERVER_USER }}
|
||||
key: ${{ secrets.SERVER_SSH_KEY }}
|
||||
script: |
|
||||
cd /path/to/your/project/on/server # Trỏ tới thư mục chứa docker-compose.yml
|
||||
docker compose pull # Kéo image mới nhất về (dựa theo file compose)
|
||||
docker compose up -d --build # Khởi động lại các service có sự thay đổi
|
||||
cd /path/to/your/project/on/server # Point to the directory containing docker-compose.yml
|
||||
docker compose pull # Pull the newest image (based on the compose file)
|
||||
docker compose up -d --build # Restart any services with modifications
|
||||
```
|
||||
|
||||
## Lưu ý cho AI Agent
|
||||
- Khi User yêu cầu sinh pipeline, hãy yêu cầu User xác nhận về tên tài khoản Docker Hub, Server Credentials và nhắc họ cấu hình đầy đủ `Secrets` trên GitHub sau khi sinh file.
|
||||
- **Hãy tự động thay thế chuỗi tên project (`chi-tiet-ten-project-lowercase` trong mẫu) bằng tên thật của Project / Repository mà User đang thao tác. Chuyển tất cả về chữ thường (lowercase) khi đặt tên Docker Image để tránh lỗi định dạng của Docker.**
|
||||
- **Nếu user có dùng `docker-compose`, hãy ưu tiên sinh lệnh `docker compose up -d` thay vì `docker run` thuần.**
|
||||
## Reminders for the AI Agent
|
||||
- When a User asks to generate a pipeline, ensure you ask the User to confirm their Docker Hub account name, Server Credentials, and remind them to fully configure `Secrets` on GitHub after the file is generated.
|
||||
- **Automatically replace the project name string (`detailed-project-name-lowercase` in the template) with the true name of the Project / Repository the User is working on. Convert everything to lowercase when naming the Docker Image to prevent Docker formatting errors.**
|
||||
- **If the user utilizes `docker-compose`, prioritize generating the `docker compose up -d` command over bare `docker run` commands.**
|
||||
|
||||
Reference in New Issue
Block a user