Skip to content

Commit 588eb39

Browse files
committed
fix: added caching in erpnext to build cache based on img
1 parent 9850d68 commit 588eb39

File tree

1 file changed

+101
-99
lines changed

1 file changed

+101
-99
lines changed

imageroot/actions/configure-module/30build_docker_image

Lines changed: 101 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -10,53 +10,53 @@ NC='\033[0m' # No Color
1010

1111
# Function to print colored output
1212
print_info() {
13-
echo -e "${GREEN}[INFO]${NC} $1"
13+
echo -e "${GREEN}[INFO]${NC} $1"
1414
}
1515

1616
print_warning() {
17-
echo -e "${YELLOW}[WARNING]${NC} $1"
17+
echo -e "${YELLOW}[WARNING]${NC} $1"
1818
}
1919

2020
print_error() {
21-
echo -e "${RED}[ERROR]${NC} $1"
21+
echo -e "${RED}[ERROR]${NC} $1"
2222
}
2323

2424
# Check if podman is installed
2525
if ! command -v podman &>/dev/null; then
26-
print_error "podman is required but not installed. Please install podman first."
27-
exit 1
26+
print_error "podman is required but not installed. Please install podman first."
27+
exit 1
2828
fi
2929

3030
# Check if jq is installed
3131
if ! command -v jq &>/dev/null; then
32-
print_error "jq is required but not installed. Please install jq first."
33-
exit 1
32+
print_error "jq is required but not installed. Please install jq first."
33+
exit 1
3434
fi
3535

3636
# Function to fetch latest ERPNext tag from GitHub based on FRAPPE_VERSION
3737
fetch_erpnext_tag() {
38-
local major_version="${FRAPPE_VERSION##version-}"
39-
local api_url="https://api.github.com/repos/frappe/erpnext/tags"
40-
41-
local tags_json
42-
if ! tags_json=$(curl -fsSL "$api_url" 2>/dev/null); then
43-
print_warning "Failed to fetch tags from GitHub, using default"
44-
return 1
45-
fi
46-
47-
local first_tag
48-
if ! first_tag=$(echo "$tags_json" | jq -r ".[] | select(.name | startswith(\"v${major_version}.\")) | .name" 2>/dev/null | head -1); then
49-
print_warning "Failed to parse tags, using default"
50-
return 1
51-
fi
52-
53-
if [ -z "$first_tag" ] || [ "$first_tag" = "null" ]; then
54-
print_warning "No v${major_version} tags found, using default"
55-
return 1
56-
fi
57-
58-
echo "$first_tag"
59-
return 0
38+
local major_version="${FRAPPE_VERSION##version-}"
39+
local api_url="https://api.github.com/repos/frappe/erpnext/tags"
40+
41+
local tags_json
42+
if ! tags_json=$(curl -fsSL "$api_url" 2>/dev/null); then
43+
print_warning "Failed to fetch tags from GitHub, using default"
44+
return 1
45+
fi
46+
47+
local first_tag
48+
if ! first_tag=$(echo "$tags_json" | jq -r ".[] | select(.name | startswith(\"v${major_version}.\")) | .name" 2>/dev/null | head -1); then
49+
print_warning "Failed to parse tags, using default"
50+
return 1
51+
fi
52+
53+
if [ -z "$first_tag" ] || [ "$first_tag" = "null" ]; then
54+
print_warning "No v${major_version} tags found, using default"
55+
return 1
56+
fi
57+
58+
echo "$first_tag"
59+
return 0
6060
}
6161

6262
# Cache file for app_json hash
@@ -65,32 +65,34 @@ CURRENT_APPS_JSON="$APPS_JSON"
6565

6666
# Function to compute hash of the apps JSON
6767
compute_apps_hash() {
68-
echo -n "$1" | sha256sum | cut -d' ' -f1
68+
echo -n "$1" | sha256sum | cut -d' ' -f1
6969
}
70+
# Fetch latest ERPNext tag from GitHub based on FRAPPE_VERSION
71+
ERPNEXT_TAG=$(fetch_erpnext_tag) || true
7072

73+
if [ -z "${ERPNEXT_TAG:-}" ]; then
74+
print_warning "Could not fetch ERPNext tag, using fallback"
75+
ERPNEXT_TAG="v${FRAPPE_VERSION##version-}.0.0"
76+
fi
77+
BUILD_FINGERPRINT=$(printf "%s\n%s" \
78+
"$CURRENT_APPS_JSON" \
79+
"$ERPNEXT_TAG")
7180
# Get current apps JSON hash
72-
CURRENT_HASH=$(compute_apps_hash "$CURRENT_APPS_JSON")
81+
CURRENT_HASH=$(compute_apps_hash "$BUILD_FINGERPRINT")
7382

7483
# Check if cache exists and compare
7584
if [ -f "$CACHE_FILE" ]; then
76-
CACHED_HASH=$(cat "$CACHE_FILE")
77-
if [ "$CURRENT_HASH" = "$CACHED_HASH" ]; then
78-
print_warning "App configuration unchanged. Skipping Docker image build."
79-
print_info "To force a rebuild, delete the cache file: rm $CACHE_FILE"
80-
exit 0
81-
else
82-
print_info "App configuration changed. Proceeding with Docker image build..."
83-
fi
85+
CACHED_HASH=$(cat "$CACHE_FILE")
86+
if [ "$CURRENT_HASH" = "$CACHED_HASH" ]; then
87+
print_warning "App configuration unchanged. Skipping Docker image build."
88+
print_info "To force a rebuild, delete the cache file: rm $CACHE_FILE"
89+
exit 0
90+
else
91+
rm $CACHE_FILE
92+
print_info "App configuration changed. Proceeding with Docker image build..."
93+
fi
8494
else
85-
print_info "No cache found. Building Docker image..."
86-
fi
87-
88-
# Fetch latest ERPNext tag from GitHub based on FRAPPE_VERSION
89-
ERPNEXT_TAG=$(fetch_erpnext_tag) || true
90-
91-
if [ -z "${ERPNEXT_TAG:-}" ]; then
92-
print_warning "Could not fetch ERPNext tag, using fallback"
93-
ERPNEXT_TAG="v${FRAPPE_VERSION##version-}.0.0"
95+
print_info "No cache found. Building Docker image..."
9496
fi
9597

9698
print_info "Using ERPNext tag: $ERPNEXT_TAG"
@@ -121,58 +123,58 @@ print_info "Building custom ERPNext image with Podman..."
121123

122124
# Build the image with Podman
123125
podman build --network=host \
124-
--jobs=4 \
125-
--build-arg APPS_JSON_BASE64="$APPS_JSON" \
126-
--build-arg FRAPPE_BRANCH="$FRAPPE_BRANCH" \
127-
-t "$CUSTOM_IMAGE_NAME:$CUSTOM_IMAGE_TAG" \
128-
-f ../actions/configure-module/Dockerfile \
129-
.
126+
--jobs=4 \
127+
--build-arg APPS_JSON_BASE64="$APPS_JSON" \
128+
--build-arg FRAPPE_BRANCH="$FRAPPE_BRANCH" \
129+
-t "$CUSTOM_IMAGE_NAME:$CUSTOM_IMAGE_TAG" \
130+
-f ../actions/configure-module/Dockerfile \
131+
.
130132

131133
if [ $? -eq 0 ]; then
132-
print_info "Build completed successfully!"
133-
print_info "Image tagged as: $CUSTOM_IMAGE_NAME:$CUSTOM_IMAGE_TAG"
134-
135-
# Also tag as latest
136-
podman tag "$CUSTOM_IMAGE_NAME:$CUSTOM_IMAGE_TAG" "$CUSTOM_IMAGE_NAME:latest"
137-
138-
# Tag with the same name as base image (for drop-in replacement)
139-
podman tag "$CUSTOM_IMAGE_NAME:$CUSTOM_IMAGE_TAG" "$BASE_IMAGE"
140-
print_info "Also tagged as: $BASE_IMAGE (drop-in replacement)"
141-
142-
echo ""
143-
print_info "To push the image to a registry:"
144-
echo " podman push $CUSTOM_IMAGE_NAME:$CUSTOM_IMAGE_TAG"
145-
echo " podman push $CUSTOM_IMAGE_NAME:latest"
146-
echo " podman push $BASE_IMAGE"
147-
148-
echo ""
149-
print_info "Image size:"
150-
podman images "$CUSTOM_IMAGE_NAME:$CUSTOM_IMAGE_TAG" --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
151-
ENV_FILE="environment"
152-
153-
# Ensure env file exists
154-
[ -f "$ENV_FILE" ] || {
155-
print_error "Env file not found"
156-
exit 1
157-
}
158-
159-
CUSTOM_IMAGE_FULL="$CUSTOM_IMAGE_NAME:$CUSTOM_IMAGE_TAG"
160-
161-
# Update or add ERPNEXT_IMAGE
162-
if grep -q '^ERPNEXT_IMAGE=' "$ENV_FILE"; then
163-
sed -i "s|^ERPNEXT_IMAGE=.*|ERPNEXT_IMAGE=$CUSTOM_IMAGE_FULL|" "$ENV_FILE"
164-
else
165-
echo "ERPNEXT_IMAGE=$CUSTOM_IMAGE_FULL" >>"$ENV_FILE"
166-
fi
167-
168-
print_info "Updated ERPNEXT_IMAGE in $ENV_FILE to:"
169-
print_info "$CUSTOM_IMAGE_FULL" print_info "To use this image, it's already tagged as: $BASE_IMAGE"
170-
print_info "Your existing docker-compose or deployment will automatically use the custom image!"
171-
172-
# Update the cache with the new hash
173-
echo "$CURRENT_HASH" >"$CACHE_FILE"
174-
print_info "Cache updated."
134+
print_info "Build completed successfully!"
135+
print_info "Image tagged as: $CUSTOM_IMAGE_NAME:$CUSTOM_IMAGE_TAG"
136+
137+
# Also tag as latest
138+
podman tag "$CUSTOM_IMAGE_NAME:$CUSTOM_IMAGE_TAG" "$CUSTOM_IMAGE_NAME:latest"
139+
140+
# Tag with the same name as base image (for drop-in replacement)
141+
podman tag "$CUSTOM_IMAGE_NAME:$CUSTOM_IMAGE_TAG" "$BASE_IMAGE"
142+
print_info "Also tagged as: $BASE_IMAGE (drop-in replacement)"
143+
144+
echo ""
145+
print_info "To push the image to a registry:"
146+
echo " podman push $CUSTOM_IMAGE_NAME:$CUSTOM_IMAGE_TAG"
147+
echo " podman push $CUSTOM_IMAGE_NAME:latest"
148+
echo " podman push $BASE_IMAGE"
149+
150+
echo ""
151+
print_info "Image size:"
152+
podman images "$CUSTOM_IMAGE_NAME:$CUSTOM_IMAGE_TAG" --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
153+
ENV_FILE="environment"
154+
155+
# Ensure env file exists
156+
[ -f "$ENV_FILE" ] || {
157+
print_error "Env file not found"
158+
exit 1
159+
}
160+
161+
CUSTOM_IMAGE_FULL="$CUSTOM_IMAGE_NAME:$CUSTOM_IMAGE_TAG"
162+
163+
# Update or add ERPNEXT_IMAGE
164+
if grep -q '^ERPNEXT_IMAGE=' "$ENV_FILE"; then
165+
sed -i "s|^ERPNEXT_IMAGE=.*|ERPNEXT_IMAGE=$CUSTOM_IMAGE_FULL|" "$ENV_FILE"
166+
else
167+
echo "ERPNEXT_IMAGE=$CUSTOM_IMAGE_FULL" >>"$ENV_FILE"
168+
fi
169+
170+
print_info "Updated ERPNEXT_IMAGE in $ENV_FILE to:"
171+
print_info "$CUSTOM_IMAGE_FULL" print_info "To use this image, it's already tagged as: $BASE_IMAGE"
172+
print_info "Your existing docker-compose or deployment will automatically use the custom image!"
173+
174+
# Update the cache with the new hash
175+
echo "$CURRENT_HASH" >"$CACHE_FILE"
176+
print_info "Cache updated."
175177
else
176-
print_error "Build failed!"
177-
exit 1
178+
print_error "Build failed!"
179+
exit 1
178180
fi

0 commit comments

Comments
 (0)