Skip to content

Instantly share code, notes, and snippets.

@thecist
Last active June 29, 2025 06:25
Show Gist options
  • Select an option

  • Save thecist/56caf48813bd23ee00615368064ea1a3 to your computer and use it in GitHub Desktop.

Select an option

Save thecist/56caf48813bd23ee00615368064ea1a3 to your computer and use it in GitHub Desktop.
The core script used to deploy thecist's repos
#!/usr/bin/env bash
set -e
REPO_DIR="$1"
TARGET_DIR="$2"
PROJECT_NAME="$3"
BRANCH="$4"
if [[ -z "$REPO_DIR" || -z "$TARGET_DIR" || -z "$PROJECT_NAME" || -z "$BRANCH" ]]; then
echo "Usage: _undeploy-core <repo_dir> <target_dir> <project_name> <branch>"
exit 1
fi
if [[ "$BRANCH" == "main" ]]; then
echo "ERROR: Cannot undeploy the 'main' branch. Aborting."
exit 1
fi
echo "Undeploying project: $PROJECT_NAME"
# Stop and remove containers
echo "Stopping containers for project $PROJECT_NAME..."
docker compose --project-name "$PROJECT_NAME" down || echo "Container already stopped or missing"
# Remove Docker image(s)
echo "Removing Docker images for project $PROJECT_NAME..."
docker images --format "{{.Repository}}:{{.Tag}} {{.ID}}" | grep "$PROJECT_NAME" | awk '{print $2}' | xargs -r docker rmi || true
# Delete deployment folder
if [[ -d "$TARGET_DIR" ]]; then
echo "Removing target directory: $TARGET_DIR"
rm -rf "$TARGET_DIR"
else
echo "Target directory $TARGET_DIR not found"
fi
# Delete branch from the bare repo
echo "Deleting branch '$BRANCH' from $REPO_DIR"
git --git-dir="$REPO_DIR" update-ref -d "refs/heads/$BRANCH"
echo "Undeployment complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment