From 414b8870363f183cc719ac75726f845929137c13 Mon Sep 17 00:00:00 2001 From: Thomas E Lackey Date: Fri, 10 Nov 2023 17:44:25 -0600 Subject: [PATCH] Allow setting build tool (npm/yarn) and next.js version. (#639) * Allow setting build tool (npm/yarn) and next.js version. --- .../cerc-nextjs-base/Dockerfile | 19 +++---------- .../cerc-nextjs-base/Dockerfile.webapp | 4 +++ .../scripts/apply-runtime-env.sh | 9 +++++++ .../cerc-nextjs-base/scripts/build-app.sh | 27 +++++++++++++++---- .../scripts/start-serving-app.sh | 11 +++++++- tests/webapp-test/run-webapp-test.sh | 8 +++--- 6 files changed, 52 insertions(+), 26 deletions(-) diff --git a/stack_orchestrator/data/container-build/cerc-nextjs-base/Dockerfile b/stack_orchestrator/data/container-build/cerc-nextjs-base/Dockerfile index 435d8c73..69e38932 100644 --- a/stack_orchestrator/data/container-build/cerc-nextjs-base/Dockerfile +++ b/stack_orchestrator/data/container-build/cerc-nextjs-base/Dockerfile @@ -28,28 +28,15 @@ RUN \ # [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 - -# [Optional] Uncomment if you want to install an additional version of node using nvm -# ARG EXTRA_NODE_VERSION=10 -# RUN su node -c "source /usr/local/share/nvm/nvm.sh && nvm install ${EXTRA_NODE_VERSION}" - -# 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 - -COPY /scripts /scripts + && apt-get -y install --no-install-recommends jq gettext-base moreutils # [Optional] Uncomment if you want to install more global node modules # RUN su node -c "npm install -g " -# RUN mkdir -p /config -# COPY ./config.yml /config - -# Install simple web server for now (use nginx perhaps later) -# RUN yarn global add http-server - # Expose port for http EXPOSE 3000 +COPY /scripts /scripts + # Default command sleeps forever so docker doesn't kill it CMD ["/scripts/start-serving-app.sh"] diff --git a/stack_orchestrator/data/container-build/cerc-nextjs-base/Dockerfile.webapp b/stack_orchestrator/data/container-build/cerc-nextjs-base/Dockerfile.webapp index f4b5d4d8..23f42385 100644 --- a/stack_orchestrator/data/container-build/cerc-nextjs-base/Dockerfile.webapp +++ b/stack_orchestrator/data/container-build/cerc-nextjs-base/Dockerfile.webapp @@ -1,4 +1,8 @@ FROM cerc/nextjs-base:local + +ARG CERC_NEXT_VERSION=latest +ARG CERC_BUILD_TOOL + WORKDIR /app COPY . . RUN rm -rf node_modules build .next* diff --git a/stack_orchestrator/data/container-build/cerc-nextjs-base/scripts/apply-runtime-env.sh b/stack_orchestrator/data/container-build/cerc-nextjs-base/scripts/apply-runtime-env.sh index 793333c3..1e30b634 100755 --- a/stack_orchestrator/data/container-build/cerc-nextjs-base/scripts/apply-runtime-env.sh +++ b/stack_orchestrator/data/container-build/cerc-nextjs-base/scripts/apply-runtime-env.sh @@ -8,6 +8,15 @@ 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 "yarn.lock" ]; then + CERC_BUILD_TOOL=npm + else + CERC_BUILD_TOOL=yarn + fi +fi + cd "${WORK_DIR}" || exit 1 rm -rf "$TRG_DIR" diff --git a/stack_orchestrator/data/container-build/cerc-nextjs-base/scripts/build-app.sh b/stack_orchestrator/data/container-build/cerc-nextjs-base/scripts/build-app.sh index c2115f6a..83977268 100755 --- a/stack_orchestrator/data/container-build/cerc-nextjs-base/scripts/build-app.sh +++ b/stack_orchestrator/data/container-build/cerc-nextjs-base/scripts/build-app.sh @@ -4,8 +4,17 @@ if [ -n "$CERC_SCRIPT_DEBUG" ]; then set -x fi -SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +CERC_NEXT_VERSION="${CERC_NEXT_VERSION:-^14.0.2}" +CERC_BUILD_TOOL="${CERC_BUILD_TOOL}" +if [ -z "$CERC_BUILD_TOOL" ]; then + if [ -f "yarn.lock" ] && [ ! -f "package-lock.json" ]; then + CERC_BUILD_TOOL=yarn + else + CERC_BUILD_TOOL=npm + fi +fi +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) WORK_DIR="${1:-/app}" cd "${WORK_DIR}" || exit 1 @@ -20,6 +29,7 @@ if [ $? -ne 0 ]; then fi js-beautify next.config.dist > next.config.js +echo "" >> next.config.js WEBPACK_REQ_LINE=$(grep -n "require([\'\"]webpack[\'\"])" next.config.js | cut -d':' -f1) if [ -z "$WEBPACK_REQ_LINE" ]; then @@ -58,7 +68,7 @@ if [ -n "$WEBPACK_CONF_LINE" ]; then cat > next.config.js.3 < next.config.js.2 cat > next.config.js.3 <&2 @@ -88,7 +98,14 @@ fi cat package.dist | jq '.scripts.cerc_compile = "next experimental-compile"' | jq '.scripts.cerc_generate = "next experimental-generate"' > package.json -npm install || exit 1 -npm run cerc_compile || exit 1 +CUR_NEXT_VERSION="`jq -r '.dependencies.next' package.json`" + +if [ "$CERC_NEXT_VERSION" != "keep" ] && [ "$CUR_NEXT_VERSION" != "$CERC_NEXT_VERSION" ]; then + echo "Changing 'next' version from $CUR_NEXT_VERSION to $CERC_NEXT_VERSION (set with --build-arg CERC_NEXT_VERSION)" + cat package.json | jq ".dependencies.next = \"$CERC_NEXT_VERSION\"" | sponge package.json +fi + +$CERC_BUILD_TOOL install || exit 1 +$CERC_BUILD_TOOL run cerc_compile || exit 1 exit 0 diff --git a/stack_orchestrator/data/container-build/cerc-nextjs-base/scripts/start-serving-app.sh b/stack_orchestrator/data/container-build/cerc-nextjs-base/scripts/start-serving-app.sh index 61664c68..04ab0ad3 100755 --- a/stack_orchestrator/data/container-build/cerc-nextjs-base/scripts/start-serving-app.sh +++ b/stack_orchestrator/data/container-build/cerc-nextjs-base/scripts/start-serving-app.sh @@ -5,6 +5,15 @@ fi SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +CERC_BUILD_TOOL="${CERC_BUILD_TOOL}" +if [ -z "$CERC_BUILD_TOOL" ]; then + if [ -f "yarn.lock" ] && [ ! -f "package-lock.json" ]; then + CERC_BUILD_TOOL=yarn + else + CERC_BUILD_TOOL=npm + fi +fi + CERC_WEBAPP_FILES_DIR="${CERC_WEBAPP_FILES_DIR:-/app}" cd "$CERC_WEBAPP_FILES_DIR" @@ -31,4 +40,4 @@ if [ "$CERC_NEXTJS_SKIP_GENERATE" != "true" ]; then fi fi -npm start . -p ${CERC_LISTEN_PORT:-3000} +$CERC_BUILD_TOOL start . -p ${CERC_LISTEN_PORT:-3000} diff --git a/tests/webapp-test/run-webapp-test.sh b/tests/webapp-test/run-webapp-test.sh index 71b4da16..d32e0eba 100755 --- a/tests/webapp-test/run-webapp-test.sh +++ b/tests/webapp-test/run-webapp-test.sh @@ -24,7 +24,7 @@ git clone https://git.vdb.to/cerc-io/test-progressive-web-app.git $CERC_REPO_BAS # Test webapp command execution $TEST_TARGET_SO build-webapp --source-repo $CERC_REPO_BASE_DIR/test-progressive-web-app -UUID=`uuidgen` +CHECK="SPECIAL_01234567890_TEST_STRING" set +e @@ -34,7 +34,7 @@ wget -O test.before -m http://localhost:3000 docker remove -f $CONTAINER_ID -CONTAINER_ID=$(docker run -p 3000:3000 -e CERC_WEBAPP_DEBUG=$UUID -d cerc/test-progressive-web-app:local) +CONTAINER_ID=$(docker run -p 3000:3000 -e CERC_WEBAPP_DEBUG=$CHECK -d cerc/test-progressive-web-app:local) sleep 3 wget -O test.after -m http://localhost:3000 @@ -43,7 +43,7 @@ docker remove -f $CONTAINER_ID echo "###########################################################################" echo "" -grep "$UUID" test.before > /dev/null +grep "$CHECK" test.before > /dev/null if [ $? -ne 1 ]; then echo "BEFORE: FAILED" exit 1 @@ -51,7 +51,7 @@ else echo "BEFORE: PASSED" fi -grep "$UUID" test.after > /dev/null +grep "$CHECK" test.after > /dev/null if [ $? -ne 0 ]; then echo "AFTER: FAILED" exit 1