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

Commit

Permalink
scripts: add a script to upload build artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
ericchiang committed Dec 29, 2021
1 parent edf4af1 commit 4f7f714
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: release
on:
release:
types: [created]

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.17.x
- name: Checkout code
uses: actions/checkout@v2
- name: Build Release
run: ./scripts/build-release.sh
- name: Update Dependencies
run: sudo apt-get update
- name: Install Dependencies
run: sudo apt-get install -y jq
- name: Upload
run: ./scripts/release.sh ./bin
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
20 changes: 20 additions & 0 deletions scripts/build-release.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
#!/bin/bash -e

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# A script for building binary releases for various platforms.

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

VERSION="$1"

if [[ "$GITHUB_REF_TYPE" == "tag" ]]; then
VERSION="$GITHUB_REF_NAME"
fi

if [[ "$VERSION" != v* ]]; then
1>&2 echo "No version specified as argument"
exit 1
Expand Down Expand Up @@ -49,6 +67,8 @@ function build {
rm -rf "$TEMP_DIR"
}

# NOTE: When adding new releases, also update .github/workflows/release.yaml.

build darwin amd64
build darwin arm64
build linux amd64
Expand Down
81 changes: 81 additions & 0 deletions scripts/release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/bin/bash -e

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# https://docs.github.com/en/actions/learn-github-actions/environment-variables

if [[ "$GITHUB_TOKEN" == "" ]]; then
2>&1 echo "GITHUB_TOKEN not present"
exit 1
fi

if [[ "$GITHUB_REF_NAME" == "" ]]; then
2>&1 echo "GITHUB_REF_NAME not present"
exit 1
fi

if [[ "$GITHUB_API_URL" == "" ]]; then
2>&1 echo "GITHUB_API_URL not present"
exit 1
fi

if [[ "$GITHUB_REPOSITORY" == "" ]]; then
2>&1 echo "GITHUB_REPOSITORY not present"
exit 1
fi

if [[ "$1" == "" ]]; then
2>&1 echo "No files to upload"
exit 1
fi

AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}"

TEMP_DIR="$( mktemp -d )"

# https://docs.github.com/en/rest/reference/releases#get-a-release-by-tag-name

curl -sSL \
--fail \
--header "$AUTH_HEADER" \
-o "${TEMP_DIR}/out" \
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/releases/tags/${GITHUB_REF_NAME}"

RELEASE_ID="$( jq '.id' < "${TEMP_DIR}/out" )"

echo "Release ID: ${RELEASE_ID}"

# https://docs.github.com/en/rest/reference/releases#upload-a-release-asset

for FILE in ${1}/*
do
echo $FILE
NAME="${FILE#$1}"
if [[ "$NAME" != "/" && "$NAME" != "" ]]; then
echo "Uploading: ${NAME}"
RELEASE_URL="https://uploads.github.com/repos/${GITHUB_REPOSITORY}/releases/${RELEASE_ID}/assets?name=${NAME}"
echo "Upload URL: ${RELEASE_URL}"
curl -sSL \
-o - \
-XPOST \
--fail \
--header "$AUTH_HEADER" \
--header "Content-Type: application/octet-stream" \
--upload-file "$FILE" \
"$RELEASE_URL"
fi
done

rm -rf "$TEMP_DIR"

0 comments on commit 4f7f714

Please sign in to comment.