Skip to content

Instantly share code, notes, and snippets.

@maietta
Created December 13, 2023 18:30
Show Gist options
  • Save maietta/d3db6db6acfd2b8f053077f9771d3958 to your computer and use it in GitHub Desktop.
Save maietta/d3db6db6acfd2b8f053077f9771d3958 to your computer and use it in GitHub Desktop.
Publish to CapRover - for SvelteKit SSR apps.
#!/bin/bash
# Exit if any command fails
set -e
# Check if the .env.production file exists
if [ ! -f .env.production ]; then
echo "The .env.production file does not exist. Please create it and add the necessary environment variables to deploy to CapRover."
exit 1
fi
# Load the environment variables
source .env.production
# If the caprover CLI is not installed, install it
if ! command -v caprover &> /dev/null; then
npm i -g caprover
fi
# Check if the .gitignore file exists and add deploy.tar to it if it doesn't exist
if [ -f .gitignore ]; then
if ! grep -q "deploy.tar" .gitignore; then
echo "deploy.tar" >> .gitignore
fi
else
echo "deploy.tar" > .gitignore
fi
# Check if the Dockerfile exists and add it to the .gitignore file if it doesn't exist
if [ -f Dockerfile ]; then
if ! grep -q "Dockerfile" .gitignore; then
echo "Dockerfile" >> .gitignore
fi
else
echo "Dockerfile" > .gitignore
fi
# Check to see if "@sveltejs/adapter-auto" is in the svelte.config.js or svelte.config.ts file and if so, change it to "@sveltejs/adapter-node" and install the adapter.
if [ -f svelte.config.js ]; then
if grep -q "@sveltejs/adapter-auto" svelte.config.js; then
sed -i 's/@sveltejs\/adapter-auto/@sveltejs\/adapter-node/g' svelte.config.js
npm i -D @sveltejs/adapter-node
fi
elif [ -f svelte.config.ts ]; then
if grep -q "@sveltejs/adapter-auto" svelte.config.ts; then
sed -i 's/@sveltejs\/adapter-auto/@sveltejs\/adapter-node/g' svelte.config.ts
npm i -D @sveltejs/adapter-node
fi
fi
# Build the app
npm run build
# Create the Dockerfile if it doesn't exist
if [ ! -f Dockerfile ]; then
cat << 'EOF' > Dockerfile
FROM node:20-alpine3.19
ENV NODE_ENV=production
ENV ORIGIN=${ORIGIN}
WORKDIR /app
COPY . .
RUN npm ci --omit=dev
ENV PORT=80
EXPOSE ${PORT}
CMD ["node", "/app/build/index.js"]
EOF
fi
# List of files to potentially add to the archive
files_to_add=("Dockerfile" "package.json" "package-lock.json" "bun.lockb")
# Create the initial archive if it doesn't exist
tar -cf deploy.tar build/
# Loop through the list of files and append them to the archive if they exist
for file in "${files_to_add[@]}"; do
if [ -f "$file" ]; then
tar --append -f deploy.tar "$file"
fi
done
# Deploy the app to CapRover using the CapRover CLI
caprover deploy \
--tarFile deploy.tar \
--appName ${APP_NAME} \
--appToken ${APP_TOKEN} \
--caproverUrl ${CAPROVER_URL}
# Remove the archive
rm deploy.tar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment