Debian Codegraph @

.github/workflows/tools/upload-build

raw symbols SCIP
1 #!/bin/bash
2
3 set +x # Do not leak information
4
5 # Exit immediately if one of the files given as arguments is not there
6 # because we don't want to delete the existing release if we don't have
7 # the new files that should be uploaded
8 for file in "$@"
9 do
10 if [ ! -e "$file" ]
11 then echo "$file is missing, giving up." >&2; exit 1
12 fi
13 done
14
15 if [ $# -eq 0 ]; then
16 echo "No artifacts to use for release, giving up."
17 exit 0
18 fi
19
20 if command -v sha256sum >/dev/null 2>&1 ; then
21 shatool="sha256sum"
22 elif command -v shasum >/dev/null 2>&1 ; then
23 shatool="shasum -a 256" # macOS fallback
24 else
25 echo "Neither sha256sum nor shasum is available, cannot check hashes"
26 fi
27
28 if [[ ${GITHUB_REF} == refs/heads/* ]]; then
29 GITHUB_BRANCH=${GITHUB_REF##*/}
30 elif [[ ${GITHUB_REF} == refs/tags/* ]]; then
31 GITHUB_TAG=${GITHUB_REF##*/}
32 fi
33
34 # The calling script (usually .travis.yml) can set a suffix to be used for
35 # the tag and release name. This way it is possible to have a release for
36 # the output of the CI/CD pipeline (marked as 'continuous') and also test
37 # builds for other branches.
38 # If this build was triggered by a tag, call the result a Release
39 if [ ! -z "$UPLOADTOOL_SUFFIX" ] ; then
40 if [ "$UPLOADTOOL_SUFFIX" = "${GITHUB_TAG}" ] ; then
41 RELEASE_NAME="${GITHUB_TAG}"
42 RELEASE_TITLE="Release build (${GITHUB_TAG})"
43 is_prerelease="false"
44 else
45 RELEASE_NAME="continuous-$UPLOADTOOL_SUFFIX"
46 RELEASE_TITLE="Continuous build ($UPLOADTOOL_SUFFIX)"
47 is_prerelease="true"
48 fi
49 else
50 if [ "${GITHUB_TAG}" != "" ]; then
51 RELEASE_NAME="${GITHUB_TAG}"
52 RELEASE_TITLE="Release build (${GITHUB_TAG})"
53 is_prerelease="false"
54 elif [ "${GITHUB_BRANCH}" != "" ] && [ "${GITHUB_BRANCH}" != "master" ]; then
55 RELEASE_NAME="continuous-${GITHUB_BRANCH}"
56 RELEASE_TITLE="Continuous build (${GITHUB_BRANCH})"
57 is_prerelease="true"
58 else
59 RELEASE_NAME="continuous" # Do not use "latest" as it is reserved by GitHub
60 RELEASE_TITLE="Continuous build"
61 is_prerelease="true"
62 fi
63 fi
64
65 # Do not upload non-master branch builds
66 if [ "${GITHUB_EVENT_NAME}" == "pull_request" ] ; then
67 echo "Release uploading disabled for pull requests, uploading to transfer.sh instead"
68 rm -f ./uploaded-to
69 for FILE in "$@" ; do
70 BASENAME="$(basename "${FILE}")"
71 curl --upload-file $FILE "https://transfer.sh/$BASENAME" > ./one-upload
72 echo "$(cat ./one-upload)" # this way we get a newline
73 echo -n "$(cat ./one-upload)\\n" >> ./uploaded-to # this way we get a \n but no newline
74 done
75 $shatool "$@"
76 exit 0
77 fi
78
79 tag_url="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/git/refs/tags/${RELEASE_NAME}"
80 tag_infos=$(curl -XGET --header "Authorization: token ${RELEASE_TOKEN}" "${tag_url}")
81 echo "tag_infos: $tag_infos"
82 tag_sha=$(echo "$tag_infos" | grep '"sha":' | head -n 1 | cut -d '"' -f 4 | cut -d '{' -f 1)
83 echo "tag_sha: $tag_sha"
84
85 release_url="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/releases/tags/${RELEASE_NAME}"
86 echo "Getting the release ID..."
87 echo "release_url: $release_url"
88 release_infos=$(curl -XGET --header "Authorization: token ${RELEASE_TOKEN}" "${release_url}")
89 echo "release_infos: $release_infos"
90 release_id=$(echo "$release_infos" | grep "\"id\":" | head -n 1 | tr -s " " | cut -f 3 -d" " | cut -f 1 -d ",")
91 echo "release ID: $release_id"
92 upload_url=$(echo "$release_infos" | grep '"upload_url":' | head -n 1 | cut -d '"' -f 4 | cut -d '{' -f 1)
93 echo "upload_url: $upload_url"
94 release_url=$(echo "$release_infos" | grep '"url":' | head -n 1 | cut -d '"' -f 4 | cut -d '{' -f 1)
95 echo "release_url: $release_url"
96 target_commit_sha=$(echo "$release_infos" | grep '"target_commitish":' | head -n 1 | cut -d '"' -f 4 | cut -d '{' -f 1)
97 echo "target_commit_sha: $target_commit_sha"
98
99 if [ "${GITHUB_SHA}" != "$target_commit_sha" ] ; then
100
101 echo "${GITHUB_SHA} != target_commit_sha, hence deleting ${RELEASE_NAME}..."
102
103 if [ ! -z "$release_id" ]; then
104 delete_url="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/releases/$release_id"
105 echo "Delete the release..."
106 echo "delete_url: $delete_url"
107 curl -XDELETE \
108 --header "Authorization: token ${RELEASE_TOKEN}" \
109 "${delete_url}"
110 fi
111
112 # echo "Checking if release with the same name is still there..."
113 # echo "release_url: $release_url"
114 # curl -XGET --header "Authorization: token ${RELEASE_TOKEN}" \
115 # "$release_url"
116
117 if [ "$is_prerelease" = "true" ] ; then
118 # if this is a continuous build tag, then delete the old tag
119 # in preparation for the new release
120 echo "Delete the tag..."
121 delete_url="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/git/refs/tags/${RELEASE_NAME}"
122 echo "delete_url: $delete_url"
123 curl -XDELETE \
124 --header "Authorization: token ${RELEASE_TOKEN}" \
125 "${delete_url}"
126 fi
127
128 echo "Create release..."
129
130 if [ -z "${GITHUB_BRANCH}" ] ; then
131 {GITHUB_BRANCH}="master"
132 fi
133
134 if [ ! -z "${GITHUB_RUN_ID}" ] ; then
135 if [ -z "${UPLOADTOOL_BODY+x}" ] ; then
136 BODY="GitHub Actions build log: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
137 else
138 BODY="$UPLOADTOOL_BODY"
139 fi
140 else
141 BODY="$UPLOADTOOL_BODY"
142 fi
143
144 release_infos=$(curl -H "Authorization: token ${RELEASE_TOKEN}" \
145 --data '{"tag_name": "'"${RELEASE_NAME}"'","target_commitish": "'"${GITHUB_SHA}"'","name": "'"${RELEASE_TITLE}"'","body": "'"${BODY}"'","draft": false,"prerelease": '$is_prerelease'}' "${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/releases")
146
147 echo "$release_infos"
148
149 unset upload_url
150 upload_url=$(echo "$release_infos" | grep '"upload_url":' | head -n 1 | cut -d '"' -f 4 | cut -d '{' -f 1)
151 echo "upload_url: $upload_url"
152
153 unset release_url
154 release_url=$(echo "$release_infos" | grep '"url":' | head -n 1 | cut -d '"' -f 4 | cut -d '{' -f 1)
155 echo "release_url: $release_url"
156
157 fi # if [ "${GITHUB_SHA}" != "$tag_sha" ]
158
159 if [ -z "$release_url" ] ; then
160 echo "Cannot figure out the release URL for ${RELEASE_NAME}"
161 exit 1
162 fi
163
164 echo "Upload binaries to the release..."
165
166 for FILE in "$@" ; do
167 FULLNAME="${FILE}"
168 BASENAME="$(basename "${FILE}")"
169 curl -H "Authorization: token ${RELEASE_TOKEN}" \
170 -H "Accept: application/vnd.github.manifold-preview" \
171 -H "Content-Type: application/octet-stream" \
172 --data-binary @$FULLNAME \
173 "$upload_url?name=$BASENAME"
174 echo ""
175 done
176
177 $shatool "$@"
178
179 if [ "${GITHUB_SHA}" != "$tag_sha" ] ; then
180 echo "Publish the release..."
181
182 release_infos=$(curl -H "Authorization: token ${RELEASE_TOKEN}" \
183 --data '{"draft": false}' "$release_url")
184
185 echo "$release_infos"
186 fi # if [ "${GITHUB_SHA}" != "$tag_sha" ]