-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-fastlane
executable file
·202 lines (171 loc) · 5.26 KB
/
git-fastlane
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/bash
#
# Fastlane utility to create mergeable branches, push to production, etc..
# Place in $PATH
#
# `$ git fastlane --help`
#
# @author dalvizu
#
SCRIPT_VERSION="0.1"
# Trap errors for immediate failout
trap 'trap_and_exit "${BASH_COMMAND}" $?' ERR
function trap_and_exit(){
local LAST_COMMAND=$1
local EXIT_CODE=$2
echo " ! Found an error while processing request:"
echo "Command: ${LAST_COMMAND}"
echo "Exit Code: ${EXIT_CODE}"
exit 1
}
# parse command line args
while [[ $# > 0 ]]
do
key="$1"
case $key in
-d|--dry-run)
DRY_RUN="true"
shift # past argument
;;
-h|--help)
SHOW_HELP="true"
shift # past argument
;;
-v|--version)
PRINT_VERSION="true"
shift # past argument
;;
-b|--branch)
BRANCH="$2"
shift
shift # past argument
;;
-t|--test-jenkins)
USE_TEST_JENKINS="true"
shift # past argument
;;
-f|--force)
FORCE="true"
shift # past argument
;;
*)
# unknown option
;;
esac
done;
function error_and_exit(){
echo " ! [fastlane rejected] ${CURRENT_BRANCH_NAME}"
echo "error: $1"
exit 1
}
function validate_required_version(){
if [[ -n $USE_TEST_JENKINS ]]; then
echo "Using test environment"
local FL_VERSION_URL="https://test-jenkins.example.com"
else
local FL_VERSION_URL="https://jenkins.example.com.com"
fi
local FL_VERSION_URL+="/fastlaneConfig/api/json"
local STATUS=$(curl -s -w %{http_code} $FL_VERSION_URL -o /dev/null)
if [[ $STATUS != "200" ]]; then
error_and_exit "Unable to find required FastLane config script @ ${FL_VERSION_URL} - Aborting. Use -f flag to force. HTTP Response code: ${STATUS}"
fi
local EXPECTED_VERSION=$(curl -s $FL_VERSION_URL --connect-timeout 5 | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["version"]')
if [[ $SCRIPT_VERSION != $EXPECTED_VERSION ]]; then
error_and_exit "Your script version [${SCRIPT_VERSION}] is out of date. You must be running version [${EXPECTED_VERSION}]"
fi
}
# Always Error out if jenkins is requiring a newer version
if [[ -z $FORCE ]]; then
validate_required_version
fi
if [[ -n $PRINT_VERSION ]]; then
echo "git fastlane version: ${SCRIPT_VERSION}"
validate_required_version
exit
fi
if [[ -n $BRANCH ]]; then
echo "fastlane: creating branch $BRANCH from origin/production"
git fetch --prune
BRANCH_NAME=$(git check-ref-format --branch $BRANCH) || error_and_exit "Invalid branch name: '$BRANCH'"
git checkout -b $BRANCH_NAME origin/production
git push -u origin $BRANCH_NAME
echo 'Success'
exit
fi
if [[ -n $SHOW_HELP ]]; then
cat << EOF
usage: git fastlane [-v|--version] [-h|--help] [-d|--dry-run]
[-b|--branch <branchname>] [-f|--force]
Common uses:
To create a new branch for ticket 'SSD-101':
$ git fastlane -b SSD-101
To push your fastlane branch to production:
$ git fastlane
EOF
exit
fi
# Start normal no parameter application
# The git fetch is FIRST to assure you can access the repo while
# this is running.
git fetch --tags
# Build variables
CURRENT_BRANCH_NAME=$(git name-rev HEAD 2> /dev/null | awk "{ print \$2 }")
# Find the current git repo from the output of the 'git remote' command -
# which is unfortunately the only way to do this. Then, we need to remove the
# '.git' from the URL, or else Jenkins will not trigger
RAW_REPO_URL=$(git remote show -n origin | egrep -m1 -o '(ssh://)?[a-zA-Z0-9_-]+@[a-zA-Z0-9\.-]+:[a-zA-Z0-9/_-]+(.git)?')
CURRENT_REPO_URL=${RAW_REPO_URL%.git}
# Start actions
echo "Current branch: ${CURRENT_BRANCH_NAME}"
echo "Current repo: ${CURRENT_REPO_NAME}"
echo "Checking workspace..."
# Check that the branch isn't special branch names
if [[ $CURRENT_BRANCH_NAME == "master" || $CURRENT_BRANCH_NAME == "deploy"
|| $CURRENT_BRANCH_NAME == "test" || $CURRENT_BRANCH_NAME == "production" ]]; then
error_and_exit "You can not run fastlane on the ${CURRENT_BRANCH_NAME} branch"
exit 1
fi
if [[ -n $(git log @{u}..) ]]; then
error_and_exit 'Your local workspace contains commits not yet in origin. Please get these reviewed and commited before using fastlane'
exit 1
fi
#Assume the current branch name is the relevant JIRA ticket to use for
# a git tag
JIRA_TICKET=$CURRENT_BRANCH_NAME
TAGNAME="fastlane-"
TAGNAME+=$USER
TAGNAME+="-"
TAGNAME+=$JIRA_TICKET
echo Finding a unique tag name to push to remote
# We need a unique tag name so start putting numbers on the end
# of it if it exists until we hit a number which doesnt exist yet
i=1
ORIGINAL_TAGNAME=$TAGNAME
while git rev-parse $TAGNAME >/dev/null 2>&1
do
TAGNAME=$ORIGINAL_TAGNAME
TAGNAME+="-"
TAGNAME+=$( printf '%d' $i)
i=$((i + 1))
done
echo "Using tag ${TAGNAME}"
if [[ -n $DRY_RUN ]]; then
echo "Dry run - not pushing"
echo "git tag ${TAGNAME}"
echo "git push origin ${TAGNAME}"
else
git tag $TAGNAME
git push origin $TAGNAME
fi
echo Force polling on commit stage...
sleep 5s
if [[ -n $USE_TEST_JENKINS ]]; then
echo "Using test environment"
TRIGGER_URL="https://test-jenkins.example.com/git/notifyCommit?url=${CURRENT_REPO_URL}"
else
TRIGGER_URL="https://jenkins.example.com/git/notifyCommit?url=${CURRENT_REPO_URL}"
fi
CURL_JENKINS_OUTPUT=$(curl --fail --silent $TRIGGER_URL)
echo -e "Forced poll @ ${TRIGGER_URL}\n# Jenkins Response\n${CURL_JENKINS_OUTPUT}\n# End"
echo Success