-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrelease.sh
executable file
·109 lines (91 loc) · 3.75 KB
/
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
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" # dir of this script
set -e # exit on error
echo -n "Checking dependencies... "
deps="git curl node terraform terraform-docs"
if [ "$(echo $deps | tr ' ' '\n' | wc -l)" != "$(command -v $deps | wc -l)" ]; then
echo -e "ERROR\n\nRequired commands not available: $deps"
exit 1
fi
echo OK
echo -n "Running terraform fmt... "
for dir in $(echo */); do
terraform fmt $dir
done
echo OK
echo -n "Checking for clean working copy... "
if [ "$(git diff-index HEAD)" != "" ]; then
echo -e "ERROR\n\nThere's uncommitted changes in the working copy"
exit 1
fi
echo OK
echo -n "Parsing git remote... "
github_raw="$(git config --get remote.origin.url | sed 's/.*://' | sed 's/\..*//')" # e.g. "[email protected]:user/project.git" => "user/project"
github_user="$(echo "$github_raw" | cut -d / -f 1)"
github_project="$(echo "$github_raw" | cut -d / -f 2)"
if [[ ! "$github_user" =~ ^[[:alnum:]-]+$ ]]; then
echo -e "ERROR\n\nCan't seem to determine GitHub user name reliably: \"$github_user\""
exit 1
fi
if [[ ! "$github_project" =~ ^[[:alnum:]-]+$ ]]; then
echo -e "ERROR\n\nCan't seem to determine GitHub project name reliably: \"$github_project\""
exit 1
fi
echo OK
echo -n "Verifying GitHub API access... "
github_test="$(curl -s -n -o /dev/null -w "%{http_code}" https://api.github.com/user)"
if [ "$github_test" != "200" ]; then
echo -e "ERROR\n\nPlease ensure that:"
echo "* You've set up a Personal access token for the GitHub API (https://github.com/settings/tokens/new)"
echo "* The resulting token is listed in your ~/.netrc file (under \"machine api.github.com\" and \"machine uploads.github.com\")"
exit 1
fi
echo OK
echo -n "Fetching previous tags from GitHub... "
git fetch --tags --quiet
tag_prev="$(git log --tags --simplify-by-decoration --pretty="%D" | grep tag: | cut -d ' ' -f 2 | head -n 1)" # figure out the latest tag (https://stackoverflow.com/a/6900369)
echo OK
echo
echo "Previous release was: $tag_prev"
echo -n "This release will be: "
read tag_next
echo
echo -n "Tagging new release... "
git tag "$tag_next"
echo OK
echo -n "Pushing release to GitHub... "
git push --quiet origin master
git push --quiet origin "$tag_next"
echo OK
echo -n "Creating release on GitHub... " # https://developer.github.com/v3/repos/releases/
curl -o curl-out -s -n -X POST "https://api.github.com/repos/$github_user/$github_project/releases" --data "{\"tag_name\":\"$tag_next\"}"
release_html_url="$(cat curl-out | node -p 'JSON.parse(fs.readFileSync(0)).html_url')"
if [[ ! "$release_html_url" =~ ^https:// ]]; then
echo ERROR
cat curl-out
exit 1
fi
echo OK
echo -n "Updating example code with new release... "
find "$DIR"/* -name README.md | xargs sed -i'.sed-bak' -E "s/\?ref=v[0-9.]+\"/?ref=$tag_next\"/g" # update all "source" links in examples
find "$DIR"/* -name README.md | xargs sed -i'.sed-bak' -E "s#/compare/v[0-9.]+...master#/compare/$tag_next...master#g" # update all "check for updates" links in examples
echo OK
echo -n "Updating Terraform module docs... "
for file in $(find $DIR/* -name README.md); do
perl -i -p0e "s/terraform-docs:begin.*?terraform-docs:end/terraform-docs:begin -->\n$( terraform-docs markdown --hide providers --hide requirements --sort=false $(dirname $file) | sed 's#/#\\/#g' | sed 's#u0026#\&#g' )\n<\!-- terraform-docs:end/s" "$file"
done
echo OK
echo -n "Creating commit from docs updates... "
git add */README.md
git commit --quiet --message "Update docs & examples for $tag_next."
echo OK
echo -n "Pushing updated docs to GitHub... "
git push --quiet origin
echo OK
echo -n "Cleaning up... "
rm -f curl-out # remove curl temp file
find . -name README.md.sed-bak | xargs rm -f # remove sed's backup files
echo OK
echo
echo "New release is: $release_html_url"
echo