Last active
April 2, 2023 16:08
-
-
Save maietta/bbbb52457e953afa4840fa97c5f47464 to your computer and use it in GitHub Desktop.
deploy.sh updated. Final builds <2mb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
<< BLOCK | |
MIT License. Do as you wish with this. | |
Author: 2023 Nick Maietta <[email protected]> | |
This is a bash script that deploys a SvelteKit application to CapRover using the Node 18 enviornment on Alpine Linux. | |
The script starts by checking if the user has provided an environment name as a command-line argument. If no argument | |
is provided, the script outputs an error message and exits. | |
The script then sets up some environment variables based on the argument provided. If the argument is "dev" or "development", | |
the script sets the environment file to ".env". Otherwise, it sets the environment file to ".env.<environment_name>". | |
Next, the script checks if the environment file exists. If it doesn't exist, the script prompts the user to create it. | |
If the user agrees, the script prompts the user for the APP_NAME, APP_TOKEN, and CAPROVER_URL variables, and writes those | |
variables to the environment file. | |
After that, the script installs the CapRover CLI globally using npm, if it is not already installed. Then, the script | |
checks if the app is using the @sveltejs/adapter-auto adapter. If it is, the script changes it to @sveltejs/adapter-node. | |
The script then builds the project using npm, installs only production dependencies using npm ci, and creates a Dockerfile | |
for running the app on the host. The Dockerfile is written to a file named "captain-definition" and includes the necessary | |
environment variable to prevent CORS issues. The script then creates a tar file that includes the "captain-definition" file, | |
the "package*.json" files, and the "build/" directory. | |
Finally, the script deploys the project using the CapRover CLI, by passing the tar file, app name, app token, and CapRover URL | |
as arguments. After the deployment is complete, the script cleans up the tar file. | |
Overall, this script automates the deployment process for a Node.js app to CapRover, by setting up the necessary environment | |
variables, building the app, creating a Dockerfile, and deploying the app using the CapRover CLI. | |
BLOCK | |
set -e | |
set -o pipefail | |
if [[ -z "$1" ]]; then | |
echo "Error: Please provide an environment name. Example: ./deploy.sh production" | |
echo "Usage: ./deploy.sh <env>" | |
exit 1 | |
fi | |
ENV="$1" | |
if [[ "$ENV" == "dev" || "$ENV" == "development" ]]; then | |
ENV_FILE=".env" | |
else | |
ENV_FILE=".env.$ENV" | |
fi | |
if [[ ! -f "$ENV_FILE" ]]; then | |
read -p "Environment file $ENV_FILE not found. Create it? (y/n) " -n 1 -r | |
echo | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
echo "Please enter the following information for your app:" | |
read -p "Enter APP_NAME: " APP_NAME | |
read -p "Enter APP_TOKEN: " APP_TOKEN | |
read -p "Enter CAPROVER_URL: " CAPROVER_URL | |
cat << EOF > "$ENV_FILE" | |
APP_NAME=$APP_NAME | |
APP_TOKEN=$APP_TOKEN | |
CAPROVER_URL=$CAPROVER_URL | |
EOF | |
echo "Environment file $ENV_FILE created." | |
else | |
echo "Error: $ENV_FILE not found." | |
exit 1 | |
fi | |
fi | |
source "$ENV_FILE" | |
if ! [ -x "$(command -v caprover)" ]; then | |
echo "Installing the CapRover CLI globally." | |
npm install --quiet -g caprover | |
fi | |
# Check if the project is using the auto adapter. If so, change it to node. | |
if npm ls --depth=0 @sveltejs/adapter-auto >/dev/null 2>&1; then | |
sed -i 's/@sveltejs\/adapter-auto/@sveltejs\/adapter-node/g' svelte.config.js | |
npm remove @sveltejs/adapter-auto | |
npm install --save-dev @sveltejs/adapter-node | |
fi | |
# Build the project | |
npm install | |
npm run build | |
npm ci --omit=dev # Install only production dependencies | |
# Create the Dockerfile for running the app on the host. CapRover provides the ORIGIN environment variable to prevent CORS issues. | |
cat <<EOF > captain-definition | |
{ | |
"schemaVersion": 2, | |
"dockerfileLines": [ | |
"FROM node:18-alpine", | |
"ENV ORIGIN=\${ORIGIN}", | |
"COPY . .", | |
"ENV NODE_ENV production", | |
"ENV PORT 80", | |
"EXPOSE 80", | |
"CMD [ \"node\", \"build/index.js\" ]" | |
] | |
} | |
EOF | |
# Create the tar file | |
tar -cf deploy.tar captain-definition package*.json build/ | |
# Deploy the project | |
caprover deploy \ | |
--tarFile deploy.tar \ | |
--appName "${APP_NAME}" \ | |
--appToken "${APP_TOKEN}" \ | |
--caproverUrl "${CAPROVER_URL}" | |
# Cleanup | |
unlink deploy.tar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment