Skip to content
This repository was archived by the owner on Jun 30, 2023. It is now read-only.

Commit 4f7f714

Browse files
committed
scripts: add a script to upload build artifacts
1 parent edf4af1 commit 4f7f714

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

.github/workflows/release.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: release
2+
on:
3+
release:
4+
types: [created]
5+
6+
jobs:
7+
release:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Install Go
11+
uses: actions/setup-go@v2
12+
with:
13+
go-version: 1.17.x
14+
- name: Checkout code
15+
uses: actions/checkout@v2
16+
- name: Build Release
17+
run: ./scripts/build-release.sh
18+
- name: Update Dependencies
19+
run: sudo apt-get update
20+
- name: Install Dependencies
21+
run: sudo apt-get install -y jq
22+
- name: Upload
23+
run: ./scripts/release.sh ./bin
24+
env:
25+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

scripts/build-release.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
#!/bin/bash -e
22

3+
# Copyright 2021 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
317
# A script for building binary releases for various platforms.
418

519
if [[ "$0" != "./scripts/build-release.sh" ]]; then
@@ -9,6 +23,10 @@ fi
923

1024
VERSION="$1"
1125

26+
if [[ "$GITHUB_REF_TYPE" == "tag" ]]; then
27+
VERSION="$GITHUB_REF_NAME"
28+
fi
29+
1230
if [[ "$VERSION" != v* ]]; then
1331
1>&2 echo "No version specified as argument"
1432
exit 1
@@ -49,6 +67,8 @@ function build {
4967
rm -rf "$TEMP_DIR"
5068
}
5169

70+
# NOTE: When adding new releases, also update .github/workflows/release.yaml.
71+
5272
build darwin amd64
5373
build darwin arm64
5474
build linux amd64

scripts/release.sh

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/bin/bash -e
2+
3+
# Copyright 2021 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# https://docs.github.com/en/actions/learn-github-actions/environment-variables
18+
19+
if [[ "$GITHUB_TOKEN" == "" ]]; then
20+
2>&1 echo "GITHUB_TOKEN not present"
21+
exit 1
22+
fi
23+
24+
if [[ "$GITHUB_REF_NAME" == "" ]]; then
25+
2>&1 echo "GITHUB_REF_NAME not present"
26+
exit 1
27+
fi
28+
29+
if [[ "$GITHUB_API_URL" == "" ]]; then
30+
2>&1 echo "GITHUB_API_URL not present"
31+
exit 1
32+
fi
33+
34+
if [[ "$GITHUB_REPOSITORY" == "" ]]; then
35+
2>&1 echo "GITHUB_REPOSITORY not present"
36+
exit 1
37+
fi
38+
39+
if [[ "$1" == "" ]]; then
40+
2>&1 echo "No files to upload"
41+
exit 1
42+
fi
43+
44+
AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}"
45+
46+
TEMP_DIR="$( mktemp -d )"
47+
48+
# https://docs.github.com/en/rest/reference/releases#get-a-release-by-tag-name
49+
50+
curl -sSL \
51+
--fail \
52+
--header "$AUTH_HEADER" \
53+
-o "${TEMP_DIR}/out" \
54+
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/releases/tags/${GITHUB_REF_NAME}"
55+
56+
RELEASE_ID="$( jq '.id' < "${TEMP_DIR}/out" )"
57+
58+
echo "Release ID: ${RELEASE_ID}"
59+
60+
# https://docs.github.com/en/rest/reference/releases#upload-a-release-asset
61+
62+
for FILE in ${1}/*
63+
do
64+
echo $FILE
65+
NAME="${FILE#$1}"
66+
if [[ "$NAME" != "/" && "$NAME" != "" ]]; then
67+
echo "Uploading: ${NAME}"
68+
RELEASE_URL="https://uploads.github.com/repos/${GITHUB_REPOSITORY}/releases/${RELEASE_ID}/assets?name=${NAME}"
69+
echo "Upload URL: ${RELEASE_URL}"
70+
curl -sSL \
71+
-o - \
72+
-XPOST \
73+
--fail \
74+
--header "$AUTH_HEADER" \
75+
--header "Content-Type: application/octet-stream" \
76+
--upload-file "$FILE" \
77+
"$RELEASE_URL"
78+
fi
79+
done
80+
81+
rm -rf "$TEMP_DIR"

0 commit comments

Comments
 (0)