-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdocker_build.sh
More file actions
executable file
·120 lines (96 loc) · 4.43 KB
/
Copy pathdocker_build.sh
File metadata and controls
executable file
·120 lines (96 loc) · 4.43 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
#!/usr/bin/env bash
# Stop the script immediately if any command fails
set -e
# Get the build number from Jenkins, default to "local" if run outside Jenkins
BUILD_NUM="${BUILD_NUMBER:-local}"
# Docker image configuration
IMAGE_NAME="redpitaya-ubuntu-os-builder"
TAG="latest"
FULL_IMAGE_NAME="${IMAGE_NAME}:${TAG}"
CONTAINER_NAME="rp-builder-${BUILD_NUM}"
echo "=== [1/5] Aggressive Docker Cache & Old Image Cleanup ==="
# Stop and remove any leftover containers from previous runs
if docker ps -a -q -f name="${CONTAINER_NAME}" | grep -q .; then
echo "Found existing container ${CONTAINER_NAME}. Saving logs..."
docker logs "${CONTAINER_NAME}" > "artifacts/previous_build_${BUILD_NUM}.log" 2>/dev/null || true
echo "Stopping and removing old container..."
docker stop "${CONTAINER_NAME}" 2>/dev/null || true
docker rm -f "${CONTAINER_NAME}" 2>/dev/null || true
echo "Old container ${CONTAINER_NAME} removed."
else
echo "No existing container named ${CONTAINER_NAME}. Starting fresh."
fi
# Remove existing builder image to force a completely clean rebuild
if docker images -q "${FULL_IMAGE_NAME}" > /dev/null 2>&1; then
docker rmi -f "${FULL_IMAGE_NAME}" || true
fi
# Remove any leftover containers from previous runs
docker rm -f "${CONTAINER_NAME}" 2>/dev/null || true
# Prune all dangling builder cache fragments and unused layers on the host system
echo "Pruning dangling Docker build cache layers..."
docker builder prune -f --filter type=frontend || true
docker image prune -f || true
echo "=== [2/5] Building Docker Image with ALL Source Code Inside ==="
if [[ ! -f "Dockerfile" ]]; then
echo "Error: Dockerfile not found!"
exit 1
fi
# Build with --no-cache for a fresh build, --pull for latest base image updates
# The Dockerfile COPY instruction bakes all source code into the image
docker build --no-cache --pull -t "${FULL_IMAGE_NAME}" .
echo "=== [3/5] Preparing Clean Artifacts Directory ==="
# Clean local artifacts directory only — no source code will be shared with the container
rm -rf artifacts
mkdir -p artifacts
echo "=== [4/5] Starting Fully Isolated Build Inside Container ==="
echo "Current build number: #${BUILD_NUM}"
# Run the build in an isolated container
# Only /dev (for loop devices) and artifacts directory are mounted
# Source code is NOT mounted from host — it's already inside the image via COPY
docker run --privileged --rm \
--name "${CONTAINER_NAME}" \
-v /dev:/dev \
-v "$(pwd)/artifacts":/artifacts \
-e BUILD_NUM="${BUILD_NUM}" \
-e GIT_COMMIT="${GIT_COMMIT}" \
"${FULL_IMAGE_NAME}" /bin/bash -c "
# Verify we are running inside the Docker container, not on the host
echo '=== Isolation Verification ==='
if [ -f /.dockerenv ]; then
echo 'SUCCESS: Running inside the Docker container'
else
echo 'ERROR: Leak detected! Running on host machine instead of container!'
exit 1
fi
echo '=== Starting Red Pitaya Build ==='
echo 'Source code location: /build (baked into image, NOT mounted from host)'
ls -l /build/build.sh
# Execute the main Red Pitaya build script from inside the container
/build/build.sh
echo '=== Artifact Filtering: Extracting tar.gz and zip files ==='
# Find and copy all .tar.gz and .zip artifacts, rename with build number
find /build -maxdepth 3 -type f \( -name 'red_pitaya*.tar.gz' -o -name 'red_pitaya*.zip' \) | while read -r file; do
filename=\$(basename \"\$file\")
# Handle double extension for .tar.gz files
if [[ \"\$filename\" == *.tar.gz ]]; then
base=\"\${filename%.tar.gz}\"
ext='tar.gz'
else
base=\"\${filename%.*}\"
ext='zip'
fi
new_name=\"\${base}.\${ext}\"
echo \" Copying: \$filename -> artifacts/\$new_name\"
cp \"\$file\" \"/artifacts/\$new_name\"
done
echo '=== Build completed successfully ==='
"
echo "=== [5/5] Post-Build Cleanup ==="
# Remove the builder image to free up disk space on the Jenkins node
echo "Removing the builder image to keep the environment clean..."
docker rmi -f "${FULL_IMAGE_NAME}" || true
# Final cleanup of any residual Docker build cache
docker builder prune -f --filter type=frontend || true
echo "=== Build #${BUILD_NUM} completed successfully! ==="
echo "Saved files in artifacts/ directory:"
ls -lh artifacts/