Publish to Maven Central #4
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 | |
| jobs: | |
| publish: | |
| runs-on: ubuntu-latest | |
| env: | |
| OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }} | |
| OSSRH_PASSWORD: ${{ secrets.OSSRH_PASSWORD }} | |
| GPR_USER: ${{ secrets.GPR_USER }} | |
| GPR_TOKEN: ${{ secrets.GPR_TOKEN }} | |
| ANDROID_SDK_ROOT: /usr/local/lib/android/sdk | |
| ANDROID_HOME: /usr/local/lib/android/sdk | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v6 | |
| with: | |
| submodules: recursive | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Set up Java | |
| uses: actions/setup-java@v5 | |
| 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: Determine modules to publish and deploy 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 }} | |
| run: | | |
| # Disable -e from the default shell so we can handle failures explicitly. | |
| # Keep -u (undefined vars) and pipefail (pipeline errors). | |
| set +e | |
| set -uo pipefail | |
| PUBLISHABLE_MODULES=( | |
| mapconductor-bom | |
| ) | |
| GROUP_ID="com.mapconductor" | |
| GROUP_PATH="${GROUP_ID//./\/}" | |
| publish_tasks=() | |
| for module in "${PUBLISHABLE_MODULES[@]}"; do | |
| props_file="$module/gradle.properties" | |
| if [[ ! -f "$props_file" ]]; then | |
| echo "Skipping $module: gradle.properties not found" | |
| continue | |
| fi | |
| local_version=$(grep '^libraryVersion=' "$props_file" | cut -d'=' -f2 | tr -d '\r') | |
| if [[ -z "$local_version" ]]; then | |
| echo "Skipping $module: libraryVersion not found" | |
| continue | |
| fi | |
| artifactId=$(grep -m1 'libraryArtifactId' "$module/build.gradle.kts" | sed -E 's/.*\"(.+)\".*/\1/' | tr -d '\r') | |
| if [[ -z "$artifactId" ]]; then | |
| echo "Skipping $module: libraryArtifactId not found" | |
| continue | |
| fi | |
| metadata_url="https://repo1.maven.org/maven2/${GROUP_PATH}/${artifactId}/maven-metadata.xml" | |
| echo "Checking ${GROUP_ID}:${artifactId} (local version ${local_version})" | |
| remote_version="" | |
| if curl -fsSL "$metadata_url" -o metadata.xml; then | |
| # Prefer <release>, fall back to last <version> | |
| 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 (likely not published yet)." | |
| fi | |
| if [[ -n "$remote_version" && "$local_version" == "$remote_version" ]]; then | |
| echo " Already published (remote version ${remote_version}); skipping." | |
| else | |
| echo " Will publish local version ${local_version} (remote='${remote_version}')." | |
| publish_tasks+=(":${module}:publishAllPublicationsToCentralPortal") | |
| fi | |
| done | |
| # If no modules need publishing, exit successfully | |
| if [[ "${#publish_tasks[@]}" -eq 0 ]]; then | |
| echo "No modules require publishing." | |
| exit 0 | |
| fi | |
| # Read coreLibraryVersion from android-sdk-core/gradle.properties and inject | |
| # as a Gradle project property so submodule build scripts can reference it | |
| # regardless of variable declaration order in their build.gradle.kts. | |
| CORE_LIBRARY_VERSION=$(grep '^libraryVersion=' android-sdk-core/gradle.properties | cut -d'=' -f2 | tr -d '\r') | |
| if [[ -z "$CORE_LIBRARY_VERSION" ]]; then | |
| echo "ERROR: Could not read libraryVersion from android-sdk-core/gradle.properties" | |
| exit 1 | |
| fi | |
| echo "Injecting coreLibraryVersion=${CORE_LIBRARY_VERSION}" | |
| echo "Publishing modules with tasks: ${publish_tasks[*]}" | |
| ./gradlew --no-daemon -PcoreLibraryVersion="${CORE_LIBRARY_VERSION}" "${publish_tasks[@]}" | |
| gradle_exit=$? | |
| if [[ $gradle_exit -ne 0 ]]; then | |
| echo "Gradle publish failed with exit code $gradle_exit" | |
| exit $gradle_exit | |
| fi |