1+ #! /bin/bash
2+
3+ # AutoGPT Platform Container Build Test Script
4+ # This script tests container builds locally before CI/CD
5+
6+ set -euo pipefail
7+
8+ # Colors for output
9+ RED=' \033[0;31m'
10+ GREEN=' \033[0;32m'
11+ YELLOW=' \033[1;33m'
12+ BLUE=' \033[0;34m'
13+ NC=' \033[0m' # No Color
14+
15+ # Configuration
16+ REGISTRY=" ghcr.io"
17+ IMAGE_PREFIX=" significant-gravitas/autogpt-platform"
18+ VERSION=" test"
19+ BUILD_ARGS=" "
20+
21+ # Functions
22+ info () {
23+ echo -e " ${BLUE} [INFO]${NC} $1 "
24+ }
25+
26+ success () {
27+ echo -e " ${GREEN} [SUCCESS]${NC} $1 "
28+ }
29+
30+ warning () {
31+ echo -e " ${YELLOW} [WARNING]${NC} $1 "
32+ }
33+
34+ error () {
35+ echo -e " ${RED} [ERROR]${NC} $1 "
36+ }
37+
38+ usage () {
39+ cat << EOF
40+ AutoGPT Platform Container Build Test Script
41+
42+ Usage: $0 [OPTIONS] [COMPONENT]
43+
44+ COMPONENTS:
45+ backend Build backend container only
46+ frontend Build frontend container only
47+ all Build both containers (default)
48+
49+ OPTIONS:
50+ -r, --registry REGISTRY Container registry (default: ghcr.io)
51+ -t, --tag TAG Image tag (default: test)
52+ --no-cache Build without cache
53+ --push Push images after build
54+ -h, --help Show this help message
55+
56+ EXAMPLES:
57+ $0 # Build both containers
58+ $0 backend # Build backend only
59+ $0 --no-cache all # Build without cache
60+ $0 --push frontend # Build and push frontend
61+
62+ EOF
63+ }
64+
65+ check_docker () {
66+ if ! command -v docker & > /dev/null; then
67+ error " Docker is not installed"
68+ exit 1
69+ fi
70+
71+ if ! docker info & > /dev/null; then
72+ error " Docker daemon is not running"
73+ exit 1
74+ fi
75+
76+ success " Docker is available"
77+ }
78+
79+ build_backend () {
80+ info " Building backend container..."
81+
82+ local image_name=" $REGISTRY /$IMAGE_PREFIX -backend:$VERSION "
83+ local dockerfile=" autogpt_platform/backend/Dockerfile"
84+
85+ info " Building: $image_name "
86+ info " Dockerfile: $dockerfile "
87+ info " Context: ."
88+ info " Target: server"
89+
90+ if docker build \
91+ -t " $image_name " \
92+ -f " $dockerfile " \
93+ --target server \
94+ $BUILD_ARGS \
95+ . ; then
96+ success " Backend container built successfully: $image_name "
97+
98+ # Test the container
99+ info " Testing backend container..."
100+ if docker run --rm -d --name autogpt-backend-test " $image_name " > /dev/null; then
101+ sleep 5
102+ if docker ps | grep -q autogpt-backend-test; then
103+ success " Backend container is running"
104+ docker stop autogpt-backend-test > /dev/null
105+ else
106+ warning " Backend container started but may have issues"
107+ fi
108+ else
109+ warning " Failed to start backend container for testing"
110+ fi
111+
112+ return 0
113+ else
114+ error " Backend container build failed"
115+ return 1
116+ fi
117+ }
118+
119+ build_frontend () {
120+ info " Building frontend container..."
121+
122+ local image_name=" $REGISTRY /$IMAGE_PREFIX -frontend:$VERSION "
123+ local dockerfile=" autogpt_platform/frontend/Dockerfile"
124+
125+ info " Building: $image_name "
126+ info " Dockerfile: $dockerfile "
127+ info " Context: ."
128+ info " Target: prod"
129+
130+ if docker build \
131+ -t " $image_name " \
132+ -f " $dockerfile " \
133+ --target prod \
134+ $BUILD_ARGS \
135+ . ; then
136+ success " Frontend container built successfully: $image_name "
137+
138+ # Test the container
139+ info " Testing frontend container..."
140+ if docker run --rm -d --name autogpt-frontend-test -p 3001:3000 " $image_name " > /dev/null; then
141+ sleep 10
142+ if docker ps | grep -q autogpt-frontend-test; then
143+ if curl -s -o /dev/null -w " %{http_code}" http://localhost:3001 | grep -q " 200\|302\|404" ; then
144+ success " Frontend container is responding"
145+ else
146+ warning " Frontend container started but not responding to HTTP requests"
147+ fi
148+ docker stop autogpt-frontend-test > /dev/null
149+ else
150+ warning " Frontend container started but may have issues"
151+ fi
152+ else
153+ warning " Failed to start frontend container for testing"
154+ fi
155+
156+ return 0
157+ else
158+ error " Frontend container build failed"
159+ return 1
160+ fi
161+ }
162+
163+ push_images () {
164+ if [[ " $PUSH_IMAGES " == " true" ]]; then
165+ info " Pushing images to registry..."
166+
167+ local backend_image=" $REGISTRY /$IMAGE_PREFIX -backend:$VERSION "
168+ local frontend_image=" $REGISTRY /$IMAGE_PREFIX -frontend:$VERSION "
169+
170+ for image in " $backend_image " " $frontend_image " ; do
171+ if docker images | grep -q " $image " ; then
172+ info " Pushing $image ..."
173+ if docker push " $image " ; then
174+ success " Pushed $image "
175+ else
176+ error " Failed to push $image "
177+ fi
178+ fi
179+ done
180+ fi
181+ }
182+
183+ show_images () {
184+ info " Built images:"
185+ docker images | grep " $IMAGE_PREFIX " | grep " $VERSION "
186+ }
187+
188+ cleanup_test_containers () {
189+ # Clean up any test containers that might be left running
190+ docker ps -a | grep " autogpt-.*-test" | awk ' {print $1}' | xargs -r docker rm -f > /dev/null 2>&1 || true
191+ }
192+
193+ # Parse command line arguments
194+ COMPONENT=" all"
195+ PUSH_IMAGES=" false"
196+
197+ while [[ $# -gt 0 ]]; do
198+ case $1 in
199+ -r|--registry)
200+ REGISTRY=" $2 "
201+ shift 2
202+ ;;
203+ -t|--tag)
204+ VERSION=" $2 "
205+ shift 2
206+ ;;
207+ --no-cache)
208+ BUILD_ARGS=" $BUILD_ARGS --no-cache"
209+ shift
210+ ;;
211+ --push)
212+ PUSH_IMAGES=" true"
213+ shift
214+ ;;
215+ -h|--help)
216+ usage
217+ exit 0
218+ ;;
219+ backend|frontend|all)
220+ COMPONENT=" $1 "
221+ shift
222+ ;;
223+ * )
224+ error " Unknown option: $1 "
225+ usage
226+ exit 1
227+ ;;
228+ esac
229+ done
230+
231+ # Main execution
232+ info " AutoGPT Platform Container Build Test"
233+ info " Component: $COMPONENT "
234+ info " Registry: $REGISTRY "
235+ info " Tag: $VERSION "
236+
237+ check_docker
238+ cleanup_test_containers
239+
240+ # Build containers based on component selection
241+ case " $COMPONENT " in
242+ backend)
243+ build_backend
244+ ;;
245+ frontend)
246+ build_frontend
247+ ;;
248+ all)
249+ if build_backend && build_frontend; then
250+ success " All containers built successfully"
251+ else
252+ error " Some container builds failed"
253+ exit 1
254+ fi
255+ ;;
256+ esac
257+
258+ push_images
259+ show_images
260+ cleanup_test_containers
261+
262+ success " Build test completed successfully"
0 commit comments