-
Notifications
You must be signed in to change notification settings - Fork 0
/
artifact.sh
executable file
·181 lines (127 loc) · 2.81 KB
/
artifact.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/bin/sh
# This bash script is created to facilitate local git repository uploads to Amazon S3 buckets
# Defining Global Variables
S3_BUCKET_NAME=""
AWS_ACCESS_KEY=""
AWS_SECRET_ACCESS_KEY=""
REPO_NAME=""
BRANCH=""
FILE_NAME=""
# Function Definitions
# Firt Funtion that reads user inputs from command prompt
# User must supply s3 bucket name, access key and secret access key
get_parameters()
{
echo ""
read -p "Enter your S3 Bucket Name : " S3_BUCKET_NAME
echo ""
read -s -p "Enter your Access Key : " AWS_ACCESS_KEY
echo ""
echo ""
read -s -p "Enter your Secret Access Key : " AWS_SECRET_ACCESS_KEY
echo ""
}
# Second Fuction exports user provided access key and secret access key into environment variables
export_env_variables()
{
echo ""
echo "Exporting Credentials..."
export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY
export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
#echo ""
#echo "AWS_ACCESS_KEY_ID : $AWS_ACCESS_KEY_ID"
#echo ""
#echo "AWS_SECRET_ACCESS_KEY : $AWS_SECRET_ACCESS_KEY"
#echo ""
}
# Third Function gets the repo name by extracting users root folder name
get_repo_name()
{
echo ""
echo "Getting Repo Name..."
echo ""
#REPO_NAME=${PWD##*/}
REPO_NAME=$(git remote get-url origin | sed 's%^.*/\([^/]*\)\.git$%\1%g')
echo "$REPO_NAME"
}
# Fourth Function extracts the branch name from the git branch command
get_branch()
{
echo ""
echo "Getting Branch Name..."
echo ""
BRANCH=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
echo "$BRANCH"
}
# Fifth Fuction pull the changes from the git repository
pull_changes()
{
echo ""
echo "Pulling Changes from GIT repository..."
echo ""
git pull
if [ $? -eq 0 ]; then
echo ""
echo "Completed Pulling Changes..."
else
echo ""
echo "Failed to Pull Changes..."
echo ""
exit 1
fi
}
# Sixth Function compresses the git repo into a tar ball
compress_repo()
{
echo ""
echo "Compressing your Repository..."
echo ""
HASH=$(git rev-parse --short HEAD)
FILE_NAME=$REPO_NAME-$BRANCH-$HASH.tar.gz
COMMAND="tar cvzf $FILE_NAME *"
echo "Executing Tar Command : $COMMAND"
$COMMAND 2>/dev/null
if [ $? -eq 0 ]; then
echo ""
echo "Completed Compression..."
echo ""
else
echo ""
echo "Failed to Compress repo..."
echo ""
exit 1
fi
}
# Seventh Funtion uploads the tar ball to the specified S3 bucket
upload_to_s3()
{
echo "Uploading your repository to S3 bucket : $S3_BUCKET_NAME "
echo ""
aws s3 cp $FILE_NAME s3://$S3_BUCKET_NAME
if [ $? -eq 0 ]; then
echo ""
echo "Completed uploading to S3 bucket..."
echo ""
else
echo ""
echo "Failed to upload to S3 bucket..."
echo ""
exit 1
fi
}
create_json()
{
#rm artifact.json
touch artifact.json
CURRENT_DATE=$(date)
echo " \"created_at\": $CURRENT_DATE} " > artifact.json
}
#Function calls
get_parameters
export_env_variables
get_repo_name
get_branch
pull_changes
compress_repo
upload_to_s3
create_json