Added ability build for custom nginx version for distributive build

This commit is contained in:
Alexey Berezhok
2026-06-21 01:15:00 +03:00
parent 6bebc87323
commit ffc5c20fee
7 changed files with 147 additions and 5 deletions

View File

@@ -47,6 +47,7 @@ function download_nginx() {
exit 1
fi
fi
echo "Built for nginx ${NGINX_VER}" > BUILDINFO.txt
}
COMMAND="$1"
@@ -56,6 +57,7 @@ case "$COMMAND" in
prepare)
DOCKER_IMAGE="$2"
NGINX_CUSTOM_VER="$3"
if [ -z "$DOCKER_IMAGE" ]; then
echo "Set docker image for build"
@@ -153,6 +155,9 @@ EOF
# Copy Dockerfile into tmpbuild and replace placeholder image name
cp packages/Dockerfile tmpbuild/Dockerfile
sed -i "s|^FROM image|FROM ${DOCKER_IMAGE}|g" tmpbuild/Dockerfile
if [ -n "$NGINX_CUSTOM_VER" ]; then
sed -i "s|^ENTRYPOINT \[ \"bash\", \"package_preparer.sh\", \"packageprep\" \]|ENTRYPOINT [ \"bash\", \"package_preparer.sh\", \"packageprep\", \"$NGINX_CUSTOM_VER\" ]|g" tmpbuild/Dockerfile
fi
# Build a temporary Docker image based on the provided base image
docker build -t tmpbuild_image -f tmpbuild/Dockerfile .
@@ -169,6 +174,8 @@ download)
;;
packageprep)
PKG_MGR=""
NGINX_CUSTOM_VER="$2"
# Determine package manager and install nginx
if command -v dnf >/dev/null 2>&1; then
PKG_MGR="dnf"
@@ -185,9 +192,63 @@ packageprep)
exit 1
fi
# Auto-detect nginx version if not specified by user
DETECTED_NGINX_VERSION=""
if [ -z "$NGINX_CUSTOM_VER" ] || [ "$NGINX_CUSTOM_VER" == "." ]; then
echo "Auto-detecting nginx version..."
if command -v rpm >/dev/null 2>&1; then
# RPM-based systems: detect via package manager and runtime
PKG_VERSION=$(rpm -q nginx --queryformat '%{VERSION}' 2>/dev/null)
RUNTIME_VERSION=$(nginx -V 2>&1 | grep 'version:' | head -n 1 | sed -n 's/.*nginx\/\(.*\).*/\1/p')
if [ -n "$RUNTIME_VERSION" ]; then
DETECTED_NGINX_VERSION="$RUNTIME_VERSION"
elif [ -n "$PKG_VERSION" ]; then
# Fallback to package version (strip release part)
DETECTED_NGINX_VERSION=$(echo "$PKG_VERSION" | cut -d'-' -f1)
fi
elif command -v dpkg >/dev/null 2>&1; then
# Debian-based systems
PKG_VERSION=$(dpkg-query -W -f='${Version}\n' nginx 2>/dev/null | head -n 1)
RUNTIME_VERSION=$(nginx -V 2>&1 | grep 'version:' | head -n 1 | sed -n 's/.*nginx\/\(.*\).*/\1/p')
if [ -n "$RUNTIME_VERSION" ]; then
DETECTED_NGINX_VERSION="$RUNTIME_VERSION"
elif [ -n "$PKG_VERSION" ]; then
# Handle Debian version format (e.g., "1.24.0-1ubuntu1.1" -> "1.24.0")
DETECTED_NGINX_VERSION=$(echo "$PKG_VERSION" | cut -d'-' -f1)
fi
else
echo "Error: Unsupported package manager. Unable to auto-detect nginx version."
exit 1
fi
if [ -n "$DETECTED_NGINX_VERSION" ]; then
echo "Detected nginx version: $DETECTED_NGINX_VERSION"
else
echo "Warning: Could not detect nginx version. Using generic download."
fi
else
echo "Using custom nginx version: $NGINX_CUSTOM_VER"
fi
if [ "$PKG_MGR" == "yum" -o "$PKG_MGR" == "dnf" ]; then
mkdir -p rpmbuild/BUILD rpmbuild/BUILDROOT rpmbuild/RPMS rpmbuild/SOURCES rpmbuild/SPECS rpmbuild/SRPMS
cp packages/rpm/nginx-mod-rewrite.spec rpmbuild/SPECS
# Determine nginx version to use in spec file
if [ -n "$NGINX_CUSTOM_VER" ]; then
NGINX_VER_FOR_SPEC="$NGINX_CUSTOM_VER"
elif [ -n "$DETECTED_NGINX_VERSION" ]; then
NGINX_VER_FOR_SPEC="$DETECTED_NGINX_VERSION"
fi
# Modify spec file to use detected/custom nginx version
if [ -n "$NGINX_VER_FOR_SPEC" ]; then
sed -i "s|%global nginx_version .*|%global nginx_version $NGINX_VER_FOR_SPEC|" rpmbuild/SPECS/nginx-mod-rewrite.spec
fi
cp nginx-mod-rewrite-*.tar.gz rpmbuild/SOURCES
rpmbuild --define='_topdir /app/rpmbuild' -ba rpmbuild/SPECS/nginx-mod-rewrite.spec
cp rpmbuild/RPMS/x86_64/* /app/tmpbuild
@@ -213,6 +274,16 @@ packageprep)
# Extract, stripping the top-level directory
tar --strip-components=1 -xzf "$(basename "$TARBALL")"
# Use custom nginx version if specified, otherwise use auto-detected
NGINX_VER="${NGINX_CUSTOM_VER:-${DETECTED_NGINX_VERSION:-1.24.0}}"
# Generate debian/control with correct nginx version
bash control.sh "$NGINX_VER"
if [ -n "$NGINX_CUSTOM_VER" ]; then
sed -i "s|bash package_preparer.sh download . system|bash package_preparer.sh download \"$NGINX_CUSTOM_VER\"|g" rules
fi
dpkg-buildpackage -us -uc
cp ../nginx-mod-rewrite* /app/tmpbuild