-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-build-test.sh
More file actions
executable file
·325 lines (281 loc) · 11.7 KB
/
docker-build-test.sh
File metadata and controls
executable file
·325 lines (281 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/bin/bash
# Configuration
IMAGE_NAME="calculator-app"
CONTAINER_NAME="calculator-container"
HOST_PORT="8080"
CONTAINER_PORT="80"
# Docker Hub configuration
# Set your Docker Hub username here, or it will be prompted
DOCKER_HUB_USERNAME="${DOCKER_HUB_USERNAME:-}"
DOCKER_HUB_REPO="calculator-app"
PUBLISH_TO_DOCKERHUB="${PUBLISH_TO_DOCKERHUB:-true}"
# GitHub configuration
PUSH_TO_GITHUB="${PUSH_TO_GITHUB:-true}"
# Ansible configuration
RUN_ANSIBLE="${RUN_ANSIBLE:-true}"
ANSIBLE_PLAYBOOK="deploy.yml"
ANSIBLE_INVENTORY="inventory.ini"
# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${YELLOW}=== Docker Build and Test Script ===${NC}\n"
# Check if Docker is installed and available
echo -e "${YELLOW}Checking Docker installation...${NC}"
# Add common Docker paths to PATH if not already there
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS - Docker Desktop locations
export PATH="/usr/local/bin:/opt/homebrew/bin:$PATH"
fi
if ! command -v docker &> /dev/null; then
echo -e "${RED}Error: Docker is not installed or not in PATH!${NC}"
if [[ "$OSTYPE" == "darwin"* ]]; then
echo -e "${YELLOW}Please install Docker Desktop from https://www.docker.com/products/docker-desktop${NC}"
echo -e "${YELLOW}Make sure Docker Desktop is running before executing this script.${NC}"
else
echo -e "${YELLOW}Please install Docker and ensure it's in your PATH.${NC}"
fi
exit 1
fi
# Check if Docker daemon is running
if ! docker info &> /dev/null; then
echo -e "${RED}Error: Docker daemon is not running!${NC}"
echo -e "${YELLOW}Please start Docker Desktop and try again.${NC}"
exit 1
fi
DOCKER_VERSION=$(docker --version)
echo -e "${GREEN}✓ Docker is available: $DOCKER_VERSION${NC}\n"
# Step 1: Build the Docker image
echo -e "${YELLOW}Step 1: Building Docker image...${NC}"
docker build -t $IMAGE_NAME .
if [ $? -ne 0 ]; then
echo -e "${RED}Error: Docker build failed!${NC}"
exit 1
fi
echo -e "${GREEN}✓ Docker image built successfully${NC}\n"
# Step 2: Stop and remove existing container if it exists
echo -e "${YELLOW}Step 2: Cleaning up existing container (if any)...${NC}"
docker stop $CONTAINER_NAME 2>/dev/null
docker rm $CONTAINER_NAME 2>/dev/null
echo -e "${GREEN}✓ Cleanup complete${NC}\n"
# Step 3: Run the container
echo -e "${YELLOW}Step 3: Running Docker container...${NC}"
docker run -d -p $HOST_PORT:$CONTAINER_PORT --name $CONTAINER_NAME $IMAGE_NAME
if [ $? -ne 0 ]; then
echo -e "${RED}Error: Failed to run container!${NC}"
exit 1
fi
echo -e "${GREEN}✓ Container is running on port $HOST_PORT${NC}\n"
# Wait a moment for the container to fully start
echo -e "${YELLOW}Waiting for container to start...${NC}"
sleep 3
# Step 4: Test the container with curl
echo -e "${YELLOW}Step 4: Testing container with curl...${NC}"
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:$HOST_PORT)
if [ $HTTP_CODE -eq 200 ]; then
echo -e "${GREEN}✓ HTTP Status: $HTTP_CODE (Success)${NC}"
# Also check if the HTML content is returned
if curl -s http://localhost:$HOST_PORT | grep -q "calculator\|Calculator\|display"; then
echo -e "${GREEN}✓ HTML content verified${NC}"
else
echo -e "${YELLOW}⚠ Warning: HTML content may not be as expected${NC}"
fi
else
echo -e "${RED}✗ HTTP Status: $HTTP_CODE (Failed)${NC}"
echo -e "${YELLOW}Container logs:${NC}"
docker logs $CONTAINER_NAME
docker stop $CONTAINER_NAME
docker rm $CONTAINER_NAME
exit 1
fi
echo ""
# Step 5: Stop the container
echo -e "${YELLOW}Step 5: Stopping container...${NC}"
docker stop $CONTAINER_NAME
if [ $? -ne 0 ]; then
echo -e "${RED}Error: Failed to stop container!${NC}"
exit 1
fi
echo -e "${GREEN}✓ Container stopped${NC}\n"
# Cleanup: Remove the container
echo -e "${YELLOW}Cleaning up: Removing container...${NC}"
docker rm $CONTAINER_NAME
echo -e "${GREEN}✓ Container removed${NC}\n"
# Step 6: Push to GitHub (if enabled)
COMMIT_HASH=""
GITHUB_PUSH_SUCCESS=false
if [ "$PUSH_TO_GITHUB" = "true" ]; then
echo -e "${YELLOW}Step 6: Pushing to GitHub...${NC}"
# Check if git repository exists
if [ ! -d .git ]; then
echo -e "${RED}Error: Not a git repository!${NC}"
exit 1
fi
# Check for uncommitted changes
if ! git diff-index --quiet HEAD -- 2>/dev/null; then
echo -e "${YELLOW}Warning: You have uncommitted changes.${NC}"
echo -e "${YELLOW}Do you want to commit them? (y/n)${NC}"
read -r response
if [ "$response" = "y" ] || [ "$response" = "Y" ]; then
echo -e "${YELLOW}Enter commit message (or press Enter for default):${NC}"
read -r commit_msg
if [ -z "$commit_msg" ]; then
commit_msg="Update calculator app"
fi
git add .
git commit -m "$commit_msg"
if [ $? -ne 0 ]; then
echo -e "${RED}Error: Failed to commit changes!${NC}"
exit 1
fi
echo -e "${GREEN}✓ Changes committed${NC}\n"
else
echo -e "${YELLOW}Skipping commit. Using current HEAD commit hash.${NC}\n"
fi
fi
# Get current commit hash before push
COMMIT_HASH=$(git rev-parse --short HEAD)
if [ $? -ne 0 ]; then
echo -e "${RED}Error: Failed to get commit hash!${NC}"
exit 1
fi
echo -e "${GREEN}Current commit hash: $COMMIT_HASH${NC}\n"
# Push to GitHub
echo -e "${YELLOW}Pushing to GitHub...${NC}"
git push origin $(git branch --show-current)
if [ $? -ne 0 ]; then
echo -e "${RED}Error: Failed to push to GitHub!${NC}"
echo -e "${RED}Aborting Docker Hub push.${NC}"
exit 1
fi
echo -e "${GREEN}✓ Pushed to GitHub successfully${NC}\n"
GITHUB_PUSH_SUCCESS=true
# Get the latest commit hash after push (in case a new commit was created)
COMMIT_HASH=$(git rev-parse --short HEAD)
echo -e "${GREEN}Using commit hash for Docker tag: $COMMIT_HASH${NC}\n"
else
# Still get commit hash even if not pushing to GitHub
if [ -d .git ]; then
COMMIT_HASH=$(git rev-parse --short HEAD 2>/dev/null)
if [ $? -eq 0 ]; then
echo -e "${GREEN}Using commit hash for Docker tag: $COMMIT_HASH${NC}\n"
else
COMMIT_HASH="latest"
echo -e "${YELLOW}Could not get commit hash, using 'latest' as tag${NC}\n"
fi
else
COMMIT_HASH="latest"
echo -e "${YELLOW}Not a git repository, using 'latest' as tag${NC}\n"
fi
# If GitHub push is disabled, we can still proceed to Docker Hub
GITHUB_PUSH_SUCCESS=true
fi
# Step 7: Publish to Docker Hub (if enabled and GitHub push was successful or skipped)
if [ "$PUBLISH_TO_DOCKERHUB" = "true" ] && [ "$GITHUB_PUSH_SUCCESS" = "true" ]; then
echo -e "${YELLOW}Step 7: Publishing to Docker Hub...${NC}"
# Get Docker Hub username if not set
if [ -z "$DOCKER_HUB_USERNAME" ]; then
echo -e "${YELLOW}Enter your Docker Hub username:${NC}"
read -r DOCKER_HUB_USERNAME
if [ -z "$DOCKER_HUB_USERNAME" ]; then
echo -e "${RED}Error: Docker Hub username is required!${NC}"
exit 1
fi
fi
# Use commit hash as tag, fallback to 'latest' if not available
if [ -z "$COMMIT_HASH" ]; then
COMMIT_HASH="latest"
fi
# Tag the image for Docker Hub using commit hash
DOCKER_HUB_IMAGE="$DOCKER_HUB_USERNAME/$DOCKER_HUB_REPO:$COMMIT_HASH"
DOCKER_HUB_IMAGE_LATEST="$DOCKER_HUB_USERNAME/$DOCKER_HUB_REPO:latest"
echo -e "${YELLOW}Tagging image as $DOCKER_HUB_IMAGE...${NC}"
docker tag $IMAGE_NAME $DOCKER_HUB_IMAGE
if [ $? -ne 0 ]; then
echo -e "${RED}Error: Failed to tag image!${NC}"
exit 1
fi
echo -e "${GREEN}✓ Image tagged with commit hash${NC}"
# Also tag as 'latest'
echo -e "${YELLOW}Tagging image as $DOCKER_HUB_IMAGE_LATEST...${NC}"
docker tag $IMAGE_NAME $DOCKER_HUB_IMAGE_LATEST
if [ $? -ne 0 ]; then
echo -e "${RED}Error: Failed to tag image as latest!${NC}"
exit 1
fi
echo -e "${GREEN}✓ Image tagged as latest${NC}\n"
# Check if already logged in to Docker Hub
echo -e "${YELLOW}Checking Docker Hub login status...${NC}"
# Check if Docker config exists and has auth for docker.io
if [ ! -f ~/.docker/config.json ] || ! grep -q "docker.io" ~/.docker/config.json 2>/dev/null; then
echo -e "${YELLOW}Please login to Docker Hub:${NC}"
docker login
if [ $? -ne 0 ]; then
echo -e "${RED}Error: Docker Hub login failed!${NC}"
exit 1
fi
else
echo -e "${GREEN}✓ Already logged in to Docker Hub${NC}\n"
fi
# Push the image to Docker Hub (both tags)
echo -e "${YELLOW}Pushing image to Docker Hub...${NC}"
docker push $DOCKER_HUB_IMAGE
if [ $? -ne 0 ]; then
echo -e "${RED}Error: Failed to push image to Docker Hub!${NC}"
exit 1
fi
echo -e "${GREEN}✓ Image pushed with commit hash tag${NC}"
docker push $DOCKER_HUB_IMAGE_LATEST
if [ $? -ne 0 ]; then
echo -e "${RED}Error: Failed to push 'latest' tag to Docker Hub!${NC}"
exit 1
fi
echo -e "${GREEN}✓ Image pushed with 'latest' tag${NC}"
echo -e "${GREEN} Images:${NC}"
echo -e "${GREEN} - $DOCKER_HUB_IMAGE${NC}"
echo -e "${GREEN} - $DOCKER_HUB_IMAGE_LATEST${NC}\n"
elif [ "$PUBLISH_TO_DOCKERHUB" = "true" ] && [ "$GITHUB_PUSH_SUCCESS" = "false" ]; then
echo -e "${RED}Skipping Docker Hub publish: GitHub push failed!${NC}\n"
else
echo -e "${YELLOW}Skipping Docker Hub publish (PUBLISH_TO_DOCKERHUB=false)${NC}\n"
fi
# Step 8: Run Ansible playbook (if enabled and Docker Hub push was successful)
if [ "$RUN_ANSIBLE" = "true" ] && [ "$PUBLISH_TO_DOCKERHUB" = "true" ] && [ "$GITHUB_PUSH_SUCCESS" = "true" ]; then
echo -e "${YELLOW}Step 8: Running Ansible playbook...${NC}"
# Check if Ansible is installed
if ! command -v ansible-playbook &> /dev/null; then
echo -e "${RED}Error: Ansible is not installed!${NC}"
echo -e "${YELLOW}Skipping Ansible deployment.${NC}\n"
else
# Check if playbook and inventory files exist
if [ ! -f "$ANSIBLE_PLAYBOOK" ]; then
echo -e "${RED}Error: Ansible playbook '$ANSIBLE_PLAYBOOK' not found!${NC}"
exit 1
fi
if [ ! -f "$ANSIBLE_INVENTORY" ]; then
echo -e "${RED}Error: Ansible inventory file '$ANSIBLE_INVENTORY' not found!${NC}"
exit 1
fi
# Update Docker Hub username in playbook if it was set
if [ -n "$DOCKER_HUB_USERNAME" ]; then
echo -e "${YELLOW}Running Ansible playbook with Docker Hub username: $DOCKER_HUB_USERNAME${NC}"
ansible-playbook -i $ANSIBLE_INVENTORY $ANSIBLE_PLAYBOOK -e "docker_hub_username=$DOCKER_HUB_USERNAME"
else
echo -e "${YELLOW}Running Ansible playbook...${NC}"
ansible-playbook -i $ANSIBLE_INVENTORY $ANSIBLE_PLAYBOOK
fi
if [ $? -ne 0 ]; then
echo -e "${RED}Error: Ansible playbook failed!${NC}"
exit 1
fi
echo -e "${GREEN}✓ Ansible deployment completed successfully${NC}\n"
fi
elif [ "$RUN_ANSIBLE" = "true" ] && [ "$PUBLISH_TO_DOCKERHUB" != "true" ]; then
echo -e "${YELLOW}Skipping Ansible deployment: Docker Hub push was not performed${NC}\n"
elif [ "$RUN_ANSIBLE" = "true" ] && [ "$GITHUB_PUSH_SUCCESS" = "false" ]; then
echo -e "${YELLOW}Skipping Ansible deployment: GitHub push failed${NC}\n"
elif [ "$RUN_ANSIBLE" = "false" ]; then
echo -e "${YELLOW}Skipping Ansible deployment (RUN_ANSIBLE=false)${NC}\n"
fi
echo -e "${GREEN}=== All steps completed successfully! ===${NC}"