From 97f725ac57f2e3672f91bb8cbefe1f7064b0c4d8 Mon Sep 17 00:00:00 2001 From: Carl Poole Date: Mon, 30 Aug 2021 10:36:34 -0500 Subject: [PATCH] chore(android): mavencentral publishing and CI (#4961) --- .github/workflows/ci.yml | 25 +++++++++ android/capacitor/build.gradle | 7 +++ android/scripts/publish-module.gradle | 77 +++++++++++++++++++++++++++ android/scripts/publish-root.gradle | 37 +++++++++++++ android/scripts/release.sh | 0 scripts/.gitignore | 1 + scripts/publish-android.sh | 37 +++++++++++++ 7 files changed, 184 insertions(+) create mode 100644 android/scripts/publish-module.gradle create mode 100644 android/scripts/publish-root.gradle delete mode 100644 android/scripts/release.sh create mode 100644 scripts/.gitignore create mode 100755 scripts/publish-android.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8696b4ab2e..45952cfab7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -169,4 +169,29 @@ jobs: key: ${{ runner.OS }}-dependency-cache-${{ hashFiles('**/package.json') }} - run: npm install - run: npm run lerna:publish:latest || true + publish-android: + runs-on: ubuntu-latest + needs: + - deploy-latest + steps: + - uses: actions/checkout@v2 + - name: set up JDK 11 + uses: actions/setup-java@v2 + with: + java-version: '11' + distribution: 'adopt' + - name: Grant execute permission for gradlew + run: chmod +x ./android/gradlew + - name: Grant execute permission for publishing script + run: chmod +x ./scripts/publish-android.sh + - name: Run publish script + working-directory: ./scripts + env: + ANDROID_OSSRH_USERNAME: ${{ secrets.ANDROID_OSSRH_USERNAME }} + ANDROID_OSSRH_PASSWORD: ${{ secrets.ANDROID_OSSRH_PASSWORD }} + ANDROID_SIGNING_KEY_ID: ${{ secrets.ANDROID_SIGNING_KEY_ID }} + ANDROID_SIGNING_PASSWORD: ${{ secrets.ANDROID_SIGNING_PASSWORD }} + ANDROID_SIGNING_KEY: ${{ secrets.ANDROID_SIGNING_KEY }} + ANDROID_SONATYPE_STAGING_PROFILE_ID: ${{ secrets.ANDROID_SONATYPE_STAGING_PROFILE_ID }} + run: ./publish-android.sh diff --git a/android/capacitor/build.gradle b/android/capacitor/build.gradle index ae96db1fb0..2fb5fc1aad 100644 --- a/android/capacitor/build.gradle +++ b/android/capacitor/build.gradle @@ -15,15 +15,22 @@ buildscript { repositories { google() mavenCentral() + maven { + url "https://plugins.gradle.org/m2/" + } } dependencies { classpath 'com.android.tools.build:gradle:4.2.1' + classpath 'io.github.gradle-nexus:publish-plugin:1.1.0' } } tasks.withType(Javadoc).all { enabled = false } apply plugin: 'com.android.library' +apply plugin: 'io.github.gradle-nexus.publish-plugin' +apply from: file('../scripts/publish-root.gradle') +apply from: file('../scripts/publish-module.gradle') android { compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 30 diff --git a/android/scripts/publish-module.gradle b/android/scripts/publish-module.gradle new file mode 100644 index 0000000000..2a8a32e0c3 --- /dev/null +++ b/android/scripts/publish-module.gradle @@ -0,0 +1,77 @@ +apply plugin: 'maven-publish' +apply plugin: 'signing' + +def LIB_VERSION = System.getenv('CAP_VERSION') + +task androidSourcesJar(type: Jar) { + archiveClassifier.set('sources') + if (project.plugins.findPlugin("com.android.library")) { + from android.sourceSets.main.java.srcDirs + } else { + from sourceSets.main.java.srcDirs + } +} + +artifacts { + archives androidSourcesJar +} + +group = 'com.capacitorjs' +version = LIB_VERSION + +afterEvaluate { + publishing { + publications { + release(MavenPublication) { + // Coordinates + groupId 'com.capacitorjs' + artifactId 'core' + version LIB_VERSION + + // Two artifacts, the `aar` (or `jar`) and the sources + if (project.plugins.findPlugin("com.android.library")) { + from components.release + } else { + artifact("$buildDir/libs/${project.getName()}-${version}.jar") + } + + artifact androidSourcesJar + + // POM Data + pom { + name = 'core' + description = 'Capacitor Android Core Native Library' + url = 'https://github.com/ionic-team/capacitor' + licenses { + license { + name = 'MIT' + url = 'https://github.com/ionic-team/capacitor/blob/main/LICENSE' + } + } + developers { + developer { + name = 'Ionic' + email = 'hi@ionic.io' + } + } + + // Version Control Info + scm { + connection = 'scm:git:github.com:ionic-team/capacitor.git' + developerConnection = 'scm:git:ssh://github.com:ionic-team/capacitor.git' + url = 'https://github.com/ionic-team/capacitor/tree/main' + } + } + } + } + } +} + +signing { + useInMemoryPgpKeys( + rootProject.ext["signing.keyId"], + rootProject.ext["signing.key"], + rootProject.ext["signing.password"], + ) + sign publishing.publications +} diff --git a/android/scripts/publish-root.gradle b/android/scripts/publish-root.gradle new file mode 100644 index 0000000000..d6b229d3d0 --- /dev/null +++ b/android/scripts/publish-root.gradle @@ -0,0 +1,37 @@ +// Create variables with empty default values +ext["signing.keyId"] = '' +ext["signing.key"] = '' +ext["signing.password"] = '' +ext["ossrhUsername"] = '' +ext["ossrhPassword"] = '' +ext["sonatypeStagingProfileId"] = '' + +File secretPropsFile = file('../local.properties') +if (secretPropsFile.exists()) { + // Read local.properties file first if it exists + Properties p = new Properties() + new FileInputStream(secretPropsFile).withCloseable { is -> p.load(is) } + p.each { name, value -> ext[name] = value } +} else { + // Use system environment variables + ext["ossrhUsername"] = System.getenv('ANDROID_OSSRH_USERNAME') + ext["ossrhPassword"] = System.getenv('ANDROID_OSSRH_PASSWORD') + ext["sonatypeStagingProfileId"] = System.getenv('ANDROID_SONATYPE_STAGING_PROFILE_ID') + ext["signing.keyId"] = System.getenv('ANDROID_SIGNING_KEY_ID') + ext["signing.key"] = System.getenv('ANDROID_SIGNING_KEY') + ext["signing.password"] = System.getenv('ANDROID_SIGNING_PASSWORD') +} + +// Set up Sonatype repository +nexusPublishing { + repositories { + sonatype { + stagingProfileId = sonatypeStagingProfileId + username = ossrhUsername + password = ossrhPassword + nexusUrl.set(uri("https://s01.oss.sonatype.org/service/local/")) + snapshotRepositoryUrl.set(uri("https://s01.oss.sonatype.org/content/repositories/snapshots/")) + } + } + repositoryDescription = 'Capacitor Android Core v' + System.getenv('CAP_VERSION') +} diff --git a/android/scripts/release.sh b/android/scripts/release.sh deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/scripts/.gitignore b/scripts/.gitignore new file mode 100644 index 0000000000..a9a5aecf42 --- /dev/null +++ b/scripts/.gitignore @@ -0,0 +1 @@ +tmp diff --git a/scripts/publish-android.sh b/scripts/publish-android.sh new file mode 100755 index 0000000000..cdc6afbc68 --- /dev/null +++ b/scripts/publish-android.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash + +DIR=../android +LOG_OUTPUT=./tmp/capacitor-android.txt +CAP_VERSION=`grep '"version": ' $DIR/package.json | awk '{print $2}' | tr -d '",'` +echo Attempting to build and publish Capacitor native libraries with version $CAP_VERSION + +# Make log dir if doesnt exist +mkdir -p ./tmp + +# Export ENV variable used by Gradle for Versioning +export CAP_VERSION + +# Get latest com.capacitorjs:core XML version info +CAPACITOR_PUBLISHED_URL="https://repo1.maven.org/maven2/com/capacitorjs/core/maven-metadata.xml" +CAPACITOR_PUBLISHED_DATA=$(curl -s $CAPACITOR_PUBLISHED_URL) +CAPACITOR_PUBLISHED_VERSION="$(perl -ne 'print and last if s/.*(.*)<\/latest>.*/\1/;' <<< $CAPACITOR_PUBLISHED_DATA)" + +# Check if we need to publish a new native version of the Capacitor Android library +if [[ $CAP_VERSION == $CAPACITOR_PUBLISHED_VERSION ]]; then + printf %"s\n" "Native Capacitor Android library version $CAPACITOR_PUBLISHED_VERSION is already published on MavenCentral, skipping." +else + printf %"s\n" "Latest native Capacitor Android library is version $CAPACITOR_PUBLISHED_VERSION, publishing to MavenCentral staging..." + + # Build and publish + $DIR/gradlew clean build publishReleasePublicationToSonatypeRepository --max-workers 1 -b $DIR/capacitor/build.gradle -Pandroid.useAndroidX=true -Pandroid.enableJetifier=true > $LOG_OUTPUT 2>&1 + + echo $RESULT + + if grep --quiet "BUILD SUCCESSFUL" $LOG_OUTPUT; then + printf %"s\n" "Success: Capacitor Android Library published to MavenCentral Staging. Manually review and release from the Sonatype Repository Manager https://s01.oss.sonatype.org/" + else + printf %"s\n" "Error publishing, check $LOG_OUTPUT for more info!" + cat $LOG_OUTPUT + exit 1 + fi +fi