Skip to content

Commit da51092

Browse files
chore(internal): support uploading Maven repo artifacts to stainless package server
1 parent 6b3f7ee commit da51092

File tree

3 files changed

+129
-2
lines changed

3 files changed

+129
-2
lines changed

.github/workflows/ci.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ jobs:
4040
build:
4141
timeout-minutes: 15
4242
name: build
43+
permissions:
44+
contents: read
45+
id-token: write
4346
runs-on: ${{ github.repository == 'stainless-sdks/lithic-java' && 'depot-ubuntu-24.04' || 'ubuntu-latest' }}
4447
if: github.event_name == 'push' || github.event.pull_request.head.repo.fork
4548

@@ -61,6 +64,21 @@ jobs:
6164
- name: Build SDK
6265
run: ./scripts/build
6366

67+
- name: Get GitHub OIDC Token
68+
if: github.repository == 'stainless-sdks/lithic-java'
69+
id: github-oidc
70+
uses: actions/github-script@v6
71+
with:
72+
script: core.setOutput('github_token', await core.getIDToken());
73+
74+
- name: Build and upload Maven artifacts
75+
if: github.repository == 'stainless-sdks/lithic-java'
76+
env:
77+
URL: https://pkg.stainless.com/s
78+
AUTH: ${{ steps.github-oidc.outputs.github_token }}
79+
SHA: ${{ github.sha }}
80+
PROJECT: lithic-java
81+
run: ./scripts/upload-artifacts
6482
test:
6583
timeout-minutes: 15
6684
name: test

buildSrc/src/main/kotlin/lithic.publish.gradle.kts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ plugins {
77
id("com.vanniktech.maven.publish")
88
}
99

10+
publishing {
11+
repositories {
12+
if (project.hasProperty("publishLocal")) {
13+
maven {
14+
name = "LocalFileSystem"
15+
url = uri("${rootProject.layout.buildDirectory.get()}/local-maven-repo")
16+
}
17+
}
18+
}
19+
}
20+
1021
repositories {
1122
gradlePluginPortal()
1223
mavenCentral()
@@ -17,8 +28,10 @@ extra["signingInMemoryKeyId"] = System.getenv("GPG_SIGNING_KEY_ID")
1728
extra["signingInMemoryKeyPassword"] = System.getenv("GPG_SIGNING_PASSWORD")
1829

1930
configure<MavenPublishBaseExtension> {
20-
signAllPublications()
21-
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)
31+
if (!project.hasProperty("publishLocal")) {
32+
signAllPublications()
33+
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)
34+
}
2235

2336
coordinates(project.group.toString(), project.name, project.version.toString())
2437
configure(

scripts/upload-artifacts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)