Skip to content

Commit 1315217

Browse files
committed
Chore: Add BUILD.md and image builder
Signed-off-by: xcaspar <changjun.xcj@alibaba-inc.com>
1 parent 1862ed4 commit 1315217

6 files changed

Lines changed: 364 additions & 12 deletions

File tree

.github/workflows/ci.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ jobs:
7272
- os: darwin
7373
arch: arm64
7474
target: darwin_arm64
75-
- os: windows
76-
arch: amd64
77-
target: windows_amd64
7875

7976
steps:
8077
- name: Checkout code
@@ -104,9 +101,13 @@ jobs:
104101
- name: Build CLI for ${{ matrix.target }}
105102
run: make ${{ matrix.target }} MODULES=cli
106103

107-
- name: Run basic functionality test
104+
- name: Verify file format and structure
108105
run: |
109106
cd target/chaosblade-*-${{ matrix.target }}
110-
# 测试基本命令功能
111-
./blade version
107+
# 校验文件格式
108+
echo "Verifying file structure for ${{ matrix.target }}..."
109+
ls -la
110+
echo "Checking blade binary..."
111+
file ./blade
112+
112113

BUILD.md

Lines changed: 305 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,305 @@
1+
# ChaosBlade Build Guide
2+
3+
## Overview
4+
5+
ChaosBlade is a powerful chaos engineering platform that supports compiling various project components on Mac, Linux, or Windows platforms. This document provides detailed instructions on how to build the ChaosBlade project.
6+
7+
## Requirements
8+
9+
### System Requirements
10+
- **Git Version**: >= 1.8.5
11+
- **Go Version**: Supports Go modules
12+
- **Operating System**: macOS (Darwin), Linux, Windows
13+
14+
### Dependencies
15+
- Go compiler
16+
- Git
17+
- Make
18+
- Optional: Docker or Podman (for containerized builds)
19+
20+
## Version Management
21+
22+
### Automatic Version Detection
23+
ChaosBlade supports automatically obtaining version numbers from Git Tags:
24+
- If Git Tag exists, the version number will be automatically extracted (removing the v prefix)
25+
- If no Git Tag exists, the default version 1.7.4 or environment variable `BLADE_VERSION` will be used
26+
27+
### Manual Version Setting
28+
```bash
29+
export BLADE_VERSION=1.8.0
30+
make build
31+
```
32+
33+
## Build Targets
34+
35+
### Basic Build
36+
37+
#### 1. Current Platform Build
38+
```bash
39+
# Build CLI tool
40+
make build
41+
42+
# Build all components
43+
make build_all
44+
```
45+
46+
#### 2. Specific Platform Build
47+
```bash
48+
# Build all components for specific platform
49+
make darwin_amd64
50+
make darwin_arm64
51+
make linux_amd64
52+
make linux_arm64
53+
make windows_amd64
54+
55+
# Build specific components for specific platform
56+
make linux_amd64 MODULES=cli
57+
make linux_amd64 MODULES=cli,os,java
58+
make linux_amd64 MODULES=all
59+
```
60+
61+
### Component List
62+
63+
| Component | Description | Notes |
64+
|-----------|-------------|-------|
65+
| `cli` | Command line tool | Core CLI tool |
66+
| `os` | Operating system experiment scenarios | Basic resource experiment scenarios |
67+
| `cloud` | Cloud platform experiment scenarios | Cloud service experiment scenarios |
68+
| `middleware` | Middleware experiment scenarios | Middleware service experiment scenarios |
69+
| `java` | Java experiment scenarios | JVM-related experiment scenarios |
70+
| `cplus` | C/C++ experiment scenarios | C/C++ application experiment scenarios |
71+
| `cri` | Container runtime experiment scenarios | CRI-related experiment scenarios |
72+
| `kubernetes` | Kubernetes experiment scenarios | K8s-related experiment scenarios |
73+
| `nsexec` | Namespace executor | Linux only, supports cross-platform compilation |
74+
| `check_yaml` | Check specification files | Downloads check specification YAML files |
75+
76+
## Build Process
77+
78+
### 1. Pre-build Preparation
79+
```bash
80+
make pre_build
81+
```
82+
- Generate version information
83+
- Clean and create build directories
84+
- Set platform-specific environment variables
85+
86+
### 2. Component Build
87+
Each component has an independent build process, including:
88+
- Clone or update source code repositories
89+
- Execute platform-specific build commands
90+
- Copy build artifacts to target directories
91+
92+
### 3. Packaging
93+
After build completion, platform-specific compressed packages are automatically generated:
94+
- `chaosblade-{version}-{platform}_{arch}.tar.gz`
95+
96+
## Platform-Specific Build
97+
98+
### macOS (Darwin)
99+
```bash
100+
# AMD64 architecture
101+
make darwin_amd64
102+
103+
# ARM64 architecture (Apple Silicon)
104+
make darwin_arm64
105+
```
106+
107+
### Linux
108+
```bash
109+
# AMD64 architecture
110+
make linux_amd64
111+
112+
# ARM64 architecture
113+
make linux_arm64
114+
```
115+
116+
### Windows
117+
```bash
118+
# AMD64 architecture
119+
make windows_amd64
120+
```
121+
122+
## Containerized Build
123+
124+
### Docker Image Build
125+
```bash
126+
# Build Linux AMD64 image
127+
make build_linux_amd64_image
128+
129+
# Build Linux ARM64 image
130+
make build_linux_arm64_image
131+
```
132+
133+
### Image Push
134+
```bash
135+
# Push to container image registry
136+
make push_image
137+
```
138+
139+
## Cross-Platform Compilation
140+
141+
### nsexec Cross-Platform Compilation
142+
The nsexec component supports cross-compilation from macOS to Linux:
143+
144+
#### Automatic Compiler Detection
145+
The system automatically detects available cross-compilation toolchains:
146+
- `musl-gcc`
147+
- `/usr/local/musl/bin/musl-gcc`
148+
- `x86_64-linux-musl-gcc`
149+
- `aarch64-linux-musl-gcc`
150+
- `gcc`
151+
- `aarch64-linux-gnu-gcc`
152+
153+
#### Containerized Compilation
154+
If no suitable cross-compilation toolchain is available, containers can be used for compilation:
155+
```bash
156+
# Using Docker
157+
make nsexec CONTAINER_RUNTIME=docker
158+
159+
# Using Podman
160+
make nsexec CONTAINER_RUNTIME=podman
161+
```
162+
163+
## Build Configuration
164+
165+
### Environment Variables
166+
- `GOOS`: Target operating system
167+
- `GOARCH`: Target architecture
168+
- `BLADE_VERSION`: Version number
169+
- `CONTAINER_RUNTIME`: Container runtime (docker/podman)
170+
- `MODULES`: List of components to build
171+
172+
### Build Flags
173+
- `CGO_ENABLED=0`: Disable CGO
174+
- `GO111MODULE=on`: Enable Go modules
175+
- Static linking flags: `-ldflags="-s -w"`
176+
177+
## Build Artifacts
178+
179+
### Directory Structure
180+
```
181+
target/
182+
└── chaosblade-{version}-{platform}_{arch}/
183+
├── bin/ # Executable files
184+
├── lib/ # Library files
185+
└── yaml/ # Configuration files
186+
```
187+
188+
### File Naming
189+
- Executable files: `blade` (Linux/macOS) or `blade.exe` (Windows)
190+
- Compressed packages: `chaosblade-{version}-{platform}_{arch}.tar.gz`
191+
192+
## Common Command Examples
193+
194+
### Quick Start
195+
```bash
196+
# Build for current platform
197+
make build
198+
199+
# Build all components
200+
make build_all
201+
```
202+
203+
### Specific Platform Build
204+
```bash
205+
# Build for Linux AMD64 platform
206+
make linux_amd64
207+
208+
# Build for macOS ARM64 platform
209+
make darwin_arm64
210+
```
211+
212+
### Component Selection Build
213+
```bash
214+
# Only build CLI and OS components
215+
make linux_amd64 MODULES=cli,os
216+
217+
# Build all components
218+
make linux_amd64 MODULES=all
219+
```
220+
221+
### Container Image Build
222+
```bash
223+
# Build and push images
224+
make build_linux_amd64_image
225+
make build_linux_arm64_image
226+
make push_image
227+
```
228+
229+
## Troubleshooting
230+
231+
### Common Issues
232+
233+
#### 1. Git Version Too Low
234+
```bash
235+
# Error message
236+
ALERTMSG="please update git to >= 1.8.5"
237+
238+
# Solution
239+
# Ubuntu/Debian
240+
sudo apt-get update && sudo apt-get install git
241+
242+
# macOS
243+
brew install git
244+
```
245+
246+
#### 2. Missing Cross-Compilation Toolchain
247+
```bash
248+
# Ubuntu/Debian
249+
sudo apt-get install musl-tools gcc-aarch64-linux-gnu
250+
251+
# macOS
252+
brew install FiloSottile/musl-cross/musl-cross
253+
```
254+
255+
#### 3. Container Runtime Issues
256+
```bash
257+
# Check Docker status
258+
docker info
259+
260+
# Check Podman status
261+
podman info
262+
263+
# Manually specify container runtime
264+
make nsexec CONTAINER_RUNTIME=docker
265+
```
266+
267+
### Clean Build Artifacts
268+
```bash
269+
# Clean all build artifacts
270+
make clean
271+
```
272+
273+
## Testing
274+
275+
### Run Tests
276+
```bash
277+
# Run all tests
278+
make test
279+
```
280+
281+
### Test Coverage
282+
Tests generate coverage reports:
283+
- File: `coverage.txt`
284+
- Mode: `atomic`
285+
286+
## Help Information
287+
288+
### View Help
289+
```bash
290+
make help
291+
```
292+
293+
### Available Targets
294+
```bash
295+
# View all available make targets
296+
make -n help
297+
```
298+
299+
## Related Links
300+
301+
- [Project Homepage](https://github.com/chaosblade-io/chaosblade)
302+
- [Contributing Guide](CONTRIBUTING.md)
303+
- [Code Style Guide](docs/code_styles.md)
304+
- [Release Process](docs/release_process.md)
305+

Makefile

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,40 @@ endif
380380
fi; \
381381
cp -R $(BUILD_TARGET_CACHE)/chaosblade-operator/$(BUILD_TARGET)/chaosblade-$(BLADE_VERSION)/* $(OUTPUT_DIR)/
382382

383+
384+
#----------------------------------------------------------------------------------
385+
# build image with all components
386+
387+
build_linux_amd64_image:
388+
@echo "Building linux amd64 image..."
389+
make linux_amd64 MODULES=all
390+
$(CONTAINER_RUNTIME) buildx build \
391+
--build-arg BLADE_VERSION=${BLADE_VERSION} \
392+
--build-arg GOOS=linux \
393+
--build-arg GOARCH=amd64 \
394+
-f build/image/blade/Dockerfile \
395+
--platform=linux/amd64 \
396+
-t ghcr.io/chaosblade-io/chaosblade-tool:${BLADE_VERSION} .
397+
398+
build_linux_arm64_image:
399+
@echo "Building linux arm64 image..."
400+
make linux_arm64 MODULES=all
401+
$(CONTAINER_RUNTIME) buildx build \
402+
--build-arg BLADE_VERSION=${BLADE_VERSION} \
403+
--build-arg GOOS=linux \
404+
--build-arg GOARCH=arm64 \
405+
-f build/image/blade_arm/Dockerfile \
406+
--platform=linux/arm64 \
407+
-t ghcr.io/chaosblade-io/chaosblade-tool-arm64:${BLADE_VERSION} .
408+
409+
push_image:
410+
$(CONTAINER_RUNTIME) push ghcr.io/chaosblade-io/chaosblade-tool:${BLADE_VERSION}
411+
$(CONTAINER_RUNTIME) push ghcr.io/chaosblade-io/chaosblade-tool-arm64:${BLADE_VERSION}
412+
413+
#----------------------------------------------------------------------------------
414+
383415
test: ## Test
384-
$(GO) test -race -coverprofile=coverage.txt -covermode=atomic ./... -skip=github.com/chaosblade-io/chaosblade/cli/cmd
416+
$(GO) test -race -coverprofile=coverage.txt -covermode=atomic ./build/spec ./data ./version ./exec/jvm ./exec/os ./exec/docker ./exec/kubernetes ./exec/cri ./exec/cplus ./exec/cloud ./exec/middleware
385417

386418
# clean all build result
387419
clean: ## Clean
@@ -419,6 +451,9 @@ help:
419451
@printf ' \033[36m%-20s\033[0m %s\n' "linux_amd64" "Build for Linux AMD64"
420452
@printf ' \033[36m%-20s\033[0m %s\n' "linux_arm64" "Build for Linux ARM64"
421453
@printf ' \033[36m%-20s\033[0m %s\n' "windows_amd64" "Build for Windows AMD64"
454+
@printf ' \033[36m%-20s\033[0m %s\n' "build_linux_amd64_image" "Build Docker image for Linux AMD64"
455+
@printf ' \033[36m%-20s\033[0m %s\n' "build_linux_arm64_image" "Build Docker image for Linux ARM64"
456+
@printf ' \033[36m%-20s\033[0m %s\n' "push_image" "Push Docker images to registry"
422457
@printf ' \033[36m%-20s\033[0m %s\n' "clean" "Clean build artifacts"
423458
@printf ' \033[36m%-20s\033[0m %s\n' "test" "Run tests"
424459
@echo ''
@@ -428,6 +463,9 @@ help:
428463
@echo ' make linux_amd64 MODULES=cli,os,java # Build cli, os, java for linux_amd64'
429464
@echo ' make linux_amd64 MODULES=all # Build all components for linux_amd64'
430465
@echo ' make build_all # Build all components for current platform'
466+
@echo ' make build_linux_amd64_image # Build Docker image for Linux AMD64'
467+
@echo ' make build_linux_arm64_image # Build Docker image for Linux ARM64'
468+
@echo ' make push_image # Push Docker images to registry'
431469
@echo ''
432470
@echo 'Component list:'
433471
@echo ' cli, os, cloud, middleware, cri, cplus, java, kubernetes, nsexec, check_yaml'

0 commit comments

Comments
 (0)