From 97bc072da168da8c070470b92897592a5eb24907 Mon Sep 17 00:00:00 2001 From: Touqeer Ahmad Date: Fri, 11 Jun 2021 15:15:23 +0800 Subject: [PATCH] NextJS support added --- README.md | 17 +++++- deploy-scripts.sh | 2 +- installer/upgrade.sh | 3 + projects/nextjs/build/npm.sh | 39 +++++++++++++ projects/nextjs/format/nextjs.sh | 11 ++++ projects/nextjs/installer/install.sh | 12 ++++ .../nextjs/push/git-bare/post-receive-hook | 18 ++++++ projects/nextjs/template/app-config.sh | 9 +++ .../environments/default/assets/nginx.conf | 22 ++++++++ .../template/environments/default/config.sh | 2 + tests/nextjs.sh | 56 +++++++++++++++++++ tests/resources/nextjs-project/package.json | 15 +++++ tests/resources/nextjs-project/pages/_app.js | 5 ++ tests/resources/nextjs-project/pages/index.js | 5 ++ 14 files changed, 213 insertions(+), 3 deletions(-) create mode 100644 projects/nextjs/build/npm.sh create mode 100644 projects/nextjs/format/nextjs.sh create mode 100644 projects/nextjs/installer/install.sh create mode 100644 projects/nextjs/push/git-bare/post-receive-hook create mode 100644 projects/nextjs/template/app-config.sh create mode 100644 projects/nextjs/template/environments/default/assets/nginx.conf create mode 100644 projects/nextjs/template/environments/default/config.sh create mode 100644 tests/nextjs.sh create mode 100644 tests/resources/nextjs-project/package.json create mode 100644 tests/resources/nextjs-project/pages/_app.js create mode 100644 tests/resources/nextjs-project/pages/index.js diff --git a/README.md b/README.md index 7e043aa..821e41f 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ The following stacks are currently supported - Django - React JS - Node JS +- Next JS - PHP - Static HTML @@ -103,6 +104,9 @@ sh install.sh python /path/to/django/project # or for a reactjs project sh install.sh reactjs /path/to/reactjs/project +# or for a nextjs project +sh install.sh nextjs /path/to/nextjs/project + # or for a node project sh install.sh node /path/to/node/project @@ -130,6 +134,9 @@ deploy --install python /path/to/django/project # or for a reactjs project deploy --install reactjs /path/to/reactjs/project +# or for a nextjs project +deploy --install nextjs /path/to/nextjs/project + # or for a node project deploy --install node /path/to/node/project @@ -188,7 +195,7 @@ It can also use the commented out optional files that are listed, but will work All projects must have the following vars defined (in app-config.sh or config.sh) so that the relevant steps are executed. ```bash -# The project type. Currently supported options are: java, rails, python, node, and reactjs +# The project type. Currently supported options are: java, rails, python, node, reactjs and nextjs TYPE=rails # The name that will be used to label the app that is being deployed, commonly the hostname where the service is made available is used SERVICE_NAME=service.com @@ -294,6 +301,8 @@ sh tests/java.sh sh tests/rails.sh # Test React JS deployment sh tests/reactjs.sh +# Test Next JS deployment +sh tests/nextjs.sh # Test Django deployment sh tests/django.sh # Test Static HTML and PHP website deployment @@ -361,6 +370,8 @@ Currently allowed values: - `reactjs` +- `nextjs` + - `html` - for static HTML sites or simple PHP sites ### `BUILD` @@ -371,7 +382,7 @@ Currently allowed values: - `mvnw` - for java projects -- `npm` - for reactjs projects +- `npm` - for reactjs and nextjs projects ### `FORMAT` @@ -389,6 +400,8 @@ Currently allowed values: - `reactjs` - for reactjs projects +- `nextjs` - for nextjs projects + - `static` - for static HTML sites or simple PHP sites ### `REPO` diff --git a/deploy-scripts.sh b/deploy-scripts.sh index a1bd9d2..4bcfec9 100755 --- a/deploy-scripts.sh +++ b/deploy-scripts.sh @@ -16,7 +16,7 @@ show_ds_usage() { show_installer_usage() { show_version - printf "\nUsage:\n\tdeploy-scripts.sh --install [project type] [project directory] [options]\n\nThe values for [project type] can be\n\tjava\n\trails\n\tpython\n\treactjs\n\thtml\n\tnode\n" + printf "\nUsage:\n\tdeploy-scripts.sh --install [project type] [project directory] [options]\n\nThe values for [project type] can be\n\tjava\n\trails\n\tpython\n\treactjs\n\tnextjs\n\thtml\n\tnode\n" exit 1 } diff --git a/installer/upgrade.sh b/installer/upgrade.sh index 2758f14..ece1aec 100644 --- a/installer/upgrade.sh +++ b/installer/upgrade.sh @@ -42,6 +42,9 @@ elif [ "$BUILD" = "python-django" ]; then elif [ "$BUILD" = "reactjs" ]; then TYPE="reactjs" BUILD="npm" +elif [ "$BUILD" = "nextjs" ]; then + TYPE="nextjs" + BUILD="npm" elif [ "$BUILD" = "rails" ]; then TYPE="rails" BUILD="" diff --git a/projects/nextjs/build/npm.sh b/projects/nextjs/build/npm.sh new file mode 100644 index 0000000..926e72c --- /dev/null +++ b/projects/nextjs/build/npm.sh @@ -0,0 +1,39 @@ +ds_build() { + if [ "$1" = "" ] || [ "$2" = "" ]; then + error "build: npm: Too few arguments given" + fi + if [ ! -d "$2" ]; then + error "build: npm: No package directory available to place warfile" + fi + cd "$1" + + ENV_JS_PATH="$1/../repo/$DS_DIR/environments/$PROJECT_ENVIRONMENT/.env" + if [ ! -f "$ENV_JS_PATH" ]; then + if [ "$NEXTJS_ENVJS_PATH" != "" ]; then + CHECK_ABS_PATH=$(echo $NEXTJS_ENVJS_PATH | cut -c -1) + if [ "$CHECK_ABS_PATH" = "/" ]; then + ENV_JS_PATH="$NEXTJS_ENVJS_PATH" + else + ENV_JS_PATH="$1/../repo/$DS_DIR/$NEXTJS_ENVJS_PATH" + fi + fi + fi + if [ ! -f "$ENV_JS_PATH" ]; then + warning "No .env found at $ENV_JS_PATH. Building without it" + else + ln -s "$ENV_JS_PATH" .env + fi + + if [ "$NPM_PREFIX" != "" ]; then + mkdir -p $NPM_PREFIX && ln -sf $NPM_PREFIX node_modules && npm install + else + npm install + fi + npx next build + npx next export + if [ -d "./out" ]; then + cp -r ./out/* "$2/" + else + error "NPM output directory not found under either out/" + fi +} \ No newline at end of file diff --git a/projects/nextjs/format/nextjs.sh b/projects/nextjs/format/nextjs.sh new file mode 100644 index 0000000..1fc144d --- /dev/null +++ b/projects/nextjs/format/nextjs.sh @@ -0,0 +1,11 @@ +ds_format() { + if [ "$1" = "" ]; then + error "format: nextjs: Too few arguments given" + fi + + cd "$1" + + info "Preparing deployment files for packaging ... " + git --work-tree=./ --git-dir=../repo/.git checkout -f 2>&1 | indent + success 'done' +} diff --git a/projects/nextjs/installer/install.sh b/projects/nextjs/installer/install.sh new file mode 100644 index 0000000..491fcf4 --- /dev/null +++ b/projects/nextjs/installer/install.sh @@ -0,0 +1,12 @@ +ds_install () { + if [ "$1" = "" ]; then + error "installer: nextjs: Project directory not supplied" + fi + + ds_create_dir_structure "$1" "$DS_DIR" "nextjs" + infof "Adding nextjs vars to app-config.sh ... " + printf "BUILD=npm\n" >> "$1/$DS_DIR/app-config.sh" + printf "LINKED_FILES=\"src/_config/env.js\"\n" >> "$1/$DS_DIR/app-config.sh" + printf "LINKED_DIRS=\"logs\"\n" >> "$1/$DS_DIR/app-config.sh" + success "done" +} diff --git a/projects/nextjs/push/git-bare/post-receive-hook b/projects/nextjs/push/git-bare/post-receive-hook new file mode 100644 index 0000000..185c812 --- /dev/null +++ b/projects/nextjs/push/git-bare/post-receive-hook @@ -0,0 +1,18 @@ +#!/bin/sh + +set -e + +SCRIPT_PATH=$(dirname $(readlink -f $0)) +. $SCRIPT_PATH/post-receive-utils.sh + +deploy + +if [ "$FORMAT" = "nextjs" ] && [ "$BUILD" = "" ]; then + title 'deploy - build' + cd $DEPLOY_DIR/current + npm install + npx next build + npx next export +fi + +post_startup diff --git a/projects/nextjs/template/app-config.sh b/projects/nextjs/template/app-config.sh new file mode 100644 index 0000000..b59a01c --- /dev/null +++ b/projects/nextjs/template/app-config.sh @@ -0,0 +1,9 @@ +TYPE=nextjs +BUILD=npm +REPO=git@github.com:loanstreet/app-name.git + +DEPLOYMENT_SERVER=dev-partners.loanstreet.com.my +DEPLOYMENT_SSH_USER=deploy +SERVICE_NAME=app-name +LINKED_FILES="src/_config/env.js" +LINKED_DIRS="logs" diff --git a/projects/nextjs/template/environments/default/assets/nginx.conf b/projects/nextjs/template/environments/default/assets/nginx.conf new file mode 100644 index 0000000..7f179b9 --- /dev/null +++ b/projects/nextjs/template/environments/default/assets/nginx.conf @@ -0,0 +1,22 @@ +server { + listen 80 ; + server_name xc-test.finology.com.my; + + gzip on; + gzip_http_version 1.0; + gzip_disable "msie6"; + gzip_vary on; + gzip_min_length 1100; + gzip_buffers 64 8k; + gzip_comp_level 3; + gzip_proxied any; + gzip_types text/css text/xml application/x-javascript application/atom+xml text/mathml text/plain text/vnd.sun.j2me.app-descriptor text/vnd.wap.wml text/x-component; + + root /home/deploy/sites/xpresscover-portal/staging/current/build; + access_log /home/deploy/sites/xpresscover-portal/staging/current/logs/nginx.access.log; + error_log /home/deploy/sites/xpresscover-portal/staging/current/logs/nginx.error.log info; + + error_page 500 502 503 504 /500.html; + client_max_body_size 25M; + keepalive_timeout 10; +} diff --git a/projects/nextjs/template/environments/default/config.sh b/projects/nextjs/template/environments/default/config.sh new file mode 100644 index 0000000..e16ed0f --- /dev/null +++ b/projects/nextjs/template/environments/default/config.sh @@ -0,0 +1,2 @@ +PROJECT_ENVIRONMENT=default +GIT_BRANCH=default diff --git a/tests/nextjs.sh b/tests/nextjs.sh new file mode 100644 index 0000000..6bd8d8c --- /dev/null +++ b/tests/nextjs.sh @@ -0,0 +1,56 @@ +#!/bin/sh + +set -e + +SCRIPT_PATH=$(dirname $(readlink -f $0)) +. $SCRIPT_PATH/common.sh + +PROJECT_FILES_DIR=nextjs-project +SRV_SCRIPT_PID=$(ps -elf | grep 'tmp-webserver.js' | grep -v grep | awk '{print $4}') +if [ "$SRV_SCRIPT_PID" != "" ]; then + printf "Killing old server.js PID: $SRV_SCRIPT_PID ... " + kill -9 $SRV_SCRIPT_PID + success 'done' +fi +copy_deployment_files 'nextjs' $SCRIPT_PATH/resources/$PROJECT_FILES_DIR + +title 'TEST - editing configs' + +SERVICE_NAME="nextjs-deploy-test" +PROJECT_ENVIRONMENT="default" +DEPLOYMENT_DIR="$TEST_WORKING_DIR/$SERVICE_NAME/$PROJECT_ENVIRONMENT" + +cd $COPY_PROJECT_DIR/$PROJECT_FILES_DIR +PROJECT_DEPLOY_DIR="$COPY_PROJECT_DIR/nextjs-project/deploy" +printf "\nDEPLOYMENT_DIR=$DEPLOYMENT_DIR\nDEPLOYMENT_SERVER=localhost\nDEPLOYMENT_SERVER_USER=$USER\nREPO=file://$COPY_PROJECT_DIR/$PROJECT_FILES_DIR\nSERVICE_NAME=$SERVICE_NAME\nLINKED_FILES=\nLINKED_DIRS=\n" >> deploy/app-config.sh +printf "PROJECT_ENVIRONMENT=$PROJECT_ENVIRONMENT\nGIT_BRANCH=master\nBUILD=''\nFORMAT=nextjs\n" >> deploy/environments/default/config.sh +cat deploy/app-config.sh +cat deploy/environments/default/config.sh +title 'TEST - deploying default environment' +rm -rf $TEST_WORKING_DIR +#sh deploy/deploy.sh default +PROJECT_DEPLOY_DIR=$PROJECT_DEPLOY_DIR sh $SCRIPT_PATH/../scripts/deploy.sh default +cd $TEST_WORKING_DIR/nextjs-deploy-test/default/current +cp $COPY_PROJECT_DIR/$PROJECT_FILES_DIR/package.json.deploy ./package.json +cp $COPY_PROJECT_DIR/$PROJECT_FILES_DIR/tmp-webserver.js ./ +cp $COPY_PROJECT_DIR/$PROJECT_FILES_DIR/phantomjs.js ./ +# npm init -y +#npm i +npm i connect serve-static phantomjs-prebuilt --save-dev +node tmp-webserver.js $TEST_WORKING_DIR/nextjs-deploy-test/default/current & +# npm run dev-server & +sleep 6 +SRV_SCRIPT_PID=$(ps -elf | grep 'tmp-webserver' | grep -v grep | awk '{print $4}') +info "Temp Web Server PID: $SRV_SCRIPT_PID" +title 'TEST - check web application' +export PATH="$TEST_WORKING_DIR/nextjs-deploy-test/default/current/node_modules/phantomjs-prebuilt/lib/phantom/bin:$PATH" +phantomjs phantomjs.js > index.test.html 2>&1 +printf 'Checking index page contents ... ' +if [ $(grep -c 'Welcome to React Parcel Micro App!' index.test.html) -eq 1 ]; then + success 'success!' +else + error 'fail! :(' +fi +kill -9 $SRV_SCRIPT_PID +cd $SCRIPT_PATH/../ +rm -rf /tmp/deploy-scripts diff --git a/tests/resources/nextjs-project/package.json b/tests/resources/nextjs-project/package.json new file mode 100644 index 0000000..885f109 --- /dev/null +++ b/tests/resources/nextjs-project/package.json @@ -0,0 +1,15 @@ +{ + "name": "nextjs-project", + "version": "0.1.0", + "private": true, + "scripts": { + "dev": "next dev", + "build": "next build", + "start": "next start" + }, + "dependencies": { + "next": "10.2.3", + "react": "17.0.2", + "react-dom": "17.0.2" + } +} diff --git a/tests/resources/nextjs-project/pages/_app.js b/tests/resources/nextjs-project/pages/_app.js new file mode 100644 index 0000000..3ac920d --- /dev/null +++ b/tests/resources/nextjs-project/pages/_app.js @@ -0,0 +1,5 @@ +function MyApp({ Component, pageProps }) { + return +} + +export default MyApp diff --git a/tests/resources/nextjs-project/pages/index.js b/tests/resources/nextjs-project/pages/index.js new file mode 100644 index 0000000..92ab24b --- /dev/null +++ b/tests/resources/nextjs-project/pages/index.js @@ -0,0 +1,5 @@ +export default function Home() { + return ( +

Welcome to Nextjs Parcel Micro App!

+ ) +}