Skip to content

Instantly share code, notes, and snippets.

@JonathanXDR
Last active March 3, 2024 12:41
Show Gist options
  • Save JonathanXDR/0ec17309cf06162bc76134840637d2fd to your computer and use it in GitHub Desktop.
Save JonathanXDR/0ec17309cf06162bc76134840637d2fd to your computer and use it in GitHub Desktop.
A shell script to manage build triggers on Vercel by checking the time difference between the last two commits on a GitHub repository.
#!/bin/bash
GITHUB_TOKEN=$VITE_GITHUB_TOKEN
GITHUB_REPO_BRANCH=${GITHUB_REPO_BRANCH:-develop}
BUILD_INTERVAL_MINUTES=${BUILD_INTERVAL_MINUTES:-30}
commits_info=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
"https://api.github.com/repos/$VITE_GITHUB_REPO_OWNER/$VITE_GITHUB_REPO_NAME/commits?sha=$GITHUB_REPO_BRANCH&per_page=2")
echo -e "$commits_info\n"
commit_timestamps=$(echo "$commits_info" | grep '"date":' | cut -d '"' -f 4 | awk 'NR % 2 == 1')
readarray -t timestamps <<<"$commit_timestamps"
echo -e "Latest Commit Timestamp: ${timestamps[0]}"
echo -e "Previous Commit Timestamp: ${timestamps[1]}\n"
latest_commit_unix=$(date -d "${timestamps[0]}" +%s)
second_latest_commit_unix=$(date -d "${timestamps[1]}" +%s)
minutes_diff=$((($latest_commit_unix - $second_latest_commit_unix) / 60))
echo -e "Time difference between the last two commits: $minutes_diff minutes\n"
if [ $minutes_diff -ge $BUILD_INTERVAL_MINUTES ]; then
echo -e "✅ - More than $BUILD_INTERVAL_MINUTES minutes between the last two commits, proceeding with build\n"
exit 1
else
echo -e "🛑 - Less than $BUILD_INTERVAL_MINUTES minutes between the last two commits, skipping build\n"
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment