Skip to content

Commit e0f5cbb

Browse files
Add ci-cd.yaml
1 parent e592b94 commit e0f5cbb

File tree

2 files changed

+178
-29
lines changed

2 files changed

+178
-29
lines changed

.github/workflows/ci-cd.yaml

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
name: Build and Publish
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
release:
8+
types: [published]
9+
10+
permissions:
11+
contents: write
12+
packages: write
13+
14+
jobs:
15+
build-and-publish:
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- name: Checkout code
20+
uses: actions/checkout@v4
21+
22+
- name: Set up Java
23+
uses: actions/setup-java@v4
24+
with:
25+
distribution: temurin
26+
java-version: 17
27+
cache: maven
28+
server-id: ${{ github.event_name == 'release' && 'central' || 'github' }}
29+
server-username: ${{ github.event_name == 'release' && 'CENTRAL_USERNAME' || 'GITHUB_ACTOR' }}
30+
server-password: ${{ github.event_name == 'release' && 'CENTRAL_TOKEN' || 'GITHUB_TOKEN' }}
31+
gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }}
32+
33+
- name: Read version from pom.xml
34+
id: get-version
35+
run: |
36+
VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
37+
echo "pom_version=$VERSION" >> $GITHUB_OUTPUT
38+
39+
- name: Validate pom.xml version has -SNAPSHOT suffix
40+
run: |
41+
POM_VERSION=${{ steps.get-version.outputs.pom_version }}
42+
echo "POM version: $POM_VERSION"
43+
if [[ "$POM_VERSION" != *"-SNAPSHOT" ]]; then
44+
echo "::error::pom.xml version must end with -SNAPSHOT, but is '$POM_VERSION'"
45+
exit 1
46+
fi
47+
48+
- name: Validate release tag matches POM version
49+
if: github.event_name == 'release'
50+
run: |
51+
TAG_VERSION=${GITHUB_REF#refs/tags/v}
52+
POM_VERSION=${{ steps.get-version.outputs.pom_version }}
53+
EXPECTED_TAG="${POM_VERSION%-SNAPSHOT}"
54+
echo "GitHub tag: $TAG_VERSION"
55+
echo "Expected from POM: $EXPECTED_TAG"
56+
if [ "$TAG_VERSION" != "$EXPECTED_TAG" ]; then
57+
echo "::error::Tag v$TAG_VERSION does not match pom.xml version $POM_VERSION without -SNAPSHOT suffix"
58+
exit 1
59+
fi
60+
61+
- name: Build and test
62+
run: mvn --batch-mode verify
63+
env:
64+
GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
65+
66+
- name: Publish Test Results
67+
uses: dorny/test-reporter@dc3a92680fcc15842eef52e8c4606ea7ce6bd3f3 # v2.1.1
68+
if: success() || failure()
69+
with:
70+
name: Test Results
71+
path: target/surefire-reports/*.xml
72+
reporter: java-junit
73+
fail-on-error: false
74+
75+
- name: Publish to GitHub Packages
76+
if: github.event_name == 'push'
77+
run: mvn --batch-mode deploy -DskipTests -DaltDeploymentRepository=github::https://maven.pkg.github.com/${{ github.repository }}
78+
env:
79+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
80+
81+
- name: Set release version for Maven Central
82+
if: github.event_name == 'release'
83+
run: |
84+
POM_VERSION=${{ steps.get-version.outputs.pom_version }}
85+
RELEASE_VERSION="${POM_VERSION%-SNAPSHOT}"
86+
mvn versions:set -DnewVersion="$RELEASE_VERSION" -DgenerateBackupPoms=false
87+
88+
- name: Update release with Maven Central links
89+
if: github.event_name == 'release'
90+
run: |
91+
RELEASE_VERSION="${{ steps.get-version.outputs.pom_version }}"
92+
RELEASE_VERSION="${RELEASE_VERSION%-SNAPSHOT}"
93+
94+
# Get existing release body first
95+
EXISTING_BODY=$(gh release view ${{ github.event.release.tag_name }} --json body -q .body)
96+
97+
# Start with existing body if it exists
98+
if [ -n "$EXISTING_BODY" ] && [ "$EXISTING_BODY" != "null" ]; then
99+
echo "$EXISTING_BODY" > release_body.md
100+
echo "" >> release_body.md
101+
echo "---" >> release_body.md
102+
echo "" >> release_body.md
103+
fi
104+
105+
# Append Maven Central links
106+
cat << EOF >> release_body.md
107+
## 📦 Maven Central
108+
109+
This release is available at https://central.sonatype.com/artifact/com.scalepoint/oauth-token-client/${RELEASE_VERSION}
110+
111+
\`\`\`xml
112+
<dependency>
113+
<groupId>com.scalepoint</groupId>
114+
<artifactId>oauth-token-client</artifactId>
115+
<version>${RELEASE_VERSION}</version>
116+
</dependency>
117+
\`\`\`
118+
EOF
119+
120+
# Update the release
121+
gh release edit ${{ github.event.release.tag_name }} --notes-file release_body.md
122+
env:
123+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
124+
125+
- name: Publish to Maven Central
126+
if: github.event_name == 'release'
127+
run: mvn --batch-mode deploy -DskipTests
128+
env:
129+
CENTRAL_USERNAME: ${{ secrets.CENTRAL_USERNAME }}
130+
CENTRAL_PASSWORD: ${{ secrets.CENTRAL_PASSWORD }}

pom.xml

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<developers>
1818
<developer>
1919
<name>Yuriy Ostapenko</name>
20-
<email>uncleyo@users.noreply.github.com</email>
20+
<email>yuriyostapenko@users.noreply.github.com</email>
2121
<organization>Scalepoint A/S</organization>
2222
<organizationUrl>https://scalepoint.com</organizationUrl>
2323
</developer>
@@ -35,10 +35,41 @@
3535
</scm>
3636

3737
<properties>
38-
<maven.compiler.release>17</maven.compiler.release>
3938
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
39+
<maven.compiler.release>17</maven.compiler.release>
40+
<central-publishing-maven-plugin.version>0.8.0</central-publishing-maven-plugin.version>
41+
<maven-compiler-plugin.version>3.14.0</maven-compiler-plugin.version>
42+
<maven-source-plugin.version>3.3.1</maven-source-plugin.version>
43+
<maven-javadoc-plugin.version>3.11.2</maven-javadoc-plugin.version>
44+
<maven-gpg-plugin.version>3.2.8</maven-gpg-plugin.version>
4045
</properties>
4146

47+
<dependencies>
48+
<dependency>
49+
<groupId>io.jsonwebtoken</groupId>
50+
<artifactId>jjwt</artifactId>
51+
<version>0.12.6</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>com.fasterxml.jackson.core</groupId>
55+
<artifactId>jackson-databind</artifactId>
56+
<version>2.19.2</version>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>org.testng</groupId>
61+
<artifactId>testng</artifactId>
62+
<version>7.11.0</version>
63+
<scope>test</scope>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.mock-server</groupId>
67+
<artifactId>mockserver-netty</artifactId>
68+
<version>5.15.0</version>
69+
<scope>test</scope>
70+
</dependency>
71+
</dependencies>
72+
4273
<profiles>
4374
<profile>
4475
<id>ci</id>
@@ -52,7 +83,7 @@
5283
<plugin>
5384
<groupId>org.sonatype.central</groupId>
5485
<artifactId>central-publishing-maven-plugin</artifactId>
55-
<version>0.8.0</version>
86+
<version>${central-publishing-maven-plugin.version}</version>
5687
<extensions>true</extensions>
5788
<configuration>
5889
<publishingServerId>central</publishingServerId>
@@ -61,6 +92,7 @@
6192
<plugin>
6293
<groupId>org.apache.maven.plugins</groupId>
6394
<artifactId>maven-compiler-plugin</artifactId>
95+
<version>${maven-compiler-plugin.version}</version>
6496
<configuration>
6597
<failOnWarning>true</failOnWarning>
6698
<showWarnings>true</showWarnings>
@@ -73,6 +105,7 @@
73105
<plugin>
74106
<groupId>org.apache.maven.plugins</groupId>
75107
<artifactId>maven-source-plugin</artifactId>
108+
<version>${maven-source-plugin.version}</version>
76109
<executions>
77110
<execution>
78111
<id>attach-sources</id>
@@ -85,6 +118,7 @@
85118
<plugin>
86119
<groupId>org.apache.maven.plugins</groupId>
87120
<artifactId>maven-javadoc-plugin</artifactId>
121+
<version>${maven-javadoc-plugin.version}</version>
88122
<executions>
89123
<execution>
90124
<id>attach-javadocs</id>
@@ -97,6 +131,7 @@
97131
<plugin>
98132
<groupId>org.apache.maven.plugins</groupId>
99133
<artifactId>maven-gpg-plugin</artifactId>
134+
<version>${maven-gpg-plugin.version}</version>
100135
<executions>
101136
<execution>
102137
<id>sign-artifacts</id>
@@ -118,31 +153,15 @@
118153
</profile>
119154
</profiles>
120155

121-
<dependencies>
122-
123-
<dependency>
124-
<groupId>io.jsonwebtoken</groupId>
125-
<artifactId>jjwt</artifactId>
126-
<version>0.12.6</version>
127-
</dependency>
128-
<dependency>
129-
<groupId>com.fasterxml.jackson.core</groupId>
130-
<artifactId>jackson-databind</artifactId>
131-
<version>2.19.2</version>
132-
</dependency>
156+
<distributionManagement>
157+
<repository>
158+
<id>central</id>
159+
<url>https://central.sonatype.com/api/v1/publish</url>
160+
</repository>
161+
<snapshotRepository>
162+
<id>github</id>
163+
<url>https://maven.pkg.github.com/scalepoint-tech/oauth-token-java-client</url>
164+
</snapshotRepository>
165+
</distributionManagement>
133166

134-
<dependency>
135-
<groupId>org.testng</groupId>
136-
<artifactId>testng</artifactId>
137-
<version>7.11.0</version>
138-
<scope>test</scope>
139-
</dependency>
140-
<dependency>
141-
<groupId>org.mock-server</groupId>
142-
<artifactId>mockserver-netty</artifactId>
143-
<version>5.15.0</version>
144-
<scope>test</scope>
145-
</dependency>
146-
147-
</dependencies>
148167
</project>

0 commit comments

Comments
 (0)