Deploying to Maven Central using GitHub Actions
I have this library that I work on from time to time, that I published on Maven Central. A few days ago I released
a new version, and since I always forget what the exact mvn
command is, what my GPG passphrase is, etc. I thought it
would be a good opportunity to automate the release process using GitHub Actions.
I came across several outdated tutorials that create settings.xml
and import GPG keys manually, but as of Feb. 2021 most
of this is not needed anymore, thanks to recent changes in the setup-java
action.
Prerequisites
From now on, I will assume that you already know how to deploy on Maven Central via Sonatype OSSRH. This means
you created an account on Sonatype’s Jira, your local settings.xml
is already configured and your pom.xml
has a
<plugins>
section that looks more or less like this:
Entering GitHub Actions
Java Actions workflows often use a setup-java
action which… well… sets up Java in the build runner:
I thought this action was only used to download a JDK, but it turns out it can do more than that: it also knows
how to set up the runner to publish artifacts on Maven Central (or any <distributionManagement>
configured in your pom.xml
, for that matter):
As explained in the readme, the second invocation of actions/setup-java@v1
will overwrite the runner’s
settings.xml
with your Sonatype credentials and GPG passphrase, using environment variables:
The private GPG key stored in the MAVEN_GPG_PRIVATE_KEY
secret will also be imported in a GPG keychain, allowing
maven-gpg-plugin
to sign your artifacts correctly.
Adding the missing pieces
In order to make the workflow run smoothly, I needed two additional pieces of information that are not (yet) mentionned in the readme:
- to fill the
MAVEN_GPG_PRIVATE_KEY
secret, you need to export your private GPG key using this command:gpg --armor --export-secret-keys KEY_ID
- to avoid a
gpg: signing failed: Inappropriate ioctl for device
error, you need to configuremaven-gpg-plugin
like this:
Final note
Setting up this workflow was not that difficult, except for the two missing pieces that I fortunately fixed very easily, thanks to issues and comments from other people who had the same problem before me. 10/10, would recommend 😜.
My final release workflow is available on GitHub.