-
Notifications
You must be signed in to change notification settings - Fork 1
171 lines (149 loc) · 6.48 KB
/
Copy pathgenerate.yml
File metadata and controls
171 lines (149 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
name: Regenerate
on:
repository_dispatch:
types: [openapi-updated]
workflow_dispatch:
# Serialize runs: concurrent dispatches race each other's push to main and
# compute the same next version from the same tag state (the second publish
# is rejected by the registry).
concurrency:
group: ${{ github.workflow }}
cancel-in-progress: false
jobs:
generate:
name: Regenerate from OpenAPI
runs-on: ubuntu-latest
permissions:
contents: write
actions: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
# Explicit ref: for repository_dispatch, the default github.sha is
# resolved at dispatch time, so a run queued behind another run
# checks out a tip that misses the earlier push and then conflicts.
ref: main
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: Setup Java
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
- name: Install OpenAPI Generator
run: npm install -g @openapitools/openapi-generator-cli
- name: Fetch latest OpenAPI spec
run: |
curl -f --retry 5 --retry-delay 2 --retry-all-errors --max-time 60 -o openapi.yaml https://zernio.com/openapi.yaml
echo "Fetched OpenAPI spec"
- name: Normalize spec for the Java generator
run: |
python3 -c 'import yaml' 2>/dev/null || python3 -m pip install --quiet --break-system-packages pyyaml
python3 scripts/normalize-spec-for-java.py openapi.yaml "$RUNNER_TEMP/openapi-java.json"
- name: Generate SDK
run: |
openapi-generator-cli generate \
-i "$RUNNER_TEMP/openapi-java.json" \
-g java \
-o . \
--skip-validate-spec \
--global-property skipFormModel=false \
--additional-properties=artifactId=zernio-sdk,groupId=dev.zernio,apiPackage=dev.zernio.api,modelPackage=dev.zernio.model,invokerPackage=dev.zernio,library=native \
--git-user-id=zernio-dev \
--git-repo-id=zernio-java
- name: Remove generated workflow files
run: |
rm -f .github/workflows/maven.yml || true
git checkout .github/workflows/generate.yml
- name: Build
run: mvn compile -q
- name: Check for changes
id: changes
run: |
git add -A
if git diff --staged --quiet; then
echo "has_changes=false" >> $GITHUB_OUTPUT
else
echo "has_changes=true" >> $GITHUB_OUTPUT
fi
- name: Get next version
if: steps.changes.outputs.has_changes == 'true'
id: version
run: |
# Base = max(highest tag, highest published Maven version). git
# describe can select an older tag when multiple tags point to the
# same commit, which made every run recompute and republish 0.0.952.
# Checking Central also self-heals any publish/tag divergence.
TAG_VERSION=$(git tag -l 'v*' | sed 's/^v//' | sort -V | tail -1)
CENTRAL_PUBLISHED=$(curl -sf --retry 3 https://repo1.maven.org/maven2/com/zernio/zernio-sdk/maven-metadata.xml | sed -n 's:.*<latest>\([^<]*\)</latest>.*:\1:p' | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' || echo "0.0.0")
BASE=$(printf '%s\n' "${TAG_VERSION:-0.0.0}" "$CENTRAL_PUBLISHED" | sort -V | tail -1)
MVN_VERSION=$(echo "$BASE" | awk -F. '{printf "%d.%d.%d", $1, $2, $3+1}')
echo "new_version=v$MVN_VERSION" >> $GITHUB_OUTPUT
echo "mvn_version=$MVN_VERSION" >> $GITHUB_OUTPUT
- name: Update pom version
if: steps.changes.outputs.has_changes == 'true'
run: |
mvn versions:set -DnewVersion=${{ steps.version.outputs.mvn_version }} -DgenerateBackupPoms=false || true
- name: Commit and push
if: steps.changes.outputs.has_changes == 'true'
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add -A
git commit -m "chore: regenerate from OpenAPI spec
- Auto-generated SDK updates
- Version: ${{ steps.version.outputs.new_version }}"
# Retry with rebase to survive concurrent dispatches that land
# back-to-back pushes to main. -X theirs: the replayed commit is a
# full regeneration from a fresher spec fetch, so on conflicting
# hunks the regenerated content wins instead of aborting the run.
for attempt in 1 2 3; do
if git push; then break; fi
echo "Push attempt $attempt failed; rebasing and retrying..."
git pull --rebase -X theirs origin main
sleep $((attempt * 5))
done
- name: Create GitHub Release
if: steps.changes.outputs.has_changes == 'true'
uses: softprops/action-gh-release@v1
with:
tag_name: ${{ steps.version.outputs.new_version }}
name: ${{ steps.version.outputs.new_version }}
generate_release_notes: true
body: |
## Auto-generated SDK Update
**Maven** (add JitPack repository):
```xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.zernio-dev</groupId>
<artifactId>zernio-java</artifactId>
<version>${{ steps.version.outputs.new_version }}</version>
</dependency>
```
**Gradle**:
```groovy
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.zernio-dev:zernio-java:${{ steps.version.outputs.new_version }}'
}
```
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Trigger Maven Central publish
# Releases created with GITHUB_TOKEN do not fire `release: published`
# workflows (GitHub anti-recursion rule), so publish.yml never ran on
# auto-releases and Maven Central sat at 0.0.307 (2026-06-01) while
# GitHub releases advanced. workflow_dispatch is exempt from that rule.
if: steps.changes.outputs.has_changes == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: gh workflow run publish.yml --ref main