Skip to content

Commit d6f1a06

Browse files
committed
Add GitHub Actions workflow for publishing Java SDK to Maven Central
1 parent e428b5f commit d6f1a06

1 file changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
name: Publish Java SDK to Maven Central
2+
3+
env:
4+
HUSKY: 0
5+
6+
on:
7+
workflow_dispatch:
8+
inputs:
9+
dist-tag:
10+
description: "Tag to publish under"
11+
type: choice
12+
required: true
13+
default: "latest"
14+
options:
15+
- latest
16+
- prerelease
17+
version:
18+
description: "Version override (optional, e.g., 1.0.0). If empty, auto-increments."
19+
type: string
20+
required: false
21+
22+
permissions:
23+
contents: write
24+
id-token: write
25+
26+
concurrency:
27+
group: publish-maven
28+
cancel-in-progress: false
29+
30+
jobs:
31+
# Calculate version using the nodejs version script (shared across all SDKs)
32+
version:
33+
name: Calculate Version
34+
runs-on: ubuntu-latest
35+
outputs:
36+
version: ${{ steps.version.outputs.VERSION }}
37+
current: ${{ steps.version.outputs.CURRENT }}
38+
current-prerelease: ${{ steps.version.outputs.CURRENT_PRERELEASE }}
39+
defaults:
40+
run:
41+
working-directory: ./nodejs
42+
steps:
43+
- uses: actions/checkout@v6
44+
- uses: actions/setup-node@v6
45+
with:
46+
node-version: "22.x"
47+
- run: npm ci --ignore-scripts
48+
- name: Get version
49+
id: version
50+
run: |
51+
CURRENT="$(node scripts/get-version.js current)"
52+
echo "CURRENT=$CURRENT" >> $GITHUB_OUTPUT
53+
echo "Current latest version: $CURRENT" >> $GITHUB_STEP_SUMMARY
54+
CURRENT_PRERELEASE="$(node scripts/get-version.js current-prerelease)"
55+
echo "CURRENT_PRERELEASE=$CURRENT_PRERELEASE" >> $GITHUB_OUTPUT
56+
echo "Current prerelease version: $CURRENT_PRERELEASE" >> $GITHUB_STEP_SUMMARY
57+
if [ -n "${{ github.event.inputs.version }}" ]; then
58+
VERSION="${{ github.event.inputs.version }}"
59+
# Validate version format matches dist-tag
60+
if [ "${{ github.event.inputs.dist-tag }}" = "latest" ]; then
61+
if [[ "$VERSION" == *-* ]]; then
62+
echo "❌ Error: Version '$VERSION' has a prerelease suffix but dist-tag is 'latest'" >> $GITHUB_STEP_SUMMARY
63+
echo "Use a version without suffix (e.g., '1.0.0') for latest releases"
64+
exit 1
65+
fi
66+
else
67+
if [[ "$VERSION" != *-* ]]; then
68+
echo "❌ Error: Version '$VERSION' has no prerelease suffix but dist-tag is 'prerelease'" >> $GITHUB_STEP_SUMMARY
69+
echo "Use a version with suffix (e.g., '1.0.0-preview.0') for prerelease"
70+
exit 1
71+
fi
72+
fi
73+
echo "Using manual version override: $VERSION" >> $GITHUB_STEP_SUMMARY
74+
else
75+
VERSION="$(node scripts/get-version.js ${{ github.event.inputs.dist-tag }})"
76+
echo "Auto-incremented version: $VERSION" >> $GITHUB_STEP_SUMMARY
77+
fi
78+
echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
79+
80+
publish-maven:
81+
name: Publish Java SDK to Maven Central
82+
needs: version
83+
runs-on: ubuntu-latest
84+
defaults:
85+
run:
86+
working-directory: ./java
87+
steps:
88+
- uses: actions/checkout@v6
89+
90+
- uses: ./.github/actions/setup-copilot
91+
92+
- name: Set up JDK 21
93+
uses: actions/setup-java@v5
94+
with:
95+
java-version: "21"
96+
distribution: "temurin"
97+
server-id: central
98+
server-username: ${{ secrets.MAVEN_CENTRAL_USERNAME }}
99+
server-password: ${{ secrets.MAVEN_CENTRAL_PASSWORD }}
100+
gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }}
101+
gpg-passphrase: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
102+
103+
- name: Set version in pom.xml
104+
run: mvn versions:set -DnewVersion=${{ needs.version.outputs.version }} -DgenerateBackupPoms=false
105+
106+
- name: Build and verify
107+
run: mvn verify
108+
109+
- name: Deploy to Maven Central
110+
run: mvn deploy -DskipTests -Prelease
111+
env:
112+
MAVEN_USERNAME: ${{ secrets.MAVEN_CENTRAL_USERNAME }}
113+
MAVEN_PASSWORD: ${{ secrets.MAVEN_CENTRAL_PASSWORD }}
114+
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
115+
116+
github-release:
117+
name: Create GitHub Release
118+
needs: [version, publish-maven]
119+
if: github.ref == 'refs/heads/main'
120+
runs-on: ubuntu-latest
121+
steps:
122+
- uses: actions/checkout@v6
123+
- name: Create GitHub Release
124+
if: github.event.inputs.dist-tag == 'latest'
125+
run: |
126+
NOTES_FLAG=""
127+
if git rev-parse "v${{ needs.version.outputs.current }}" >/dev/null 2>&1; then
128+
NOTES_FLAG="--notes-start-tag v${{ needs.version.outputs.current }}"
129+
fi
130+
gh release create "java/v${{ needs.version.outputs.version }}" \
131+
--title "Java SDK v${{ needs.version.outputs.version }}" \
132+
--generate-notes $NOTES_FLAG \
133+
--target ${{ github.sha }}
134+
env:
135+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
136+
- name: Create GitHub Pre-Release
137+
if: github.event.inputs.dist-tag == 'prerelease'
138+
run: |
139+
NOTES_FLAG=""
140+
if git rev-parse "v${{ needs.version.outputs.current-prerelease }}" >/dev/null 2>&1; then
141+
NOTES_FLAG="--notes-start-tag v${{ needs.version.outputs.current-prerelease }}"
142+
fi
143+
gh release create "java/v${{ needs.version.outputs.version }}" \
144+
--prerelease \
145+
--title "Copilot SDK Java v${{ needs.version.outputs.version }}" \
146+
--generate-notes $NOTES_FLAG \
147+
--target ${{ github.sha }}
148+
env:
149+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)