Skip to content

Commit 3a278b6

Browse files
author
Marvin Zhang
committed
chore: Revamp GitHub Actions workflow for Maven Central and GitHub Packages publishing
- Updated the workflow to publish artifacts to both Maven Central and GitHub Packages. - Enhanced JDK setup and added caching for Maven dependencies to improve build efficiency. - Configured dynamic Maven settings for secure deployment, including GPG key import and passphrase handling. - Streamlined the build and publish steps, ensuring proper GPG signing and artifact management.
1 parent e3942b1 commit 3a278b6

File tree

1 file changed

+84
-30
lines changed

1 file changed

+84
-30
lines changed

.github/workflows/publish.yml

+84-30
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,91 @@
1-
# This workflow will build a package using Maven and then publish it to GitHub packages when a release is created
2-
# For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#apache-maven-with-a-settings-path
3-
4-
name: Maven Package
1+
name: Publish to Maven Central and GitHub Packages
52

63
on:
74
push:
8-
branches: [main]
5+
branches:
6+
- main
97

108
jobs:
11-
build:
12-
9+
publish:
1310
runs-on: ubuntu-latest
14-
permissions:
15-
contents: read
16-
packages: write
17-
1811
steps:
19-
- uses: actions/checkout@v4
20-
- name: Set up JDK 17
21-
uses: actions/setup-java@v4
22-
with:
23-
java-version: '17'
24-
distribution: 'temurin'
25-
server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
26-
settings-path: ${{ github.workspace }} # location for the settings.xml file
27-
28-
- name: Build and Test
29-
run: mvn clean test
30-
31-
- name: Build with Maven
32-
run: mvn -B package --file pom.xml
33-
34-
- name: Publish to GitHub Packages Apache Maven
35-
run: mvn deploy -s $GITHUB_WORKSPACE/settings.xml
36-
env:
37-
GITHUB_TOKEN: ${{ github.token }}
12+
- uses: actions/checkout@v3
13+
14+
- name: Set up JDK 17
15+
uses: actions/setup-java@v3
16+
with:
17+
java-version: '17'
18+
distribution: 'temurin'
19+
server-id: ossrh # Maven Central settings server id
20+
server-username: ${{ secrets.OSSRH_USERNAME }}
21+
server-password: ${{ secrets.OSSRH_PASSWORD }}
22+
gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }}
23+
gpg-passphrase: ${{ secrets.GPG_PASSPHRASE }}
24+
25+
- name: Cache Maven dependencies
26+
uses: actions/cache@v3
27+
with:
28+
path: ~/.m2/repository
29+
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
30+
restore-keys: |
31+
${{ runner.os }}-m2-
32+
33+
- name: Setup Maven settings
34+
run: |
35+
mkdir -p ~/.m2
36+
echo "<settings>
37+
<servers>
38+
<server>
39+
<id>ossrh</id>
40+
<username>${{ secrets.OSSRH_USERNAME }}</username>
41+
<password>${{ secrets.OSSRH_PASSWORD }}</password>
42+
</server>
43+
<server>
44+
<id>github</id>
45+
<username>${{ secrets.GITHUB_ACTOR }}</username>
46+
<password>${{ secrets.GITHUB_TOKEN }}</password>
47+
</server>
48+
</servers>
49+
<profiles>
50+
<profile>
51+
<id>ossrh</id>
52+
<repositories>
53+
<repository>
54+
<id>ossrh</id>
55+
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
56+
</repository>
57+
</repositories>
58+
</profile>
59+
<profile>
60+
<id>github</id>
61+
<repositories>
62+
<repository>
63+
<id>github</id>
64+
<url>https://maven.pkg.github.com/crawlab-team/crawlab-java-sdk</url>
65+
</repository>
66+
</repositories>
67+
</profile>
68+
</profiles>
69+
<activeProfiles>
70+
<activeProfile>ossrh</activeProfile>
71+
<activeProfile>github</activeProfile>
72+
</activeProfiles>
73+
</settings>" > ~/.m2/settings.xml
74+
75+
- name: Import GPG key
76+
run: |
77+
echo "${{ secrets.GPG_PRIVATE_KEY }}" | gpg --batch --import
78+
env:
79+
GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
80+
81+
- name: Set GPG_TTY
82+
run: echo "GPG_TTY=$(tty)" >> $GITHUB_ENV
83+
84+
- name: Reload gpg-agent
85+
run: gpg-connect-agent reloadagent /bye
86+
87+
- name: Build and Test
88+
run: mvn clean test
89+
90+
- name: Build and Publish to Maven Central and GitHub Packages
91+
run: mvn clean deploy -P ossrh,github -Dgpg.passphrase=${{ secrets.GPG_PASSPHRASE }}

0 commit comments

Comments
 (0)