-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrelease.sh
executable file
·132 lines (111 loc) · 4.57 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash
#
# This scripts generates a new Jou release. It is invoked from GitHub Actions
# automatically as documented in CONTRIBUTING.md.
set -e -o pipefail
dry_run=no
auth_header=""
while [ $# != 0 ]; do
case "$1" in
--dry-run)
dry_run=yes
shift
;;
--github-token)
auth_header="Authorization: token $2"
shift 2
;;
*)
echo "Usage: $0 [--github-token TOKEN] [--dry-run]"
echo ""
echo "With --dry-run, no release is made. This can be used to develop many"
echo "features of this script."
echo ""
echo "Passing a GitHub API token may be useful even with --dry-run. GitHub API"
echo "is used to check which pull requests have the skip-release label, and you"
echo "may run into rate-limit issues if you don't specify a token."
exit 2
;;
esac
done
echo "Generating release description"
rm -rvf tmp/release
mkdir -vp tmp/release
cd tmp/release
# Make sure git knows about the latest tags in github
git fetch --tags -q
latest_tag=$(git describe --tags --abbrev=0)
for commit in $(git log --pretty=%H $latest_tag..origin/main --reverse); do
# Get first line of commit message (the summary line)
summary="$(git show -s --format=%s $commit)"
echo "Found commit ${commit:0:10}: $summary"
# Parse PR number
if ! [[ $summary =~ \(\#([0-9]+)\)$ ]]; then
echo " Skipping because commit is not associated with a pull request."
continue
fi
pr_number=${BASH_REMATCH[1]}
if curl -s -H "$auth_header" https://api.github.com/repos/Akuli/jou/pulls/$pr_number | jq -r '.labels[].name' | grep -q '^skip-release$'; then
echo " Skipping because pull request #$pr_number has the 'skip-release' label"
continue
fi
if ! [ -f desc.md ]; then
echo "This release contains the following changes:" > desc.md
fi
echo "- [$summary](https://github.com/Akuli/jou/pull/$pr_number)" >> desc.md
done
if ! [ -f desc.md ]; then
echo "No changes to release. Stopping."
exit 0
fi
echo ""
cat desc.md
echo ""
echo "Finding latest Windows CI run for main branch..."
commit=$(git rev-parse origin/main)
curl -s -H "$auth_header" "https://api.github.com/repos/Akuli/jou/actions/runs?branch=main&head_sha=$commit" | jq '.workflow_runs[] | select(.path==".github/workflows/windows.yml")' > run.json
echo " Run ID: $(jq -r .id < run.json)"
echo " Status: $(jq -r .status < run.json)"
echo " Artifacts URL: $(jq -r .artifacts_url < run.json)"
echo ""
if [ $(jq -r .status < run.json) != completed ]; then
echo "Seems like CI is still running. Not releasing."
exit 0
fi
echo "Finding windows-zip artifact..."
archive_download_url="$(curl -s -H "$auth_header" "$(jq -r .artifacts_url < run.json)" | jq -r '.artifacts[] | select(.name == "windows-zip") | .archive_download_url')"
echo " Archive download URL: $archive_download_url"
echo ""
if [ -z "$auth_header" ]; then
echo "Error: GitHub requires authentication to download artifacts. Cannot continue without a token."
exit 1
fi
# We get a zip file inside a zip file:
# * Inner zip file: The build creates a releasing-ready zip file. This is
# good because you can test the zip file before Jou is released.
# * Outer zip file: It is possible to include multiple files to the same
# GitHub Actions artifact, and downloadArtifact() gives a zip of all
# files that the artifact consists of.
echo "Downloading windows-zip artifact..."
curl -L -H "$auth_header" "$archive_download_url" -o nested-zip-file.zip
unzip nested-zip-file.zip
echo ""
datetag=$(date +'%Y-%m-%d-%H00')
jq -n --arg tag $datetag --arg desc "$(cat desc.md)" '{tag_name: $tag, body: $desc}' > release-params.json
echo "Release parameters:"
cat release-params.json
echo ""
if [ $dry_run = yes ]; then
echo "Aborting because this is a dry-run."
exit 0
fi
# To test this, you can change Akuli/jou to a temporary private repo
echo "CREATING RELEASE!!!"
curl -X POST "https://api.github.com/repos/Akuli/jou/releases" -H "$auth_header" -d @release-params.json | tee release-response.json
echo ""
echo "Attaching the Windows zip file to the release..."
# Response contains:
# "upload_url": "https://uploads.github.com/repos/Akuli/jou/releases/xxxxxxxxx/assets{?name,label}",
upload_url="$(jq -r .upload_url < release-response.json | cut -d'{' -f1)?name=jou_windows_64bit_$datetag.zip"
echo " POSTing to $upload_url"
curl -L -X POST -H "$auth_header" -H "Content-Type: application/zip" "$upload_url" --data-binary "@jou.zip"