1- name : Reusable workflow to deploy in Apache Maven
1+ # This workflow deploys a Maven project to Sonatype Central using a reusable action.
2+ name : Reusable Sonatype Central Deployment
23
34on :
45 workflow_call :
56 inputs :
67 maven_opts :
8+ description : ' Additional Maven CLI options (optional)'
79 type : string
810 required : false
911 secrets :
1012 MAVEN_NEXUS_USER :
13+ description : ' Sonatype Central username (token)'
1114 required : true
1215 MAVEN_NEXUS_PASSWORD :
13- required : true
14- MAVEN_GPG_PASSPHRASE :
16+ description : ' Sonatype Central password (token)'
1517 required : true
1618 MAVEN_GPG_PRIVATE_KEY :
19+ description : ' Base64-encoded GPG private key'
20+ required : true
21+ MAVEN_GPG_PASSPHRASE :
22+ description : ' Passphrase for your GPG key'
1723 required : true
1824
1925jobs :
20- deploy-workflow :
21- name : Deploy to Maven and GitHub Packages
26+ deploy :
27+ name : Deploy to Sonatype Central
2228 runs-on : ubuntu-22.04
29+ env :
30+ # Allow GPG to access a TTY if needed
31+ GPG_TTY : ${{ runner.tool_cache }}/workspace/.gpg
32+
2333 steps :
24- - uses : actions/checkout@v4
34+ - name : Checkout repository
35+ uses : actions/checkout@v4
2536 with :
26- fetch-depth : ' 10'
27- - name : Set up JDK 8
37+ # Fetch full history so tags and versions are available
38+ fetch-depth : 10
39+
40+ - name : Set up Java 8 & Maven cache
2841 uses : actions/setup-java@v4
2942 with :
30- distribution : ' temurin'
31- java-version : ' 8'
32- cache : ' maven'
33- # # Future Nacho and Juanfe, please read this very carefully: DO NOT TOUCH!!!
34- server-id : ossrh # Value of the distributionManagement/repository/id field of the pom.xml
35- server-username : MAVEN_NEXUS_USER # env variable for username in deploy
36- server-password : MAVEN_NEXUS_PASSWORD # env variable for token in deploy
37- gpg-private-key : ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} # Value of the GPG private key to import
38- gpg-passphrase : MAVEN_GPG_PASSPHRASE # env variable for GPG private key passphrase
39- - name : Deploy to Maven Central repository
40- run : mvn clean deploy -DskipTests -P deploy-maven ${{ inputs.maven_opts }} --no-transfer-progress
43+ distribution : ' temurin' # Eclipse Temurin JDK
44+ java-version : ' 8' # Java 8 compatibility
45+ cache : ' maven' # Cache dependencies
46+
47+ - name : Import GPG private key
48+ # Decode and import your Base64-encoded private key
4149 env :
42- MAVEN_NEXUS_USER : ${{ secrets.MAVEN_USER_TOKEN }}
43- MAVEN_NEXUS_PASSWORD : ${{ secrets.MAVEN_PASSWORD_TOKEN }}
44- MAVEN_GPG_PASSPHRASE : ${{ secrets.MAVEN_GPG_PASSPHRASE }}
45- - name : Set up Java for publishing to GitHub Packages
46- uses : actions/setup-java@v4
47- with :
48- distribution : ' temurin'
49- java-version : ' 8'
50+ GPG_KEY : ${{ secrets.MAVEN_GPG_PRIVATE_KEY }}
51+ GPG_PASSPHRASE : ${{ secrets.MAVEN_GPG_PASSPHRASE }}
52+ run : |
53+ echo "$GPG_KEY" | base64 -d > private.key
54+ gpg --batch --import private.key
55+ rm private.key
56+
57+ - name : Generate Maven settings.xml
58+ # Create settings.xml with Sonatype Central credentials & GPG profile
59+ run : |
60+ cat > settings.xml <<EOF
61+ <settings>
62+ <servers>
63+ <server>
64+ <id>central</id>
65+ <username>${{ secrets.MAVEN_NEXUS_USER }}</username>
66+ <password>${{ secrets.MAVEN_NEXUS_PASSWORD }}</password>
67+ </server>
68+ </servers>
69+ <profiles>
70+ <profile>
71+ <id>gpg</id>
72+ <properties>
73+ <gpg.passphrase>${{ secrets.MAVEN_GPG_PASSPHRASE }}</gpg.passphrase>
74+ </properties>
75+ </profile>
76+ </profiles>
77+ </settings>
78+ EOF
79+
80+ - name : Deploy to Central
81+ # A single Maven deploy picks SNAPSHOT vs Release by your POM’s version
82+ run : mvn clean deploy -DskipTests -P deploy-maven -s settings.xml ${{ inputs.maven_opts }} --no-transfer-progress
5083 - name : Deploy to GitHub Packages repository
5184 run : mvn clean deploy -DskipTests -P deploy-github ${{ inputs.maven_opts }} --no-transfer-progress
5285 env :
53- GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
86+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
0 commit comments