forked from dennyzhang/devops_public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_update.sh
executable file
·80 lines (73 loc) · 2.62 KB
/
git_update.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
#!/bin/bash -e
##-------------------------------------------------------------------
## @copyright 2016 DennyZhang.com
## Licensed under MIT
## https://raw.githubusercontent.com/DennyZhang/devops_public/tag_v1/LICENSE
##
## File : git_update.sh
## Author : Denny <[email protected]>
## Description :
## --
## Created : <2016-04-15>
## Updated: Time-stamp: <2017-09-04 18:54:43>
##-------------------------------------------------------------------
working_dir=${1?}
git_repo_url=${2?}
branch_name=${3?}
function parse_git_repo() {
# [email protected]:MYORG/mydevops.wiki.git -> mdmdevops.wiki
# [email protected]:MYORG/mydevops.git/wiki -> wiki
# [email protected]:MYORG/mydevops.git -> mydevops
local git_url=${1?}
local repo_name=""
if [[ "$git_url" = *:*/*.*.git ]]; then
if [[ "$git_url" = https:*.* ]]; then
# Sample: https://github.com/DennyZhang/devops_public.git
repo_name=$(echo "${git_url%.git}" | awk -F'/' '{print $5}')
else
# Sample: [email protected]:DennyZhang/devops_public.git
repo_name=$(echo "${git_url%.git}" | awk -F'/' '{print $2}')
fi
else
if [[ "$git_url" = *:*/*.git/* ]]; then
repo_name=$(echo "${git_url}" | awk -F'/' '{print $3}')
else
if [[ "$git_url" = *:*/*.git ]]; then
repo_name=$(echo "${git_url%.git}" | awk -F'/' '{print $2}')
fi
fi
fi
echo "$repo_name"
}
function git_update_code() {
set -e
local branch_name=${1?}
local working_dir=${2?}
local git_repo_url=${3?}
git_repo=$(parse_git_repo "$git_repo_url")
git_repo=${git_repo##*\/}
local code_dir="$working_dir/$branch_name/$git_repo"
echo "Git update code for $git_repo_url to $code_dir"
# checkout code, if absent
if [ ! -d "$working_dir/$branch_name/$git_repo" ]; then
mkdir -p "$working_dir/$branch_name"
cd "$working_dir/$branch_name"
git clone --depth 1 "$git_repo_url" --branch "$branch_name" --single-branch
cd "$code_dir"
git config user.email "[email protected]"
git config user.name "Jenkins Auto"
else
cd "$code_dir"
git ls-remote --tags
git config remote.origin.url "$git_repo_url"
git config user.email "[email protected]"
git config user.name "Jenkins Auto"
# add retry for network turbulence
git pull origin "$branch_name" || (sleep 2 && git pull origin "$branch_name")
fi
cd "$code_dir"
git checkout "$branch_name"
# git reset --hard
}
git_update_code "$branch_name" "$working_dir" "$git_repo_url"
## File : git_update.sh ends