|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +# ANSI Color Codes |
| 6 | +GREEN='\033[32m' |
| 7 | +RED='\033[31m' |
| 8 | +NC='\033[0m' # No Color |
| 9 | + |
| 10 | +log_error() { |
| 11 | + local msg="$1" |
| 12 | + local headers="$2" |
| 13 | + local body="$3" |
| 14 | + echo -e "${RED}${msg}${NC}" |
| 15 | + [[ -f "$headers" ]] && echo -e "${RED}Headers:$(cat "$headers")${NC}" |
| 16 | + echo -e "${RED}Body: ${body}${NC}" |
| 17 | + exit 1 |
| 18 | +} |
| 19 | + |
| 20 | +upload_file() { |
| 21 | + local file_name="$1" |
| 22 | + local tmp_headers |
| 23 | + tmp_headers=$(mktemp) |
| 24 | + |
| 25 | + if [ -f "$file_name" ]; then |
| 26 | + echo -e "${GREEN}Processing file: $file_name${NC}" |
| 27 | + pkg_file_name="mvn${file_name#./build/local-maven-repo}" |
| 28 | + |
| 29 | + # Get signed URL for uploading artifact file |
| 30 | + signed_url_response=$(curl -X POST -G "$URL" \ |
| 31 | + -sS --retry 5 \ |
| 32 | + -D "$tmp_headers" \ |
| 33 | + --data-urlencode "filename=$pkg_file_name" \ |
| 34 | + -H "Authorization: Bearer $AUTH" \ |
| 35 | + -H "Content-Type: application/json") |
| 36 | + |
| 37 | + # Validate JSON and extract URL |
| 38 | + if ! signed_url=$(echo "$signed_url_response" | jq -e -r '.url' 2>/dev/null) || [[ "$signed_url" == "null" ]]; then |
| 39 | + log_error "Failed to get valid signed URL" "$tmp_headers" "$signed_url_response" |
| 40 | + fi |
| 41 | + |
| 42 | + # Set content-type based on file extension |
| 43 | + local extension="${file_name##*.}" |
| 44 | + local content_type |
| 45 | + case "$extension" in |
| 46 | + jar) content_type="application/java-archive" ;; |
| 47 | + md5|sha1|sha256|sha512) content_type="text/plain" ;; |
| 48 | + module) content_type="application/json" ;; |
| 49 | + pom|xml) content_type="application/xml" ;; |
| 50 | + *) content_type="application/octet-stream" ;; |
| 51 | + esac |
| 52 | + |
| 53 | + # Upload file |
| 54 | + upload_response=$(curl -v -X PUT \ |
| 55 | + --retry 5 \ |
| 56 | + -D "$tmp_headers" \ |
| 57 | + -H "Content-Type: $content_type" \ |
| 58 | + --data-binary "@${file_name}" "$signed_url" 2>&1) |
| 59 | + |
| 60 | + if ! echo "$upload_response" | grep -q "HTTP/[0-9.]* 200"; then |
| 61 | + log_error "Failed upload artifact file" "$tmp_headers" "$upload_response" |
| 62 | + fi |
| 63 | + |
| 64 | + # Insert small throttle to reduce rate limiting risk |
| 65 | + sleep 0.1 |
| 66 | + fi |
| 67 | +} |
| 68 | + |
| 69 | +walk_tree() { |
| 70 | + local current_dir="$1" |
| 71 | + |
| 72 | + for entry in "$current_dir"/*; do |
| 73 | + # Check that entry is valid |
| 74 | + [ -e "$entry" ] || [ -h "$entry" ] || continue |
| 75 | + |
| 76 | + if [ -d "$entry" ]; then |
| 77 | + walk_tree "$entry" |
| 78 | + else |
| 79 | + upload_file "$entry" |
| 80 | + fi |
| 81 | + done |
| 82 | +} |
| 83 | + |
| 84 | +cd "$(dirname "$0")/.." |
| 85 | + |
| 86 | +echo "::group::Creating local Maven content" |
| 87 | +./gradlew publishMavenPublicationToLocalFileSystemRepository -PpublishLocal |
| 88 | +echo "::endgroup::" |
| 89 | + |
| 90 | +echo "::group::Uploading to pkg.stainless.com" |
| 91 | +walk_tree "./build/local-maven-repo" |
| 92 | +echo "::endgroup::" |
| 93 | + |
| 94 | +echo "::group::Generating instructions" |
| 95 | +echo "Configure maven or gradle to use the repo located at 'https://pkg.stainless.com/s/${PROJECT}/${SHA}/mvn'" |
| 96 | +echo "::endgroup::" |
0 commit comments