upload-nightly

 1#!/usr/bin/env bash
 2
 3# Based on the template in: https://docs.digitalocean.com/reference/api/spaces-api/
 4bash -euo pipefail
 5
 6allowed_targets=("linux-deb" "macos")
 7is_allowed_target() {
 8    for val in "${allowed_targets[@]}"; do
 9        if [[ "$1" == "$val" ]]; then
10            return 0
11        fi
12    done
13    return 1
14}
15
16if [[ -n "${1:-}" ]]; then
17    if is_allowed_target "$1"; then
18        target="$1"
19    else
20        echo "Error: Target '$1' is not allowed"
21        echo "Usage: $0 [${allowed_targets[@]}]"
22        exit 1
23    fi
24else
25echo "Error: Target is not specified"
26echo "Usage: $0 [${allowed_targets[@]}]"
27exit 1
28fi
29echo "Uploading nightly for target: $target"
30
31# Step 1: Define the parameters for the Space you want to upload to.
32SPACE="zed-nightly-host" # Find your endpoint in the control panel, under Settings.
33REGION="nyc3" # Must be "us-east-1" when creating new Spaces. Otherwise, use the region in your endpoint (e.g. nyc3).
34
35# Step 2: Define a function that uploads your object via cURL.
36function uploadToSpaces
37{
38  file_to_upload="$1"
39  file_name="$2"
40  space_path="nightly"
41  date=$(date +"%a, %d %b %Y %T %z")
42  acl="x-amz-acl:private"
43  content_type="application/octet-stream"
44  storage_type="x-amz-storage-class:STANDARD"
45  string="PUT\n\n${content_type}\n${date}\n${acl}\n${storage_type}\n/${SPACE}/${space_path}/${file_name}"
46  signature=$(echo -en "${string}" | openssl sha1 -hmac "${DIGITALOCEAN_SPACES_SECRET_KEY}" -binary | base64)
47
48  curl --fail -vv -s -X PUT -T "$file_to_upload" \
49    -H "Host: ${SPACE}.${REGION}.digitaloceanspaces.com" \
50    -H "Date: $date" \
51    -H "Content-Type: $content_type" \
52    -H "$storage_type" \
53    -H "$acl" \
54    -H "Authorization: AWS ${DIGITALOCEAN_SPACES_ACCESS_KEY}:$signature" \
55    "https://${SPACE}.${REGION}.digitaloceanspaces.com/${space_path}/${file_name}"
56}
57
58sha=$(git rev-parse HEAD)
59echo ${sha} > target/latest-sha
60case "$target" in
61    macos)
62        uploadToSpaces "target/release/Zed.dmg" "Zed.dmg"
63        uploadToSpaces "target/latest-sha" "latest-sha"
64        ;;
65    linux-deb)
66        find target/release -type f -name "*.deb" -print0 | while IFS= read -r -d '' bundle_file; do
67            uploadToSpaces "$bundle_file" "$(basename "$bundle_file")"
68        done
69        uploadToSpaces "target/latest-sha" "latest-sha-linux-deb"
70        ;;
71    *)
72        echo "Error: Unknown target '$target'"
73        exit 1
74        ;;
75esac