forked from cerc-io/stack-orchestrator
nextjs-snowball
This commit is contained in:
parent
fcf2584a3a
commit
f982b7fd33
@ -0,0 +1,51 @@
|
||||
# Originally from: https://github.com/devcontainers/images/blob/main/src/javascript-node/.devcontainer/Dockerfile
|
||||
# [Choice] Node.js version (use -bullseye variants on local arm64/Apple Silicon): 18, 16, 14, 18-bullseye, 16-bullseye, 14-bullseye, 18-buster, 16-buster, 14-buster
|
||||
ARG VARIANT=20-bullseye-slim
|
||||
FROM node:${VARIANT}
|
||||
|
||||
ARG USERNAME=node
|
||||
ARG NPM_GLOBAL=/usr/local/share/npm-global
|
||||
|
||||
# Add NPM global to PATH.
|
||||
ENV PATH=${NPM_GLOBAL}/bin:${PATH}
|
||||
# Prevents npm from printing version warnings
|
||||
ENV NPM_CONFIG_UPDATE_NOTIFIER=false
|
||||
|
||||
RUN \
|
||||
# Configure global npm install location, use group to adapt to UID/GID changes
|
||||
if ! cat /etc/group | grep -e "^npm:" > /dev/null 2>&1; then groupadd -r npm; fi \
|
||||
&& usermod -a -G npm ${USERNAME} \
|
||||
&& umask 0002 \
|
||||
&& mkdir -p ${NPM_GLOBAL} \
|
||||
&& touch /usr/local/etc/npmrc \
|
||||
&& chown ${USERNAME}:npm ${NPM_GLOBAL} /usr/local/etc/npmrc \
|
||||
&& chmod g+s ${NPM_GLOBAL} \
|
||||
&& npm config -g set prefix ${NPM_GLOBAL} \
|
||||
&& su ${USERNAME} -c "npm config -g set prefix ${NPM_GLOBAL}" \
|
||||
# Install eslint
|
||||
&& su ${USERNAME} -c "umask 0002 && npm install -g eslint" \
|
||||
# Install semver
|
||||
&& su ${USERNAME} -c "umask 0002 && npm install -g semver" \
|
||||
# Install pnpm
|
||||
&& su ${USERNAME} -c "umask 0002 && npm install -g pnpm" \
|
||||
# Install bun
|
||||
&& su ${USERNAME} -c "umask 0002 && npm install -g bun@1.1.x" \
|
||||
&& npm cache clean --force > /dev/null 2>&1
|
||||
|
||||
# [Optional] Uncomment this section to install additional OS packages.
|
||||
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
|
||||
&& apt-get -y install --no-install-recommends jq gettext-base procps
|
||||
|
||||
# [Optional] Uncomment if you want to install more global node modules
|
||||
# RUN su node -c "npm install -g <your-package-list-here>"
|
||||
|
||||
# We do this to get a yq binary from the published container, for the correct architecture we're building here
|
||||
COPY --from=docker.io/mikefarah/yq:latest /usr/bin/yq /usr/local/bin/yq
|
||||
|
||||
# Expose port for http
|
||||
EXPOSE 80
|
||||
|
||||
COPY /scripts /scripts
|
||||
|
||||
# Default command sleeps forever so docker doesn't kill it
|
||||
ENTRYPOINT ["/scripts/start-serving-app.sh"]
|
@ -0,0 +1,9 @@
|
||||
FROM cerc/nextjs-base:local
|
||||
|
||||
ARG CERC_NEXT_VERSION=keep
|
||||
ARG CERC_BUILD_TOOL
|
||||
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
RUN rm -rf node_modules build .next*
|
||||
RUN /scripts/build-app.sh /app
|
35
stack_orchestrator/data/container-build/cerc-nextjs-snowball/build.sh
Executable file
35
stack_orchestrator/data/container-build/cerc-nextjs-snowball/build.sh
Executable file
@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env bash
|
||||
# Build cerc/laconic-registry-cli
|
||||
|
||||
source ${CERC_CONTAINER_BASE_DIR}/build-base.sh
|
||||
|
||||
# See: https://stackoverflow.com/a/246128/1701505
|
||||
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
|
||||
|
||||
CERC_CONTAINER_BUILD_WORK_DIR=${CERC_CONTAINER_BUILD_WORK_DIR:-$SCRIPT_DIR}
|
||||
CERC_CONTAINER_BUILD_DOCKERFILE=${CERC_CONTAINER_BUILD_DOCKERFILE:-$SCRIPT_DIR/Dockerfile}
|
||||
CERC_CONTAINER_BUILD_TAG=${CERC_CONTAINER_BUILD_TAG:-cerc/nextjs-base:local}
|
||||
|
||||
docker build -t $CERC_CONTAINER_BUILD_TAG ${build_command_args} -f $CERC_CONTAINER_BUILD_DOCKERFILE $CERC_CONTAINER_BUILD_WORK_DIR
|
||||
rc=$?
|
||||
|
||||
if [ $rc -ne 0 ]; then
|
||||
echo "BUILD FAILED" 1>&2
|
||||
exit $rc
|
||||
fi
|
||||
|
||||
if [ "$CERC_CONTAINER_BUILD_TAG" != "cerc/nextjs-base:local" ]; then
|
||||
cat <<EOF
|
||||
|
||||
#################################################################
|
||||
|
||||
Built host container for $CERC_CONTAINER_BUILD_WORK_DIR with tag:
|
||||
|
||||
$CERC_CONTAINER_BUILD_TAG
|
||||
|
||||
To test locally run:
|
||||
|
||||
laconic-so run-webapp --image $CERC_CONTAINER_BUILD_TAG --env-file /path/to/environment.env
|
||||
|
||||
EOF
|
||||
fi
|
@ -0,0 +1,52 @@
|
||||
#!/bin/bash
|
||||
|
||||
if [ -n "$CERC_SCRIPT_DEBUG" ]; then
|
||||
set -x
|
||||
fi
|
||||
|
||||
WORK_DIR="${1:-./}"
|
||||
SRC_DIR="${2:-.next}"
|
||||
TRG_DIR="${3:-.next-r}"
|
||||
|
||||
CERC_BUILD_TOOL="${CERC_BUILD_TOOL}"
|
||||
if [ -z "$CERC_BUILD_TOOL" ]; then
|
||||
if [ -f "pnpm-lock.yaml" ]; then
|
||||
CERC_BUILD_TOOL=pnpm
|
||||
elif [ -f "yarn.lock" ]; then
|
||||
CERC_BUILD_TOOL=yarn
|
||||
elif [ -f "bun.lockb" ]; then
|
||||
CERC_BUILD_TOOL=bun
|
||||
else
|
||||
CERC_BUILD_TOOL=npm
|
||||
fi
|
||||
fi
|
||||
|
||||
cd "${WORK_DIR}" || exit 1
|
||||
|
||||
rm -rf "$TRG_DIR"
|
||||
mkdir -p "$TRG_DIR"
|
||||
cp -rp "$SRC_DIR" "$TRG_DIR/"
|
||||
|
||||
if [ -f ".env" ]; then
|
||||
TMP_ENV=`mktemp`
|
||||
declare -px > $TMP_ENV
|
||||
set -a
|
||||
source .env
|
||||
source $TMP_ENV
|
||||
set +a
|
||||
rm -f $TMP_ENV
|
||||
fi
|
||||
|
||||
for f in $(find . -type f \( -regex '.*.html?' -or -regex ".*.[tj]s\(x\|on\)?$" \) | grep -v 'node_modules' | grep -v '.git'); do
|
||||
for e in $(cat "${f}" | tr -s '[:blank:]' '\n' | tr -s '["/\\{},();]' '\n' | tr -s "[']" '\n' | egrep -o -e '^CERC_RUNTIME_ENV_.+$' -e '^LACONIC_HOSTED_CONFIG_.+$'); do
|
||||
orig_name=$(echo -n "${e}" | sed 's/"//g')
|
||||
cur_name=$(echo -n "${orig_name}" | sed 's/CERC_RUNTIME_ENV_//g')
|
||||
cur_val=$(echo -n "\$${cur_name}" | envsubst)
|
||||
if [ "$CERC_RETAIN_ENV_QUOTES" != "true" ]; then
|
||||
cur_val=$(sed "s/^[\"']//" <<< "$cur_val" | sed "s/[\"']//")
|
||||
fi
|
||||
esc_val=$(sed 's/[&/\]/\\&/g' <<< "$cur_val")
|
||||
echo "$f: $cur_name=$cur_val"
|
||||
sed -i "s/$orig_name/$esc_val/g" $f
|
||||
done
|
||||
done
|
@ -0,0 +1,201 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
if [ -n "${CERC_SCRIPT_DEBUG:-}" ]; then
|
||||
set -x
|
||||
fi
|
||||
|
||||
# Determine the build tool
|
||||
CERC_BUILD_TOOL="${CERC_BUILD_TOOL:-}"
|
||||
if [ -z "$CERC_BUILD_TOOL" ]; then
|
||||
if [ -f "pnpm-lock.yaml" ]; then
|
||||
CERC_BUILD_TOOL=pnpm
|
||||
elif [ -f "yarn.lock" ]; then
|
||||
CERC_BUILD_TOOL=yarn
|
||||
elif [ -f "bun.lockb" ]; then
|
||||
CERC_BUILD_TOOL=bun
|
||||
else
|
||||
CERC_BUILD_TOOL=npm
|
||||
fi
|
||||
fi
|
||||
|
||||
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
|
||||
WORK_DIR="${1:-/app}"
|
||||
|
||||
cd "${WORK_DIR}" || exit 1
|
||||
|
||||
# Get the app's Next.js version
|
||||
CERC_NEXT_VERSION=$(jq -r '.dependencies.next' package.json)
|
||||
echo "Using Next.js version: $CERC_NEXT_VERSION"
|
||||
|
||||
# Determine webpack version based on Next.js version (unused if already installed)
|
||||
determine_webpack_version() {
|
||||
local next_version=$1
|
||||
local major_version=${next_version%%.*}
|
||||
case $major_version in
|
||||
13)
|
||||
echo "5.75.0"
|
||||
;;
|
||||
14)
|
||||
echo "5.88.0"
|
||||
;;
|
||||
*)
|
||||
echo "5.93.0" # Default to latest stable version if unknown
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Check if webpack is already in package.json
|
||||
if CERC_WEBPACK_VERSION=$(jq -r '.dependencies.webpack // .devDependencies.webpack // empty' package.json); then
|
||||
if [ -n "$CERC_WEBPACK_VERSION" ]; then
|
||||
echo "Using existing webpack version: $CERC_WEBPACK_VERSION"
|
||||
else
|
||||
CERC_WEBPACK_VERSION=$(determine_webpack_version "$CERC_NEXT_VERSION")
|
||||
echo "Determined webpack version based on Next.js: $CERC_WEBPACK_VERSION"
|
||||
# Add webpack to devDependencies
|
||||
jq ".devDependencies.webpack = \"$CERC_WEBPACK_VERSION\"" package.json > package.json.tmp && mv package.json.tmp package.json
|
||||
fi
|
||||
else
|
||||
CERC_WEBPACK_VERSION=$(determine_webpack_version "$CERC_NEXT_VERSION")
|
||||
echo "Determined webpack version based on Next.js: $CERC_WEBPACK_VERSION"
|
||||
# Add webpack to devDependencies
|
||||
jq ".devDependencies.webpack = \"$CERC_WEBPACK_VERSION\"" package.json > package.json.tmp && mv package.json.tmp package.json
|
||||
fi
|
||||
|
||||
# Determine the Next.js config file
|
||||
if [ -f "next.config.mjs" ]; then
|
||||
NEXT_CONFIG_JS="next.config.mjs"
|
||||
IMPORT_OR_REQUIRE="import"
|
||||
else
|
||||
NEXT_CONFIG_JS="next.config.js"
|
||||
IMPORT_OR_REQUIRE="require"
|
||||
fi
|
||||
|
||||
# If this file doesn't exist at all, we'll get errors below.
|
||||
if [ ! -f "${NEXT_CONFIG_JS}" ]; then
|
||||
touch ${NEXT_CONFIG_JS}
|
||||
fi
|
||||
|
||||
# Backup the original config if not already done
|
||||
if [ ! -f "next.config.dist" ]; then
|
||||
cp "$NEXT_CONFIG_JS" next.config.dist
|
||||
fi
|
||||
|
||||
# Install js-beautify if not present
|
||||
command -v js-beautify >/dev/null || npm i -g js-beautify
|
||||
|
||||
# Beautify the config file
|
||||
js-beautify "$NEXT_CONFIG_JS" > "${NEXT_CONFIG_JS}.pretty"
|
||||
mv "${NEXT_CONFIG_JS}.pretty" "$NEXT_CONFIG_JS"
|
||||
|
||||
# Add webpack import/require if not present
|
||||
if [ "$IMPORT_OR_REQUIRE" = "import" ]; then
|
||||
if ! grep -q "import.*webpack" "$NEXT_CONFIG_JS"; then
|
||||
sed -i '1iimport webpack from "webpack";' "$NEXT_CONFIG_JS"
|
||||
fi
|
||||
if ! grep -q "import.*createRequire" "$NEXT_CONFIG_JS"; then
|
||||
sed -i '2iimport { createRequire } from "module";\nconst require = createRequire(import.meta.url);' "$NEXT_CONFIG_JS"
|
||||
fi
|
||||
else
|
||||
if ! grep -q "require.*webpack" "$NEXT_CONFIG_JS"; then
|
||||
sed -i '1iconst webpack = require("webpack");' "$NEXT_CONFIG_JS"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Add environment mapping logic if not present
|
||||
if ! grep -q "let envMap;" "$NEXT_CONFIG_JS"; then
|
||||
cat << 'EOF' >> "$NEXT_CONFIG_JS"
|
||||
|
||||
let envMap;
|
||||
try {
|
||||
envMap = require('./.env-list.json').reduce((a, v) => {
|
||||
a[v] = JSON.stringify(`CERC_RUNTIME_ENV_${v.split(/\./).pop()}`);
|
||||
return a;
|
||||
}, {});
|
||||
} catch (e) {
|
||||
console.error('Error loading .env-list.json:', e);
|
||||
envMap = Object.keys(process.env).reduce((a, v) => {
|
||||
if (v.startsWith('CERC_')) {
|
||||
a[`process.env.${v}`] = JSON.stringify(process.env[v]);
|
||||
}
|
||||
return a;
|
||||
}, {});
|
||||
}
|
||||
console.log('Environment map:', envMap);
|
||||
EOF
|
||||
fi
|
||||
|
||||
# Add or update webpack configuration
|
||||
if ! grep -q '__xCfg__' "$NEXT_CONFIG_JS"; then
|
||||
cat << 'EOF' >> "$NEXT_CONFIG_JS"
|
||||
|
||||
const __xCfg__ = (nextConfig) => {
|
||||
return {
|
||||
...nextConfig,
|
||||
webpack: (config, options) => {
|
||||
config.plugins.push(new webpack.DefinePlugin(envMap));
|
||||
if (typeof nextConfig.webpack === 'function') {
|
||||
return nextConfig.webpack(config, options);
|
||||
}
|
||||
return config;
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
EOF
|
||||
|
||||
# Update the export/module.exports line
|
||||
if [ "$IMPORT_OR_REQUIRE" = "import" ]; then
|
||||
sed -i 's/export default/const __orig_cfg__ =/' "$NEXT_CONFIG_JS"
|
||||
echo "export default __xCfg__(__orig_cfg__);" >> "$NEXT_CONFIG_JS"
|
||||
else
|
||||
sed -i 's/module.exports =/const __orig_cfg__ =/' "$NEXT_CONFIG_JS"
|
||||
echo "module.exports = __xCfg__(__orig_cfg__);" >> "$NEXT_CONFIG_JS"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Clean up the config file
|
||||
js-beautify "$NEXT_CONFIG_JS" > "${NEXT_CONFIG_JS}.pretty"
|
||||
mv "${NEXT_CONFIG_JS}.pretty" "$NEXT_CONFIG_JS"
|
||||
|
||||
# Generate .env-list.json
|
||||
"${SCRIPT_DIR}/find-env.sh" "$(pwd)" > .env-list.json
|
||||
|
||||
# Update package.json
|
||||
[ ! -f "package.dist" ] && cp package.json package.dist
|
||||
|
||||
# Install dependencies
|
||||
$CERC_BUILD_TOOL install || exit 1
|
||||
|
||||
# Get the build command from package.json
|
||||
BUILD_COMMAND=`jq -r '.scripts.build // ""' package.json`
|
||||
|
||||
if [ -z "$BUILD_COMMAND" ]; then
|
||||
echo "No build command found in package.json. Using default 'next build'."
|
||||
BUILD_COMMAND="next build"
|
||||
fi
|
||||
|
||||
# Determine the appropriate build commands based on the Next.js version
|
||||
if semver -p -r ">=14.2.0" "$CERC_NEXT_VERSION"; then
|
||||
# For >= 14.2.0
|
||||
CERC_NEXT_COMPILE_COMMAND="${BUILD_COMMAND/next build/next build --experimental-build-mode compile}"
|
||||
CERC_NEXT_GENERATE_COMMAND="${BUILD_COMMAND/next build/next build --experimental-build-mode generate}"
|
||||
elif semver -p -r ">=13.4.2" "$CERC_NEXT_VERSION"; then
|
||||
# For 13.4.2 to 14.1.x
|
||||
CERC_NEXT_COMPILE_COMMAND="${BUILD_COMMAND/next build/next experimental-compile}"
|
||||
CERC_NEXT_GENERATE_COMMAND="${BUILD_COMMAND/next build/next experimental-generate}"
|
||||
else
|
||||
# For versions before 13.4.2
|
||||
CERC_NEXT_COMPILE_COMMAND="$BUILD_COMMAND"
|
||||
CERC_NEXT_GENERATE_COMMAND="$BUILD_COMMAND"
|
||||
fi
|
||||
|
||||
# Update package.json with the appropriate scripts
|
||||
cat package.json | jq ".scripts.cerc_compile = \"$CERC_NEXT_COMPILE_COMMAND\"" | jq ".scripts.cerc_generate = \"$CERC_NEXT_GENERATE_COMMAND\"" > package.json.$$
|
||||
mv package.json.$$ package.json
|
||||
|
||||
# Run the compile command
|
||||
time $CERC_BUILD_TOOL run cerc_compile || exit 1
|
||||
|
||||
exit 0
|
@ -0,0 +1,31 @@
|
||||
#!/bin/bash
|
||||
|
||||
if [ -n "$CERC_SCRIPT_DEBUG" ]; then
|
||||
set -x
|
||||
fi
|
||||
|
||||
WORK_DIR="${1:-./}"
|
||||
TMPF=$(mktemp)
|
||||
|
||||
cd "$WORK_DIR" || exit 1
|
||||
|
||||
for d in $(find . -maxdepth 1 -type d | grep -v '\./\.' | grep '/' | cut -d'/' -f2); do
|
||||
egrep "/$d[/$]?" .gitignore >/dev/null 2>/dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
for f in $(find "$d" -regex ".*.[tj]sx?$" -type f); do
|
||||
cat "$f" | tr -s '[:blank:]' '\n' | tr -s '[{},()]' '\n' | egrep -o 'process.env.[A-Za-z0-9_]+' >> $TMPF
|
||||
done
|
||||
done
|
||||
|
||||
NEXT_CONF="next.config.mjs next.config.js next.config.dist"
|
||||
for f in $NEXT_CONF; do
|
||||
if [ -f "$f" ]; then
|
||||
cat "$f" | tr -s '[:blank:]' '\n' | tr -s '[{},()]' '\n' | egrep -o 'process.env.[A-Za-z0-9_]+' >> $TMPF
|
||||
fi
|
||||
done
|
||||
|
||||
cat $TMPF | sort -u | jq --raw-input . | jq --slurp .
|
||||
rm -f $TMPF
|
@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env bash
|
||||
if [ -n "$CERC_SCRIPT_DEBUG" ]; then
|
||||
set -x
|
||||
fi
|
||||
|
||||
|
||||
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
|
||||
CERC_MAX_GENERATE_TIME=${CERC_MAX_GENERATE_TIME:-120}
|
||||
tpid=""
|
||||
|
||||
ctrl_c() {
|
||||
kill $tpid $(ps -ef | grep node | grep next | awk '{print $2}') 2>/dev/null
|
||||
}
|
||||
|
||||
trap ctrl_c INT
|
||||
|
||||
CERC_BUILD_TOOL="${CERC_BUILD_TOOL}"
|
||||
if [ -z "$CERC_BUILD_TOOL" ]; then
|
||||
if [ -f "pnpm-lock.yaml" ]; then
|
||||
CERC_BUILD_TOOL=pnpm
|
||||
elif [ -f "yarn.lock" ]; then
|
||||
CERC_BUILD_TOOL=yarn
|
||||
elif [ -f "bun.lockb" ]; then
|
||||
CERC_BUILD_TOOL=bun
|
||||
else
|
||||
CERC_BUILD_TOOL=npm
|
||||
fi
|
||||
fi
|
||||
|
||||
CERC_WEBAPP_FILES_DIR="${CERC_WEBAPP_FILES_DIR:-/app}"
|
||||
cd "$CERC_WEBAPP_FILES_DIR"
|
||||
|
||||
"$SCRIPT_DIR/apply-runtime-env.sh" "`pwd`" .next .next-r
|
||||
mv .next .next.old
|
||||
mv .next-r/.next .
|
||||
|
||||
if [ "$CERC_NEXTJS_SKIP_GENERATE" != "true" ]; then
|
||||
jq -e '.scripts.cerc_generate' package.json >/dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
npm run cerc_generate > gen.out 2>&1 &
|
||||
tail -f gen.out &
|
||||
tpid=$!
|
||||
|
||||
count=0
|
||||
generate_done="false"
|
||||
while [ $count -lt $CERC_MAX_GENERATE_TIME ] && [ "$generate_done" == "false" ]; do
|
||||
sleep 1
|
||||
count=$((count + 1))
|
||||
grep 'rendered as static' gen.out > /dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
generate_done="true"
|
||||
fi
|
||||
done
|
||||
|
||||
if [ $generate_done != "true" ]; then
|
||||
echo "ERROR: 'npm run cerc_generate' not successful within CERC_MAX_GENERATE_TIME" 1>&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
kill $tpid $(ps -ef | grep node | grep next | grep generate | awk '{print $2}') 2>/dev/null
|
||||
tpid=""
|
||||
fi
|
||||
fi
|
||||
|
||||
$CERC_BUILD_TOOL start . -- -p ${CERC_LISTEN_PORT:-80}
|
Loading…
Reference in New Issue
Block a user