#!/usr/bin/env bash set -euo pipefail # ANSI Color Codes GREEN='\033[32m' RED='\033[31m' NC='\033[0m' # No Color MAVEN_REPO_PATH="./build/local-maven-repo" log_error() { local msg="$1" local headers="$2" local body="$3" echo -e "${RED}${msg}${NC}" [[ -f "$headers" ]] && echo -e "${RED}Headers:$(cat "$headers")${NC}" echo -e "${RED}Body: ${body}${NC}" exit 1 } upload_file() { local file_name="$1" local tmp_headers tmp_headers=$(mktemp) if [ -f "$file_name" ]; then echo -e "${GREEN}Processing file: $file_name${NC}" pkg_file_name="mvn${file_name#"${MAVEN_REPO_PATH}"}" # Get signed URL for uploading artifact file signed_url_response=$(curl -X POST -G "$URL" \ -sS --retry 5 \ -D "$tmp_headers" \ --data-urlencode "filename=$pkg_file_name" \ -H "Authorization: Bearer $AUTH" \ -H "Content-Type: application/json") # Validate JSON and extract URL if ! signed_url=$(echo "$signed_url_response" | jq -e -r '.url' 2>/dev/null) || [[ "$signed_url" == "null" ]]; then log_error "Failed to get valid signed URL" "$tmp_headers" "$signed_url_response" fi # Set content-type based on file extension local extension="${file_name##*.}" local content_type case "$extension" in jar) content_type="application/java-archive" ;; md5|sha1|sha256|sha512) content_type="text/plain" ;; module) content_type="application/json" ;; pom|xml) content_type="application/xml" ;; html) content_type="text/html" ;; *) content_type="application/octet-stream" ;; esac # Upload file upload_response=$(curl -v -X PUT \ --retry 5 \ --retry-all-errors \ -D "$tmp_headers" \ -H "Content-Type: $content_type" \ --data-binary "@${file_name}" "$signed_url" 2>&1) if ! echo "$upload_response" | grep -q "HTTP/[0-9.]* 200"; then log_error "Failed to upload artifact file" "$tmp_headers" "$upload_response" fi # Insert small throttle to reduce rate limiting risk sleep 0.1 fi } walk_tree() { local current_dir="$1" for entry in "$current_dir"/*; do # Check that entry is valid [ -e "$entry" ] || [ -h "$entry" ] || continue if [ -d "$entry" ]; then walk_tree "$entry" else upload_file "$entry" fi done } generate_instructions() { cat << EOF > "$MAVEN_REPO_PATH/index.html"
This is the Maven repository for your Stainless Java SDK build.
The details depend on whether you're using Maven or Gradle as your build tool.
Add the following to your project's pom.xml:
<repositories>
<repository>
<id>stainless-sdk-repo</id>
<url>https://pkg.stainless.com/s/${PROJECT}/${SHA}/mvn</url>
</repository>
</repositories>
Add the following to your build.gradle file:
repositories {
maven {
url "https://pkg.stainless.com/s/${PROJECT}/${SHA}/mvn"
}
}
Some accounts may require authentication to access the repository. If so, use the
following instructions, replacing YOUR_STAINLESS_API_TOKEN with your actual token.
First, ensure you have the following in your Maven settings.xml for repo authentication:
<servers>
<server>
<id>stainless-sdk-repo</id>
<configuration>
<httpHeaders>
<property>
<name>Authorization</name>
<value>Bearer YOUR_STAINLESS_API_TOKEN</value>
</property>
</httpHeaders>
</configuration>
</server>
</servers>
Then, add the following to your project's pom.xml:
<repositories>
<repository>
<id>stainless-sdk-repo</id>
<url>https://pkg.stainless.com/s/${PROJECT}/${SHA}/mvn</url>
</repository>
</repositories>
Add the following to your build.gradle file:
repositories {
maven {
url "https://pkg.stainless.com/s/${PROJECT}/${SHA}/mvn"
credentials(HttpHeaderCredentials) {
name = "Authorization"
value = "Bearer YOUR_STAINLESS_API_TOKEN"
}
authentication {
header(HttpHeaderAuthentication)
}
}
}
Once you've configured the repository, you can include dependencies from it as usual. See your project README for more details.
EOF upload_file "${MAVEN_REPO_PATH}/index.html" echo "Configure maven or gradle to use the repo located at 'https://pkg.stainless.com/s/${PROJECT}/${SHA}/mvn'" echo "For more details, see the directions in https://pkg.stainless.com/s/${PROJECT}/${SHA}/mvn/index.html" } cd "$(dirname "$0")/.." echo "::group::Creating local Maven content" ./gradlew publishMavenPublicationToLocalFileSystemRepository -PpublishLocal echo "::endgroup::" echo "::group::Uploading to pkg.stainless.com" walk_tree "$MAVEN_REPO_PATH" echo "::endgroup::" echo "::group::Generating instructions" generate_instructions echo "::endgroup::"