Skip to content

Commit

Permalink
chore(android): mavencentral publishing and CI (#4961)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlpoole committed Aug 30, 2021
1 parent 4f35202 commit 97f725a
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -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

7 changes: 7 additions & 0 deletions android/capacitor/build.gradle
Expand Up @@ -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
Expand Down
77 changes: 77 additions & 0 deletions 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
}
37 changes: 37 additions & 0 deletions 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')
}
Empty file removed android/scripts/release.sh
Empty file.
1 change: 1 addition & 0 deletions scripts/.gitignore
@@ -0,0 +1 @@
tmp
37 changes: 37 additions & 0 deletions 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>(.*)<\/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

0 comments on commit 97f725a

Please sign in to comment.