Skip to content

Instantly share code, notes, and snippets.

@strayer
Created August 24, 2024 20:31
Show Gist options
  • Save strayer/cf7c59aba97736532b0aa281f2df5cf3 to your computer and use it in GitHub Desktop.
Save strayer/cf7c59aba97736532b0aa281f2df5cf3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
# Set the path to your local repository
readonly REPO_PATH="/path/to/your/local/repository"
# Set the log file path
readonly LOG_FILE="/path/to/your/logfile.log"
# Function to log messages
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
}
# Change to the repository directory
cd "$REPO_PATH" || { log_message "Failed to change to repository directory"; exit 1; }
# Perform git pull
log_message "Performing git pull"
if ! git pull >> "$LOG_FILE" 2>&1; then
log_message "Git pull failed"
exit 1
fi
# Get list of changed files
mapfile -t changed_files < <(git diff --name-only HEAD@{1} HEAD)
# Get unique first-level directories of changed files
mapfile -t changed_dirs < <(printf '%s\n' "${changed_files[@]}" | cut -d'/' -f1 | sort -u)
# Iterate over changed directories
for dir in "${changed_dirs[@]}"; do
if [[ -d "$dir" && (-f "$dir/compose.yaml" || -f "$dir/compose.yml") ]]; then
log_message "Processing directory: $dir"
# Change to the subdirectory
if ! cd "$dir"; then
log_message "Failed to change to directory: $dir"
continue
fi
# Run docker compose pull
log_message "Running docker compose pull in $dir"
if ! docker compose pull >> "$LOG_FILE" 2>&1; then
log_message "Docker compose pull failed in $dir"
fi
# Run docker compose up
log_message "Running docker compose up in $dir"
if ! docker compose up -d --remove-orphans --force-recreate >> "$LOG_FILE" 2>&1; then
log_message "Docker compose up failed in $dir"
fi
# Change back to the repository root
if ! cd "$REPO_PATH"; then
log_message "Failed to change back to repository root"
exit 1
fi
else
log_message "Skipping directory $dir (no compose file or not a directory)"
fi
done
log_message "Script execution completed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment