Skip to content

Instantly share code, notes, and snippets.

@kibotu
Last active March 29, 2022 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kibotu/dbcf2fe338f2e2530980f753608288d6 to your computer and use it in GitHub Desktop.
Save kibotu/dbcf2fe338f2e2530980f753608288d6 to your computer and use it in GitHub Desktop.
Deploy aab to Huawei AppGallery.
// based on https://github.com/ferPrieto/steps-app-gallery-deploy/blob/main/step.sh
import groovy.json.JsonSlurper
ext {
def properties = project.gradle.startParameter.projectProperties
println(properties)
// https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agcapi-getstarted-0000001111845114
// IMPORTANT: when creating api key: 'N/A' as Project
// start parameters
filePath = properties.getOrDefault("aab", "")
clientId = properties.getOrDefault("clientId", "")
clientSecret = properties.getOrDefault("clientSecret", "")
appId = properties.getOrDefault("appId", "")
submitForReview = properties.getOrDefault("submitForReview", "false")
releaseType = properties.getOrDefault("releaseType", "1") // 1: on the entire network, 3: by phase
// internal variables
accessToken = ""
uploadUrl = ""
authCode = ""
fileDestUrl = ""
fileSize = ""
}
tasks.register('getToken') {
doLast {
new ByteArrayOutputStream().withStream { os ->
exec {
executable "/bin/sh"
def json = """
{
"grant_type" : "client_credentials",
"client_id": "$clientId",
"client_secret": "$clientSecret"
}
""".stripMargin().stripIndent()
def request = "curl -X POST https://connect-api.cloud.huawei.com/api/oauth2/v1/token " +
"-H 'Content-Type: application/json' " +
"-H 'cache-control: no-cache' " +
"-d '$json'"
println("$request")
args "-c", request
standardOutput = os
}
def response = os.toString()
println("$response")
def jsonResponse = new JsonSlurper()
.parseText(response)
accessToken = jsonResponse.access_token
}
}
}
tasks.register('getFileUploadUrl') {
dependsOn getToken
doLast {
def fileExtension = filePath.drop(filePath.lastIndexOf('.') + 1)
new ByteArrayOutputStream().withStream { os ->
exec {
executable "/bin/sh"
def request = "curl -X GET 'https://connect-api.cloud.huawei.com/api/publish/v2/upload-url?appId=$appId&suffix=$fileExtension&releaseType=$releaseType' " +
"-H 'Authorization: Bearer $accessToken' " +
"-H 'Content-Type: application/json' " +
"-H 'client_id: $clientId'"
println("$request")
args "-c", request
standardOutput = os
}
def response = os.toString()
println("$response")
def jsonResponse = new JsonSlurper()
.parseText(response)
uploadUrl = jsonResponse.uploadUrl
authCode = jsonResponse.authCode
}
}
}
tasks.register('uploadFile') {
dependsOn getFileUploadUrl
doLast {
new ByteArrayOutputStream().withStream { os ->
def request = exec {
executable "/bin/sh"
def request = "curl -X POST '$uploadUrl' " +
"-H 'Accept: application/json' " +
"-F 'authCode=$authCode' " +
"-F 'fileCount=1' " +
"-F 'parseType=1' " +
"-F 'file=@${filePath}'"
println("$request")
args "-c", request
standardOutput = os
}
println("request=$request")
def response = os.toString()
println("$response")
def jsonResponse = new JsonSlurper()
.parseText(os.toString())
def fileInfoList = jsonResponse.result.UploadFileRsp.fileInfoList[0]
fileDestUrl = fileInfoList.fileDestUlr
fileSize = fileInfoList.size
}
}
}
tasks.register('updateAppFileInfo') {
dependsOn uploadFile
doLast {
new ByteArrayOutputStream().withStream { os ->
exec {
executable "/bin/sh"
def fileName = new File(filePath).name
def json = """
{
"fileType": "5",
"files": [
{
"fileName": "$fileName",
"fileDestUrl": "$fileDestUrl",
"size": "$fileSize"
}
]
}
""".stripMargin().stripIndent()
def request = "curl -X PUT 'https://connect-api.cloud.huawei.com/api/publish/v2/app-file-info?appId=$appId' " +
"-H 'Authorization: Bearer $accessToken' " +
"-H 'Content-Type: application/json' " +
"-H 'releaseType: $releaseType' " +
"-H 'client_id: $clientId' " +
"-d '$json'"
println("$request")
args "-c", request
standardOutput = os
}
def response = os.toString()
println("$response")
def jsonResponse = new JsonSlurper()
.parseText(response)
}
}
}
tasks.register('submitAppDirectly') {
dependsOn getFileUploadUrl
doLast {
new ByteArrayOutputStream().withStream { os ->
exec {
executable "/bin/sh"
def request = "curl -X POST https://connect-api.cloud.huawei.com/api/publish/v2/app-submit?appid=$appId " +
"-H 'Authorization: Bearer $accessToken' " +
"-H 'client_id: $clientId'"
println(request)
args "-c", request
standardOutput = os
}
def response = os.toString()
println("$response")
def jsonResponse = new JsonSlurper()
.parseText(response)
}
}
}
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o xtrace
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
# provided artifact
AAB=$(find "$(pwd -P)" -name "*.aab")
# updateAppFileInfo - uploads aar file to huawei appgallery and update aab infos to respective project/app
# does not submit for review yet
# todo submitAppDirectly - also submits to phased / direct review
./gradlew updateAppFileInfo --s \
-Paab=$AAB -PclientId=${huawei_secret_client_id} \
-PclientSecret=${huawei_client_secret} \
-PappId=${huawei_secret_app_id} \
-PsubmitForReview=false \
-PreleaseType=1 # 1: on the entire network, 3: by phase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment