Skip to content

Instantly share code, notes, and snippets.

@maietta
Last active April 15, 2023 17:18
Show Gist options
  • Save maietta/6ae2bf4e823c0220832208d58e686a18 to your computer and use it in GitHub Desktop.
Save maietta/6ae2bf4e823c0220832208d58e686a18 to your computer and use it in GitHub Desktop.
Deployment files specifically intended for CapRover, used to deploy SSR-enabled Svelte applications with the Node Adaptor.
APP_NAME="<APP_NAME>"
APP_TOKEN="<APP_TOKEN>"
CAPROVER_URL="https://apps.your-caprover-hostname.com"
#!/bin/bash
set -e
ENVIRONMENT="production"
# Check if @sveltejs/adapter-node is present in package.json
if ! grep -q "@sveltejs/adapter-node" package.json; then
echo "This script prepares and deploys SvelteKit for Node servers. See: https://kit.svelte.dev/docs/adapter-node#deploying for details."
echo "This script can add @sveltejs/adapter-node to your package.json and update svelte.config.js. If you would like to do this, please enter 'y' and press enter."
# Prompt user to enter y and press enter, or CTRL+C to exit
read -r -p "Continue? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
;;
*)
exit
;;
esac
yarn remove @sveltejs/adapter-auto
yarn add -D @sveltejs/adapter-node
sed -i 's/@sveltejs\/adapter-auto/@sveltejs\/adapter-node/g' svelte.config.js
exit;
fi
# Make sure the .env.$ENVIRONMENT file exists
if [ ! -f ".env.$ENVIRONMENT" ]; then
echo "ERROR: .env.$ENVIRONMENT file does not exist"
exit 1
fi
# Load environment variables from environment variables file
set -o allexport
source .env.$ENVIRONMENT
set +o allexport
# Make sure the $APP_NAME, $APP_TOKEN, and $CAPROVER_URL are set
if [ -z "$APP_NAME" ] || [ -z "$APP_TOKEN" ] || [ -z "$CAPROVER_URL" ]; then
echo "ERROR: APP_NAME, APP_TOKEN, and CAPROVER_URL must be set a .env.$ENVIRONMENT"
exit 1
fi
HASH=$(echo -n $APP_NAME | md5sum | cut -d ' ' -f 1)
# Build the docker image
docker build --build-arg BUILD=$ENVIRONMENT -t svelte-deploy:$HASH -f Dockerfile.release .
# Deploy to CapRover
docker run \
-e APP_NAME=$APP_NAME \
-e APP_TOKEN=$APP_TOKEN \
-e CAPROVER_URL=$CAPROVER_URL \
svelte-deploy:$HASH \
sh -c 'DEPLOY_SIZE=$(stat -c "%s" /app/deploy.tar) && echo "Deployment file size: $(printf "%.2f" $(echo "scale=2; $DEPLOY_SIZE/1048576" | bc))MB" && caprover deploy --tarFile /app/deploy.tar --appName $APP_NAME --appToken $APP_TOKEN --caproverUrl $CAPROVER_URL'
FROM node:18-alpine
RUN apk add --no-cache yarn && \
npm i -g caprover
WORKDIR /app
ARG BUILD=production
COPY prisma/ package.json yarn.lock .env.$BUILD ./
RUN set -a && . .env.$BUILD && set +a
RUN yarn install --frozen-lockfile
COPY . .
RUN yarn build
RUN yarn prisma generate
RUN { \
echo 'FROM node:18-alpine'; \
echo 'ENV ORIGIN=$ORIGIN'; \
echo 'ENV NODE_ENV=$BUILD'; \
echo 'USER nobody'; \
echo 'COPY . .'; \
echo 'ENV PORT=80'; \
echo 'EXPOSE 80'; \
echo 'ENTRYPOINT ["node", "build/index.js"]'; \
} > /app/Dockerfile \
&& echo "Dockerfile created successfully!"
RUN tar -cf deploy.tar Dockerfile package.json build/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment