Publish to Maven Central #3
This file contains hidden or 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
| name: Publish to Maven Central | |
| on: | |
| workflow_dispatch: | |
| permissions: | |
| contents: read | |
| packages: read | |
| jobs: | |
| publish: | |
| runs-on: ubuntu-latest | |
| env: | |
| OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }} | |
| OSSRH_PASSWORD: ${{ secrets.OSSRH_PASSWORD }} | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Set up Java | |
| uses: actions/setup-java@v4 | |
| with: | |
| distribution: temurin | |
| java-version: '17' | |
| cache: 'gradle' | |
| - name: Import GPG key | |
| run: | | |
| echo "${{ secrets.GPG_PRIVATE_KEY }}" | base64 --decode > "$HOME/secring.gpg" | |
| gpg --batch --import "$HOME/secring.gpg" | |
| - name: Check core availability (Maven Central or GitHub Packages) | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| CORE_VERSION=$(grep '^coreLibraryVersion=' gradle.properties | cut -d'=' -f2 | tr -d '\r') | |
| if [[ -z "$CORE_VERSION" ]]; then | |
| CORE_VERSION=$(grep '^libraryVersion=' gradle.properties | cut -d'=' -f2 | tr -d '\r') | |
| fi | |
| if [[ -z "$CORE_VERSION" ]]; then | |
| echo "ERROR: Could not determine core version from gradle.properties" | |
| exit 1 | |
| fi | |
| echo "Checking com.mapconductor:core:${CORE_VERSION}" | |
| MC_URL="https://repo1.maven.org/maven2/com/mapconductor/core/${CORE_VERSION}/core-${CORE_VERSION}.pom" | |
| if curl -fsSL "$MC_URL" > /dev/null 2>&1; then | |
| echo "Found on Maven Central" | |
| exit 0 | |
| fi | |
| GPR_URL="https://maven.pkg.github.com/MapConductor/android-sdk-core/com/mapconductor/core/${CORE_VERSION}/core-${CORE_VERSION}.pom" | |
| if curl -fsSL -H "Authorization: Bearer ${GITHUB_TOKEN}" "$GPR_URL" > /dev/null 2>&1; then | |
| echo "Found on GitHub Packages" | |
| exit 0 | |
| fi | |
| echo "ERROR: com.mapconductor:core:${CORE_VERSION} not found on Maven Central or GitHub Packages." | |
| echo "Please publish android-sdk-core first." | |
| exit 1 | |
| - name: Check version and publish to Maven Central | |
| env: | |
| ORG_GRADLE_PROJECT_ossrh_username: ${{ secrets.OSSRH_USERNAME }} | |
| ORG_GRADLE_PROJECT_ossrh_password: ${{ secrets.OSSRH_PASSWORD }} | |
| ORG_GRADLE_PROJECT_signingKey: ${{ secrets.GPG_PRIVATE_KEY }} | |
| ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.GPG_PASSPHRASE }} | |
| GITHUB_ACTOR: ${{ github.actor }} | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| set +e | |
| set -uo pipefail | |
| GROUP_ID="com.mapconductor" | |
| GROUP_PATH="${GROUP_ID//./\/}" | |
| ARTIFACT_ID="for-googlemaps" | |
| local_version=$(grep '^libraryVersion=' gradle.properties | cut -d'=' -f2 | tr -d '\r') | |
| if [[ -z "$local_version" ]]; then | |
| echo "ERROR: Could not read libraryVersion from gradle.properties" | |
| exit 1 | |
| fi | |
| metadata_url="https://repo1.maven.org/maven2/${GROUP_PATH}/${ARTIFACT_ID}/maven-metadata.xml" | |
| echo "Checking ${GROUP_ID}:${ARTIFACT_ID} (local version ${local_version})" | |
| remote_version="" | |
| if curl -fsSL "$metadata_url" -o metadata.xml; then | |
| remote_version=$(grep '<release>' metadata.xml | head -n1 | sed -E 's/.*<release>([^<]+)<\/release>.*/\1/' || true) | |
| if [[ -z "$remote_version" ]]; then | |
| remote_version=$(grep '<version>' metadata.xml | sed -E 's/.*<version>([^<]+)<\/version>.*/\1/' | tail -n1 || true) | |
| fi | |
| rm -f metadata.xml | |
| else | |
| echo " No metadata found on Maven Central (not published yet)." | |
| fi | |
| if [[ -n "$remote_version" && "$local_version" == "$remote_version" ]]; then | |
| echo " Already published (remote version ${remote_version}); skipping." | |
| exit 0 | |
| fi | |
| echo " Will publish local version ${local_version} (remote='${remote_version}')." | |
| ./gradlew --no-daemon publishReleasePublicationToCentralPortal | |
| gradle_exit=$? | |
| if [[ $gradle_exit -ne 0 ]]; then | |
| echo "Gradle publish failed with exit code $gradle_exit" | |
| exit $gradle_exit | |
| fi |