-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-final-public-release.sh
executable file
·187 lines (161 loc) · 5.85 KB
/
create-final-public-release.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
181
182
183
184
185
186
187
#!/usr/bin/env bash
# set -o pipefail # exit if pipe command fails
[ -z "$DEBUG" ] || set -x
set -e
##
RELEASE="cf-logging"
DESCRIPTION="CF logging Bosh Release"
GITHUB_REPO="SpringerPE/cf-logging-boshrelease"
# Do not define this variable if the repo uses git lfs
# https://starkandwayne.com/blog/bosh-releases-with-git-lfs/
RELEASE_BUCKET="cf-logging-boshrelease"
###
BOSH_CLI=${BOSH_CLI:-bosh}
S3CMD=${S3CMD:-s3cmd}
JQ=jq
CURL="curl -s"
SHA1="sha1sum -b"
RE_VERSION_NUMBER='^[0-9]+([0-9\.]*[0-9]+)*$'
# Create a personal github token to use this script
if [ -z "$GITHUB_TOKEN" ]
then
echo "Github TOKEN not defined!"
echo "See https://help.github.com/articles/creating-an-access-token-for-command-line-use/"
exit 1
fi
# You need bosh installed and with you credentials
if ! [ -x "$(command -v $BOSH_CLI)" ]
then
echo "ERROR: $BOSH_CLI command not found! Please install it and make it available in the PATH"
exit 1
fi
# You need jq installed
if ! [ -x "$(command -v $JQ)" ]
then
echo "ERROR: $JQ command not found! Please install it and make it available in the PATH"
exit 1
fi
# You need sha1sum installed (brew md5sha1sum)
if ! [ -x "$(command -v $SHA1)" ]
then
echo "ERROR: $SHA1 command not found! Please install it and make it available in the PATH"
exit 1
fi
if [ -n "$RELEASE_BUCKET" ]
then
# You need s3cmd installed and with you credentials
if ! [ -x "$(command -v $S3CMD)" ]
then
echo "$S3CMD command not found!"
exit 1
fi
# Checking if the bucket is there. If not, create it first (this just a check to
# be sure that s3cmd is properly setup and with the correct credentials
if ! $S3CMD ls | grep -q "s3://$RELEASE_BUCKET"
then
echo "Bucket 's3://$RELEASE_BUCKET' not found! Please create it first!"
exit 1
fi
fi
case $# in
0)
echo "*** Creating a new release. Automatically calculating next release version number"
;;
1)
if [ $1 == "-h" ] || [ $1 == "--help" ]
then
echo "Usage: $0 [version-number]"
echo " Creates a new boshrelease, commits the changes to this repository using tags and uploads "
echo " the release to Github releases. It also adds comments based on previous git commits and "
echo " calculates the sha1 checksum."
exit 0
else
version=$1
if ! [[ $version =~ $RE_VERSION_NUMBER ]]
then
echo "ERROR: Incorrect version number!"
exit 1
fi
echo "*** Creating a new release. Using release version number $version."
fi
;;
*)
echo "ERROR: incorrect argument. See '$0 --help'"
exit 1
;;
esac
# Get the last git commit made by this script
lastcommit=$(git log --format="%h" --grep="$RELEASE v*" | head -1)
echo "* Changes since last version with commit $lastcommit: "
git_changes=$(git log --pretty="%h %aI %s (%an)" $lastcommit..@ | sed 's/^/- /')
if [ -z "$git_changes" ]
then
echo "Error, no commits since last release with commit $lastcommit!. Please "
echo "commit your changes to create and publish a new release!"
exit 1
fi
echo "$git_changes"
# Uploading blobs
echo "* Uploading blobs to the blobstore ..."
$BOSH_CLI upload-blobs
# Creating the release
if [ -z "$version" ]
then
echo "* Creating final release ..."
$BOSH_CLI create-release --force --final --tarball="/tmp/$RELEASE-$$.tgz" --name "$RELEASE"
# Get the version of the release
version=$(ls releases/$RELEASE/$RELEASE-*.yml | sed 's/.*\/.*-\(.*\)\.yml$/\1/' | sort -rn | head -1)
else
echo "* Creating final release version $version ..."
$BOSH_CLI create-release --force --final --tarball="/tmp/$RELEASE-$$.tgz" --name "$RELEASE" --version "$version"
fi
# Create a release in Github
sha1=$($SHA1 "/tmp/$RELEASE-$$.tgz" | cut -d' ' -f1)
cat <<EOF > manifest/vars-release-version.yml
releases:
- name: $RELEASE
url: https://github.com/${GITHUB_REPO}/releases/download/v${version}/${RELEASE}-${version}.tgz
version: $version
sha1: $sha1
EOF
# Create a new tag and update the changes
echo "* Commiting git changes ..."
git add .final_builds releases/$RELEASE/index.yml "releases/$RELEASE/$RELEASE-$version.yml" config/blobs.yml manifest/vars-release-version.yml
git commit -m "$RELEASE v$version Boshrelease"
git push
git push --tags
# Create a release in Github
echo "* Creating a new release in Github ... "
sha1=$($SHA1 "/tmp/$RELEASE-$$.tgz" | cut -d' ' -f1)
description=$(cat <<EOF
# $RELEASE version $version
$DESCRIPTION
## Changes since last version
$git_changes
## Using in a bosh Deployment
releases:
- name: $RELEASE
url: https://github.com/${GITHUB_REPO}/releases/download/v${version}/${RELEASE}-${version}.tgz
version: $version
sha1: $sha1
EOF
)
printf -v DATA '{"tag_name": "v%s","target_commitish": "master","name": "v%s","body": %s,"draft": false, "prerelease": false}' "$version" "$version" "$(echo "$description" | $JQ -R -s '@text')"
releaseid=$($CURL -H "Authorization: token $GITHUB_TOKEN" -H "Content-Type: application/json" -XPOST --data "$DATA" "https://api.github.com/repos/$GITHUB_REPO/releases" | $JQ '.id')
# Upload the release
echo "* Uploading tarball to Github releases section ... "
echo -n " URL: "
$CURL -H "Authorization: token $GITHUB_TOKEN" -H "Content-Type: application/octet-stream" --data-binary @"/tmp/$RELEASE-$$.tgz" "https://uploads.github.com/repos/$GITHUB_REPO/releases/$releaseid/assets?name=$RELEASE-$version.tgz" | $JQ -r '.browser_download_url'
# Delete the release
rm -f "/tmp/$RELEASE-$$.tgz"
if [ -n "$RELEASE_BUCKET" ]
then
# Update ACLs on bucket
echo "* Assigning public ACL to new blobs in bucket ... "
$S3CMD setacl "s3://${RELEASE_BUCKET}" --acl-public --recursive
fi
echo
echo "*** Description https://github.com/$GITHUB_REPO/releases/tag/v$version: "
echo
echo "$description"
exit 0