From 33ff2c2d1f4e6e2b205245729eec975169be770e Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Fri, 23 Feb 2024 18:17:23 +0200 Subject: [PATCH] update entrypoint --- _deploy/entrypoint.old.sh | 35 +++++++++++++++++++++++++++++++++++ _deploy/entrypoint.sh | 30 ++++++++++++++++++++---------- 2 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 _deploy/entrypoint.old.sh diff --git a/_deploy/entrypoint.old.sh b/_deploy/entrypoint.old.sh new file mode 100644 index 0000000..7f71d21 --- /dev/null +++ b/_deploy/entrypoint.old.sh @@ -0,0 +1,35 @@ +#!/bin/sh + +if [ "$UPDATE_CODE_FROM_GIT" = "true" ]; then + apk add git nano rsync + echo "Updating code from git.d-popov.com...(as '$GIT_USERNAME')" + rm -rf /tmp/clone + mkdir /tmp/clone + + git clone -b ${GIT_BRANCH:-master} --depth 1 https://$GIT_USERNAME:${GIT_PASSWORD//@/%40}@git.d-popov.com/popov/next-cart-app.git /tmp/clone || exit 1 + GIT_COMMIT_ID=$(git -C /tmp/clone/next-cart-app rev-parse HEAD) + LAST_COMMIT_MESSAGE=$(git log -1 --pretty=%B) + echo "Current Git Commit: $LAST_COMMIT_MESSAGE: $GIT_COMMIT_ID" + export GIT_COMMIT_ID + + # Use rsync for synchronizing files, ensuring deletion of files not in source + rsync -av --delete /tmp/clone/next-cart-app/ /app/ || echo "Rsync failed: Issue synchronizing files" + rsync -av /tmp/clone/next-cart-app/.env* /app/ || echo "Rsync failed: Issue copying .env files" + rsync -av /tmp/clone/next-cart-app/_deploy/entrypoint.sh /app/entrypoint.sh || echo "Rsync failed: Issue copying entrypoint.sh" + chmod +x /app/entrypoint.sh + # Consider uncommenting the next line to clean up after successful copy + 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 + #npx prisma migrate deploy + echo "Done cloning. Current Git Commit ID: $GIT_COMMIT_ID" + # prod script + # npx next build + # npx next start +fi + +echo "Running the main process" +exec "$@" diff --git a/_deploy/entrypoint.sh b/_deploy/entrypoint.sh index 7f71d21..571d72e 100644 --- a/_deploy/entrypoint.sh +++ b/_deploy/entrypoint.sh @@ -1,32 +1,42 @@ #!/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')" + + # Remove the previous clone directory to ensure a fresh start rm -rf /tmp/clone mkdir /tmp/clone - git clone -b ${GIT_BRANCH:-master} --depth 1 https://$GIT_USERNAME:${GIT_PASSWORD//@/%40}@git.d-popov.com/popov/next-cart-app.git /tmp/clone || exit 1 - GIT_COMMIT_ID=$(git -C /tmp/clone/next-cart-app rev-parse HEAD) - LAST_COMMIT_MESSAGE=$(git log -1 --pretty=%B) + # Clone the specific branch of the new repository + git clone -b ${GIT_BRANCH:-main} --depth 1 https://$GIT_USERNAME:${GIT_PASSWORD//@/%40}@git.d-popov.com/popov/mwhitnessing.git /tmp/clone || exit 1 + # Fetch the latest commit ID and message from the cloned repository + GIT_COMMIT_ID=$(git -C /tmp/clone rev-parse HEAD) + LAST_COMMIT_MESSAGE=$(git -C /tmp/clone log -1 --pretty=%B) echo "Current Git Commit: $LAST_COMMIT_MESSAGE: $GIT_COMMIT_ID" export GIT_COMMIT_ID - # Use rsync for synchronizing files, ensuring deletion of files not in source - rsync -av --delete /tmp/clone/next-cart-app/ /app/ || echo "Rsync failed: Issue synchronizing files" - rsync -av /tmp/clone/next-cart-app/.env* /app/ || echo "Rsync failed: Issue copying .env files" - rsync -av /tmp/clone/next-cart-app/_deploy/entrypoint.sh /app/entrypoint.sh || echo "Rsync failed: Issue copying entrypoint.sh" + # 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 - # Consider uncommenting the next line to clean up after successful copy + + # 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 - #npx prisma migrate deploy + # Uncomment the next line if database migrations are necessary + # npx prisma migrate deploy echo "Done cloning. Current Git Commit ID: $GIT_COMMIT_ID" - # prod script + # Uncomment the following lines for production deployment # npx next build # npx next start fi