Last active
July 13, 2017 12:34
-
-
Save krishnamurthydasari/3ab99eb1308f9ef940909d059eecc38e to your computer and use it in GitHub Desktop.
Github repository to s3 bucket sync and send email with new updates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#Date : 13-07-2017 | |
#Owner : Krishnamurthy Dasari | |
#Description: Pull the changes from upstreem to local git repository, upload all changes to s3 bucket and send email alert with changed files. | |
GitLocalRepoDir="/path/to/local/directory" | |
s3bucket="s3://BucketName" | |
email_subject="Subject" | |
email_From="From@domain.com" | |
email_TO="To@domain.com" | |
export AWS_ACCESS_KEY_ID="xxxxxxxxxxxxxxx" | |
export AWS_SECRET_ACCESS_KEY="xxxxxxxxxxxxxxxxxx" | |
PIDFILE="/opt/scripts/github_s3Bucket_sync.pid" | |
if [ -e "${PIDFILE}" ] && (ps -p $(cat ${PIDFILE}) > /dev/null); then | |
echo "Process is already running. Skipping this run." | |
exit 99 | |
fi | |
echo $$ > ${PIDFILE} | |
cd $GitLocalRepoDir | |
git config user.name "Git Username" | |
git config user.email "Git Email id" | |
git fetch | |
git pull | |
git diff-tree -r ORIG_HEAD.. > /tmp/git-updates.out 2>&1 | |
if [ $? -eq 0 ]; | |
then | |
echo "Git pull has been completed.." | |
all_files=`cat /tmp/git-updates.out |awk '{print $NF}'` | |
files_added=`cat /tmp/git-updates.out |awk '{ if ($5 == "A") print $NF}'` | |
files_modified=`cat /tmp/git-updates.out |awk '{ if ($5 == "M") print $NF}'` | |
files_deleted=`cat /tmp/git-updates.out |awk '{ if ($5 == "D") print $NF}'` | |
if [[ ! -z "$all_files" ]]; | |
then | |
aws s3 sync $GitLocalRepoDir/ $s3bucket/ | |
echo -e "Below are files added at $s3bucket: \n\n\n$files_added\n\n\n\ | |
Below are files modified at $s3bucket: \n\n\n$files_modified\n\n\n\ | |
Below are files deleted at github: \n\n\n$files_deleted" | mail -s "$email_subject" -r $email_From $email_TO | |
fi | |
fi | |
rm $PIDFILE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment