22#
33# Tools for deploying our release to github
44#
5- # Usage: ./deploy.sh deploy [-n] [-r REMOTE_REPOS] [RELEASE_VERSION]
6- # ./deploy.sh build
5+ # Usage: ./deploy.sh deploy [-n] [-r REMOTE_REPOS] [--replace] [ RELEASE_VERSION]
6+ # ./deploy.sh build [RELEASE_VERSION]
77#
88# Options:
99# -n Dry run, show with version, files and description
1010# -r REMOTE_REPOS Specify a REMOTE_REPOS name [default: origin]
11+ # --replace Replace existing release with this one, previous release
12+ # will be deleted first.
1113#
1214# Arguments:
1315# RELEASE_VERSION a git tag
1416#
1517# Actions:
16- # build only build using gox and deployment.yml config
18+ # build only build using gox and deployment.yml config
19+ # deploy prepare and deploy the release
1720#
1821# deploy.sh read description in deployment.yml
1922
@@ -26,27 +29,53 @@ BUILD_DEST_DIR=build
2629
2730create_release ()
2831{
32+ local release=" $1 "
33+ local name=" $2 "
34+ local description=" $3 "
35+
36+ # TODO: detect alpha ==> pre-release
2937 gothub release \
3038 --user $GITHUB_USER \
3139 --repo $GITHUB_REPO \
32- --tag $TAG \
33- --name " docopt for shell Bash " \
34- --description " Written in Go. This binaries is for GNU/Linux 32bits and 64bits " \
40+ --tag " $release " \
41+ --name " $name " \
42+ --description " $description " \
3543 --pre-release
3644}
3745
46+ check_release ()
47+ {
48+ local release=$1
49+ gothub info \
50+ --user $GITHUB_USER \
51+ --repo $GITHUB_REPO \
52+ --tag " $release " > /dev/null 2>&1
53+ }
54+
55+ delete_release ()
56+ {
57+ local release=$1
58+ gothub delete \
59+ --user $GITHUB_USER \
60+ --repo $GITHUB_REPO \
61+ --tag " $release "
62+ }
63+
3864prepare_upload ()
3965{
4066 local build_dest_dir=$1
4167 pushd $build_dest_dir > /dev/null
42- rm -f sha256sum.txt
68+ # remove docopts source used for build
69+ rm -f sha256sum.txt docopts.go
4370 sha256sum * > sha256sum.txt
4471 popd > /dev/null
4572 find $build_dest_dir -type f -a ! -name .\*
4673}
4774
4875upload_binaries ()
4976{
77+ local release=$1
78+ shift
5079 local filenames=$*
5180
5281 local f
@@ -56,9 +85,9 @@ upload_binaries()
5685 gothub upload \
5786 --user $GITHUB_USER \
5887 --repo $GITHUB_REPO \
59- --tag $TAG \
60- --name " $f " \
61- --file $f \
88+ --tag " $release " \
89+ --name " $( basename $f ) " \
90+ --file " $f " \
6291 --replace
6392 done
6493}
@@ -86,59 +115,96 @@ get_arch_build_target()
86115
87116build_binaries ()
88117{
89- gox -osarch " $( get_arch_build_target gox) " -output=" $BUILD_DEST_DIR /{{.Dir}}_{{.OS}}_{{.Arch}}"
118+ local release=$1
119+ local build_dest_dir=$2
120+ # checkout release version
121+ git show $release :./docopts.go > $build_dest_dir /docopts.go
122+ local osarch=" $( get_arch_build_target gox) "
123+ pushd $build_dest_dir > /dev/null
124+ gox -osarch " $osarch " -output=" docopts_{{.OS}}_{{.Arch}}"
125+ popd > /dev/null
90126}
91127
92128yaml_keys ()
93129{
94130 yq.v2 r " $1 " " $2 " | sed -n -e ' /^\([^ ]\([^:]\+\)\?\):/ s/:.*// p'
95131}
96132
97- main_deploy ()
133+ show_release_data ()
98134{
99- # redefine GITHUB_TOKEN to test if exported for strict mode
100- GITHUB_TOKEN=${GITHUB_TOKEN:- }
101-
102- if [[ -n $ARGS_RELEASE_VERSION ]] ; then
103- TAG=$ARGS_RELEASE_VERSION
104- else
105- # fetch last tag from git
106- TAG=$( git describe --abbrev=0)
107- fi
135+ local release=$1
136+ local name=" $2 "
137+ local description=" $3 "
108138
109- repository=$( git remote -v | grep $ARGS_REMOTE_REPOS | grep push | head -1)
110- description=$( yq.v2 r $DEPLOYMENT_FILE " releases[$TAG ].description" )
111- if [[ -z $description || $description == null ]] ; then
112- echo " description not found for tag '$TAG ' in $DEPLOYMENT_FILE "
113- echo " available git tags: ($repository )"
114- indent " $( git tag) "
115- echo " available git tags in $DEPLOYMENT_FILE :"
116- indent " $( yaml_keys $DEPLOYMENT_FILE releases) "
117- return 1
118- fi
119-
120- build_binaries
121- UPLOAD_FILES=$( prepare_upload $BUILD_DEST_DIR )
139+ local repository=$( git remote -v | grep $ARGS_REMOTE_REPOS | grep push | head -1)
122140
123- if $ARGS_n ; then
124- cat << EOT
141+ cat << EOT
125142GITHUB_TOKEN: $GITHUB_TOKEN
126143build_dir: $BUILD_DEST_DIR
127144repository: $repository
128- tag: $TAG
145+ name: $name
146+ tag: $release
129147files: $UPLOAD_FILES
130148sha256sum.txt:
131149$( indent $BUILD_DEST_DIR /sha256sum.txt)
132150description:
133151$( indent " $description " )
134152EOT
153+ }
154+
155+ check_name_description ()
156+ {
157+ local name=" $1 "
158+ local description=" $2 "
159+ if [[ -z $description || $description == null || -z $name || $name == null ]] ; then
160+ echo " description or name not found for tag '$TAG ' in $DEPLOYMENT_FILE "
161+ echo " available git tags:"
162+ indent " $( git tag) "
163+ echo " available git tags in $DEPLOYMENT_FILE :"
164+ indent " $( yaml_keys $DEPLOYMENT_FILE releases) "
165+ return 1
166+ fi
167+ }
168+
169+ main_deploy ()
170+ {
171+ # redefine GITHUB_TOKEN to test if exported for strict mode
172+ GITHUB_TOKEN=${GITHUB_TOKEN:- }
173+
174+ local description=$( yq.v2 r $DEPLOYMENT_FILE " releases[$TAG ].description" )
175+ local name=$( yq.v2 r $DEPLOYMENT_FILE " releases[$TAG ].name" )
176+
177+ check_name_description " $name " " $description "
178+
179+ build_binaries $TAG $BUILD_DEST_DIR
180+ UPLOAD_FILES=$( prepare_upload $BUILD_DEST_DIR )
181+
182+ if $ARGS_n ; then
183+ show_release_data $TAG " $name " " $description "
135184 exit 0
136185 else
137186 if [[ -z $GITHUB_TOKEN ]] ; then
138187 echo " GITHUB_TOKEN must be exported"
139188 return 1
140189 fi
141- upload_binaries $UPLOAD_FILES
190+
191+ if check_release $TAG ; then
192+ echo " release: already $TAG exists"
193+ if $ARGS_replace ; then
194+ echo " deleting existing release $TAG "
195+ delete_release $TAG
196+ echo " creating release $TAG "
197+ create_release $TAG " $name " " $description "
198+ else
199+ echo " use --replace to replace the existing release"
200+ echo " only upload new files"
201+ fi
202+ else
203+ echo " release: $TAG doesn't exists yet"
204+ echo " creating release $TAG ..."
205+ create_release $TAG " $name " " $description "
206+ fi
207+ upload_binaries $TAG $UPLOAD_FILES
142208 fi
143209}
144210
@@ -155,10 +221,17 @@ if [[ $0 == $BASH_SOURCE ]] ; then
155221
156222 docopt_print_ARGS -G
157223
224+ if [[ -n $ARGS_RELEASE_VERSION ]] ; then
225+ TAG=$ARGS_RELEASE_VERSION
226+ else
227+ # fetch last tag from git
228+ TAG=$( git describe --abbrev=0)
229+ fi
230+
158231 if $ARGS_build ; then
159- # build only
232+ echo " build only ... "
160233 echo " dest build dir: $BUILD_DEST_DIR /"
161- build_binaries
234+ build_binaries $TAG $BUILD_DEST_DIR
162235 ls -l $BUILD_DEST_DIR
163236 exit 0
164237 elif [[ $ARGS_deploy ]] ; then
0 commit comments