update only if new commits

This commit is contained in:
Dobromir Popov
2024-02-23 21:37:16 +02:00
parent bce92fb6cc
commit 9b8fe36155

View File

@ -1,11 +1,13 @@
#!/bin/sh
# Check if the environment variable to update code from git is set to true
if [ "$UPDATE_CODE_FROM_GIT" = "true" ]; then
# Install necessary packages
apk add git nano rsync
echo "Updating code from git.d-popov.com...(as '$GIT_USERNAME')"
# Check for the current Git commit ID in the local copy
CURRENT_GIT_COMMIT_ID=$(git -C /app rev-parse HEAD 2>/dev/null)
# Remove the previous clone directory to ensure a fresh start
rm -rf /tmp/clone
mkdir /tmp/clone
@ -18,28 +20,41 @@ if [ "$UPDATE_CODE_FROM_GIT" = "true" ]; then
echo "Current Git Commit: $LAST_COMMIT_MESSAGE: $GIT_COMMIT_ID"
export GIT_COMMIT_ID
# Use rsync to synchronize the files to /app, including deletion of files not in the source
rsync -av --delete /tmp/clone/ /app/ || echo "Rsync failed: Issue synchronizing files"
# Copy .env files
rsync -av /tmp/clone/.env* /app/ || echo "Rsync failed: Issue copying .env files"
# Copy the entrypoint.sh if exists in the new structure
[ -f /tmp/clone/entrypoint.sh ] && rsync -av /tmp/clone/entrypoint.sh /app/entrypoint.sh || echo "Rsync failed: Issue copying entrypoint.sh"
chmod +x /app/entrypoint.sh
# Check if the current commit ID matches the latest commit ID
if [ "$CURRENT_GIT_COMMIT_ID" != "$GIT_COMMIT_ID" ]; then
echo "Detected changes in the repository, updating /app..."
# Use rsync to synchronize the files to /app, including deletion of files not in the source
rsync -av --delete /tmp/clone/ /app/ || echo "Rsync failed: Issue synchronizing files"
# Copy .env files
rsync -av /tmp/clone/.env* /app/ || echo "Rsync failed: Issue copying .env files"
# Copy the entrypoint.sh if exists in the new structure
[ -f /tmp/clone/entrypoint.sh ] && rsync -av /tmp/clone/entrypoint.sh /app/entrypoint.sh || echo "Rsync failed: Issue copying entrypoint.sh"
chmod +x /app/entrypoint.sh
# Delete and reinstall node_modules if there are changes
echo "Deleting and reinstalling node_modules..."
rm -rf /app/node_modules
cd /app
npm install --no-audit --no-fund --no-optional --omit=optional
yes | npx prisma generate
# Uncomment the next line if database migrations are necessary
# npx prisma migrate deploy
else
echo "No changes detected in the repository. Skipping update."
fi
# Clean up the temporary clone directory
rm -rf /tmp/clone
cd /app
echo "Installing packages in /app"
npm install --no-audit --no-fund --no-optional --omit=optional
yes | npx prisma generate
# Uncomment the next line if database migrations are necessary
# npx prisma migrate deploy
echo "Done cloning. Current Git Commit ID: $GIT_COMMIT_ID"
# Uncomment the following lines for production deployment
# npx next build
# npx next start
else
echo "UPDATE_CODE_FROM_GIT is not set to true. Skipping update."
fi
echo "Running the main process"
exec "$@"