release-1.0.0 #6
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 | |
name: Maven Release | |
on: | |
pull_request: | |
types: [closed] | |
jobs: | |
release: | |
runs-on: ubuntu-latest | |
name: release | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
- name: Read Pull Request Title | |
id: pr-title | |
run: | | |
title=$(cat $GITHUB_EVENT_PATH | jq -r '.pull_request.title') | |
echo "Pull Request Title: $title" | |
echo "pr_title=$title" >> $GITHUB_OUTPUT | |
- name: Extract info from pom.xml | |
id: extract-info | |
run: | | |
groupId=$(mvn help:evaluate -Dexpression=project.groupId -q -DforceStdout) | |
artifactId=$(mvn help:evaluate -Dexpression=project.artifactId -q -DforceStdout) | |
version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout) | |
echo "Pom GroupId: $groupId" | |
echo "Pom ArtifactId: $artifactId" | |
echo "Pom Version: $version" | |
echo "version=$version" >> $GITHUB_OUTPUT | |
echo "artifactId=$artifactId" >> $GITHUB_OUTPUT | |
echo "groupId=$groupId" >> $GITHUB_OUTPUT | |
- name: Check Maven Central Repository | |
id: check-maven-central | |
run: | | |
version="${{ steps.extract-info.outputs.version }}" | |
groupId="${{ steps.extract-info.outputs.groupId }}" | |
artifactId="${{ steps.extract-info.outputs.artifactId }}" | |
URL=https://repo1.maven.org/maven2/$(echo $groupId | tr . /)/${artifactId}/maven-metadata.xml | |
echo $URL | |
# 检查 URL 是否存在 | |
RESPONSE_CODE=$(curl -sSL -o /dev/null -w "%{http_code}" "$URL") | |
echo $RESPONSE_CODE | |
if [ "$RESPONSE_CODE" == "200" ]; then | |
# 文件存在,继续检查版本 | |
RESPONSE=$(curl -sSL "$URL") | |
if echo "$RESPONSE" | grep "<version>$version</version>" > /dev/null; then | |
echo "Version $version exists in Maven Central." | |
exit 1 | |
else | |
echo "Version $version does not exist in Maven Central." | |
fi | |
elif [ "$RESPONSE_CODE" == "404" ]; then | |
echo "The metadata file for $groupId:$artifactId does not exist in Maven Central." | |
else | |
echo "An unexpected error occurred while checking the metadata file for $groupId:$artifactId." | |
fi | |
- name: Validate Pull Request Title | |
run: | # 读取 Pull Request 标题和版本号 | |
title="${{ steps.pr-title.outputs.pr_title }}" | |
version="${{ steps.extract-info.outputs.version }}" | |
echo "Pull Request Title: $title" | |
echo "Version: $version" | |
# 转换为小写并进行条件判断 | |
if [[ "${title,,}" =~ ^release-$version$ ]]; then | |
echo "Pull Request title is valid." | |
else | |
echo "Pull Request title must be in the format 'release-$version'." | |
exit 1 | |
fi | |
- name: Set up Maven Central Repository | |
uses: actions/setup-java@v4 | |
with: # running setup-java again overwrites the settings.xml | |
java-version: '8' | |
distribution: 'temurin' | |
server-id: central # Value of the distributionManagement/repository/id field of the pom.xml | |
server-username: MAVEN_USERNAME # env variable for username in deploy | |
server-password: MAVEN_CENTRAL_TOKEN # env variable for token in deploy | |
gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }} # Value of the GPG private key to import | |
gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable for GPG private key passphrase | |
- name: Publish to Apache Maven Central | |
run: mvn -B deploy | |
env: | |
MAVEN_USERNAME: ${{ secrets.MAVEN_CENTRAL_USERNAME }} | |
MAVEN_CENTRAL_TOKEN: ${{ secrets.MAVEN_CENTRAL_PASSWORD }} | |
MAVEN_GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }} | |