96 lines
3.0 KiB
Groovy
96 lines
3.0 KiB
Groovy
//=================================================================
|
|
// Byway Studios Inc. Confidential Information.
|
|
// Copyright Byway Studios Inc. All rights reserved
|
|
//=================================================================
|
|
|
|
def call(Map config)
|
|
{
|
|
def publishSubFolder = config.publishSubFolder
|
|
def uploadEnv = config.uploadEnv
|
|
def uploadUrl = config.uploadUrl ?: 'https://gadmin.bywaystudios.com/api/open/apk/upload/'
|
|
def uploadToken = (config.uploadToken ?: '').trim()
|
|
def uploadTokenCredentialsId = (config.uploadTokenCredentialsId ?: 'apk-upload-token').trim()
|
|
def packageType = config.packageType ?: 'with_sdk'
|
|
|
|
if (!publishSubFolder) {
|
|
error('publishSubFolder is required for APK upload.')
|
|
}
|
|
|
|
if (!uploadEnv) {
|
|
error('uploadEnv is required for APK upload.')
|
|
}
|
|
|
|
if (!(packageType in ['with_sdk', 'without_sdk'])) {
|
|
error("packageType must be with_sdk or without_sdk, got: ${packageType}")
|
|
}
|
|
|
|
def apkListOutput = bat(
|
|
returnStdout: true,
|
|
script: "@echo off\r\ndir /b /a-d /o-d \"apk\\${publishSubFolder}\\*.apk\" 2>nul"
|
|
).trim()
|
|
|
|
if (!apkListOutput) {
|
|
error("No APK found under apk/${publishSubFolder}.")
|
|
}
|
|
|
|
def apkNames = apkListOutput.readLines().collect { it.trim() }.findAll { it }
|
|
def apkName = apkNames[0]
|
|
def apkPath = "${env.WORKSPACE}\\apk\\${publishSubFolder}\\${apkName}"
|
|
def version = config.uploadVersion ?: extractVersionFromFileName(apkName)
|
|
|
|
if (apkNames.size() > 1) {
|
|
echo "Multiple APKs found, uploading newest file: ${apkName}"
|
|
}
|
|
|
|
def scriptRelativePath = 'upload.py'
|
|
writeFile file: scriptRelativePath, text: libraryResource('uploadApk.py')
|
|
|
|
def pythonArgs = [
|
|
"python \"${scriptRelativePath}\"",
|
|
" --upload-url \"${uploadUrl}\"",
|
|
" --apk-path \"${apkPath}\"",
|
|
" --env \"${uploadEnv}\"",
|
|
" --package-type \"${packageType}\""
|
|
]
|
|
|
|
if (version) {
|
|
pythonArgs << " --version \"${version}\""
|
|
}
|
|
|
|
if (uploadToken) {
|
|
pythonArgs << " --token \"${uploadToken}\""
|
|
bat(
|
|
label: 'Upload APK to server',
|
|
script: "@echo off\r\n" + pythonArgs.join(" ^\r\n")
|
|
)
|
|
} else if (uploadTokenCredentialsId) {
|
|
withCredentials([string(credentialsId: uploadTokenCredentialsId, variable: 'APK_UPLOAD_TOKEN')]) {
|
|
pythonArgs << " --token \"%APK_UPLOAD_TOKEN%\""
|
|
bat(
|
|
label: 'Upload APK to server',
|
|
script: "@echo off\r\n" + pythonArgs.join(" ^\r\n")
|
|
)
|
|
}
|
|
} else {
|
|
error('Either uploadToken or uploadTokenCredentialsId must be provided for APK upload.')
|
|
}
|
|
|
|
return [
|
|
apkName: apkName,
|
|
version: version ?: '',
|
|
packageType: packageType,
|
|
uploadEnv: uploadEnv,
|
|
uploadUrl: uploadUrl
|
|
]
|
|
}
|
|
|
|
def extractVersionFromFileName(String fileName)
|
|
{
|
|
def matcher = fileName =~ /(?i)(?:^|[^0-9])v?(\d+(?:\.\d+){1,3})(?:[^0-9]|$)/
|
|
if (matcher.find()) {
|
|
return matcher.group(1)
|
|
}
|
|
|
|
return ''
|
|
}
|