Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This workflow will build a package using Maven and then publish it to GitHub packages when a release is created | |
# For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#apache-maven-with-a-settings-path | |
# https://docs.github.com/en/actions/publishing-packages/publishing-java-packages-with-maven | |
name: Publish To Maven Central Repository | |
on: | |
release: | |
types: [ created ] | |
# 允许手工触发 | |
workflow_dispatch: | |
jobs: | |
check: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Check version | |
run: | | |
sudo apt-get update | |
sudo apt-get -y install libxml2-utils | |
# 获取 pom.xml 中的版本号,并去掉可能的 'v' 前缀 | |
POM_VERSION=$(xmllint --xpath "//*[local-name()='project']/*[local-name()='version']/text()" pom.xml | sed 's/^v//') | |
# 获取最近的发布版本号 | |
RELEASE_VERSION=$(gh api repos/${{ github.repository }}/releases/latest | jq -r .tag_name | sed 's/^v//') | |
echo Current version in POM.xml: "$POM_VERSION" | |
echo Current version of latest release: "$RELEASE_VERSION" | |
# 版本号对比 | |
if [ "$POM_VERSION" != "$RELEASE_VERSION" ]; then | |
echo -e "\e[31mError:\e[0m pom.xml version \e[31m($POM_VERSION)\e[0m does not match the latest release version ($RELEASE_VERSION)." | |
exit 1 | |
fi | |
env: | |
GH_TOKEN: ${{ github.token }} | |
build: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
# 'check' 这个 job 通过了才允许运行 | |
needs: check | |
# https://docs.github.com/en/actions/publishing-packages/publishing-java-packages-with-maven#publishing-packages-to-the-maven-central-repository | |
# https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'liberica' | |
cache: 'maven' | |
server-id: central | |
server-username: MAVEN_USERNAME | |
server-password: MAVEN_PASSWORD | |
gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }} | |
gpg-passphrase: ${{ secrets.GPG_PASSPHRASE }} | |
- name: Publish package | |
# mvn -X -e --batch-mode clean deploy | |
run: | | |
mvn -e --batch-mode clean deploy | |
env: | |
MAVEN_GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }} | |
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} | |
MAVEN_PASSWORD: ${{ secrets.OSSRH_PASSWORD }} |