Skip to content

Commit 5e52170

Browse files
Merge pull request #21 from ipdata/auto-release
Release automatically when the version changes
2 parents f8feb0e + 51c7cee commit 5e52170

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,61 @@ jobs:
2828
2929
- name: Build and Test
3030
run: mvn clean test post-integration-test
31+
32+
# Tags and releases the version declared in pom.xml once it reaches the
33+
# default branch. A version that is already tagged is left alone.
34+
release:
35+
needs: test
36+
if: github.event_name == 'push' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch)
37+
runs-on: ubuntu-latest
38+
permissions:
39+
contents: write
40+
concurrency:
41+
group: release
42+
cancel-in-progress: false
43+
outputs:
44+
tag: ${{ steps.version.outputs.tag }}
45+
created: ${{ steps.release.outputs.created }}
46+
steps:
47+
- uses: actions/checkout@v4
48+
49+
- name: Read the version from pom.xml
50+
id: version
51+
run: |
52+
VERSION=$(awk '/<artifactId>ipdata-java-client<\/artifactId>/{f=1} f&&/<version>/{sub(/.*<version>/,"");sub(/<\/version>.*/,"");print;exit}' pom.xml)
53+
if [ -z "$VERSION" ]; then
54+
echo "::error file=pom.xml::Could not read the project version"
55+
exit 1
56+
fi
57+
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
58+
echo "tag=v$VERSION" >> "$GITHUB_OUTPUT"
59+
60+
- name: Create the tag and the GitHub release
61+
id: release
62+
env:
63+
GH_TOKEN: ${{ github.token }}
64+
TAG: ${{ steps.version.outputs.tag }}
65+
run: |
66+
status=0
67+
git ls-remote --exit-code --tags origin "refs/tags/$TAG" > /dev/null || status=$?
68+
if [ "$status" -eq 0 ]; then
69+
echo "Tag $TAG already exists; nothing to release."
70+
echo "created=false" >> "$GITHUB_OUTPUT"
71+
exit 0
72+
elif [ "$status" -ne 2 ]; then
73+
echo "::error::git ls-remote failed with status $status"
74+
exit "$status"
75+
fi
76+
gh release create "$TAG" --target "$GITHUB_SHA" --title "$TAG" --generate-notes
77+
echo "Created tag and release $TAG."
78+
echo "created=true" >> "$GITHUB_OUTPUT"
79+
80+
# A tag created with the workflow token does not trigger the Release
81+
# workflow on its own, so the Maven Central deploy runs in this run.
82+
publish:
83+
needs: release
84+
if: needs.release.outputs.created == 'true'
85+
uses: ./.github/workflows/release.yml
86+
with:
87+
tag: ${{ needs.release.outputs.tag }}
88+
secrets: inherit

‎.github/workflows/release.yml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@ name: Release
33
on:
44
push:
55
tags: [ 'v*' ]
6+
workflow_call:
7+
inputs:
8+
tag:
9+
description: The tag to publish to Maven Central, for example v0.3.0
10+
required: true
11+
type: string
612

713
jobs:
814
release:
915
runs-on: ubuntu-latest
1016
steps:
1117
- uses: actions/checkout@v4
18+
with:
19+
ref: refs/tags/${{ inputs.tag || github.ref_name }}
1220

1321
- name: Set up JDK 11
1422
uses: actions/setup-java@v4

‎README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ An 100% compliant [ipdata.co](https://ipdata.co) API java client.
1515
- [Single Field Selection](#single-field-selection)
1616
- [Multiple Field Selection](#multiple-field-selection)
1717
- [Bulk data](#bulk-data)
18+
- [Releasing](#releasing)
1819

1920

2021
## Install
@@ -232,5 +233,10 @@ You can as well get multiple responses at once by using the ``bulk`` api:
232233
List<IpdataModel> models = ipdataService.bulk(Arrays.asList("1.1.1.1", "8.8.8.8"));
233234
```
234235

236+
## Releasing
237+
Releases are cut by CI from the version declared in `pom.xml`; there is no need to push a tag by hand:
235238

239+
1. Bump the `<version>` of the project in `pom.xml` (and the install snippet above) in a pull request and merge it.
240+
2. CI tags the merge commit `vx.y.z`, creates the GitHub release with generated notes and publishes the artifacts to Maven Central.
236241

242+
A merge whose version is already tagged publishes nothing. Pushing a `v*` tag by hand still publishes that tag's commit, as before.

0 commit comments

Comments
 (0)