#!groovy
/*
* This work is protected under copyright law in the Kingdom of
* The Netherlands. The rules of the Berne Convention for the
* Protection of Literary and Artistic Works apply.
* Digital Me B.V. is the copyright owner.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Load Jenkins shared libraries common to all projects
def libCmn = [
remote: 'https://code.in.digital-me.nl/git/DEVops/JenkinsLibLazy.git',
branch: 'master',
credentialsId: null,
]
library(
identifier: "libCmn@${libCmn.branch}",
retriever: modernSCM([
$class: 'GitSCMSource',
remote: libCmn.remote,
credentialsId: libCmn.credentialsId
])
)
// Load Jenkins shared libraries to customize this project
def libCst = [
remote: 'ssh://git@code.in.digital-me.nl:2222/DEVops/JenkinsLibCustom.git',
branch: 'master',
credentialsId: 'bot-ci-dgm-rsa',
]
library(
identifier: "libCst@${libCst.branch}",
retriever: modernSCM([
$class: 'GitSCMSource',
remote: libCst.remote,
credentialsId: libCst.credentialsId
])
)
def getVersion(part, returnGroup = false) {
def gradleFile = readFile(encoding: 'UTF-8', file: 'app/build.gradle')
def m = gradleFile =~ /([^\n]*(?:${part})(?:[^:]*:?\s+|\s*=\s*)"?)([.0-9]+)("?\s*(?:(?:\/\/|#)[^\n]*)?[\n])/
if (m) returnGroup ? m[0] : m[0][2]
else return null
}
def withGitPassword(id, body = { sh 'git version' }) {
withCredentials([usernamePassword([credentialsId: id, passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USER'])]) {
// TODO: Move git_askpass.sh as a library resource
withEnv(["GIT_ASKPASS=/opt/jenkins-scripts/git_askpass.sh"]) {
body()
}
}
}
def gitMerge(from, into, msg) {
sh("""
git checkout ${from}
git checkout ${into}
git merge ${from} -m '${msg}'
""")
}
def gitPush(gitRemote, gitCommit, gitOpts = "") {
gitOpts = ( env.DRYRUN == 'true' ) ? "--dry-run ${gitOpts}" : gitOpts
sh("git push ${gitOpts} ${gitRemote} ${gitCommit}")
}
def gitLog(bottom = null, top = 'HEAD') {
sh("git fetch --tags --quiet")
def from = bottom ?: 'tags/' + sh(script: "git tag -l | tail -1", returnStdout: true).trim()
sh(script: "git --no-pager log ${from}...${top} --pretty=format:'- %s' --reverse", returnStdout: true)
}
def prepareChangelogs(versionCode) {
lDir = 'fastlane/metadata/android'
// TODO: Test if latest changelogs have changed since last tag and use gitLog if not
sh("""
for LOCALE in \$(ls -1d ${lDir}/??-??); do
cp -vf \${LOCALE}/changelogs/latest.txt \${LOCALE}/changelogs/${versionCode}.txt
done
""")
}
def gitUpdateChangelogs(versionCode, gitBranch = null, gitRemote = 'origin') {
prepareChangelogs(versionCode)
def lDir = 'fastlane/metadata/android'
sh("""
git status --porcelain ${lDir} | grep -q 'changelogs/${versionCode}\\.txt\$' \
|| { echo 'Nothing to update'; exit 0; }
""")
if (gitBranch) sh("""
git stash save --quiet --include-untracked changelogs
git checkout --quiet ${gitRemote}/${gitBranch}
git stash pop
""")
sh("""
git add ${lDir}/*/changelogs/${versionCode}.txt
git commit --quiet -s -m 'Provide changelogs for version ${versionCode}' ${lDir}
""")
if (gitBranch) gitPush(gitRemote, gitBranch)
}
def gitTag(version, gitRemote = 'origin' ) {
sh("git tag -a '${version}' -m 'Create new tag for version ${version}'")
gitPush(gitRemote, version)
}
def setVersion(versionName, versionCode) {
def gradleFilePath = 'app/build.gradle'
def gradleFile = readFile(encoding: 'UTF-8', file: gradleFilePath)
gName = getVersion('versionName', true)
gCode = getVersion('versionCode', true)
gradleFile = gradleFile.replace(gName[0], gName[1] + versionName + gName[3])
gradleFile = gradleFile.replace(gCode[0], gCode[1] + versionCode + gCode[3])
writeFile(encoding: 'UTF-8', file: gradleFilePath, text: gradleFile)
}
def gitUpdateVersion(versionName, versionCode, gitBranch = null, gitRemote = 'origin') {
def gradleFilePath = 'app/build.gradle'
def dryRun = ( env.DRYRUN == 'true' ) ? '--dry-run' : ''
setVersion(versionName, versionCode)
sh("""
git status --porcelain ${gradleFilePath} | grep -q '${gradleFilePath}\$' \
|| { echo 'Nothing to update'; exit 0; }
""")
if (gitBranch) sh("""
git stash save --quiet versions
git checkout --quiet ${gitRemote}/${gitBranch}
git stash pop
""")
sh("git commit --quiet -s -m 'Update version from ${gName[2]}-${gCode[2]} to ${versionName}-${versionCode}' app/build.gradle")
if (gitBranch) gitPush(gitRemote, gitBranch)
}
// Define the remotes and the branches used to release from and to
def releaseFrom = [ remote: 'origin', branch: 'devel' ]
def releaseTo = [ remote: 'origin', branch: 'master' ]
// Initialize lazyConfig for this pipeline
lazyConfig(
name: 'DummyAnd',
nopoll: '.+_.+',
)
// Define lazyStages
lazyStage {
name = 'validate'
tasks = [
run: {
fastlane('android', 'test')
},
on: 'android',
]
}
lazyStage {
name = 'package'
tasks = [
run: {
fastlane('android', 'build')
},
post: {
archiveArtifacts(artifacts: 'app/build/outputs/apk/**', allowEmptyArchive: false)
},
on: 'android',
]
}
// Release stage only only if criteria are met
if (env.BRANCH_NAME == releaseFrom['branch'] && env.env ==~ /RELEASE=true/) {
lazyStage {
name = 'release'
// Ask version if release flag and set and we are in the branch to fork release from
input = [
message: 'Version name ?',
parameters: [string(defaultValue: '', description: 'Version name?', name: 'VERSION')]
]
tasks = [
run: {
def currentVersion = [ name: getVersion('versionName') as String, code: getVersion('versionCode') as Integer ]
echo("currentVersion = ${currentVersion.toString()}")
withGitPassword('bot-ci-dgm', {
// Fork a release branch as requested
echo("Git logs since last tag:\n" + gitLog())
sh("git checkout -b release-${env.LAZY_INPUT}")
gitUpdateVersion(env.LAZY_INPUT, currentVersion.code + 1)
def nextVersion = [ name: getVersion('versionName') as String, code: getVersion('versionCode') as Integer ]
echo("nextVersion = ${nextVersion.toString()}" )
gitUpdateChangelogs(nextVersion.code)
gitPush(releaseFrom['remote'], "release-${env.LAZY_INPUT}")
})
},
on: 'android',
]
}
}
// From systemtest to production, only for release branches
if (env.BRANCH_NAME ==~ /^release-.*/) {
lazyStage {
name = 'systemtest'
tasks = [
pre: {
unarchive(mapping:['app/build/outputs/apk/' : '.'])
sh("ls -lA app/build/outputs/apk")
},
run: {
def currentVersion = [ name: getVersion('versionName') as String, code: getVersion('versionCode') as Integer ]
echo("currentVersion = ${currentVersion.toString()}")
if ( env.DRYRUN != 'true' ) fastlane('android', 'alpha')
withGitPassword('bot-ci-dgm', {
gitMerge(
"${env.BRANCH_NAME}",
"${releaseFrom['branch']}",
"Merge changes from ${currentVersion['name']}-${currentVersion['code']} back into ${releaseFrom['branch']}"
)
gitPush(releaseFrom['remote'], releaseFrom['branch'])
})
},
on: 'android',
]
}
lazyStage {
name = 'acceptance'
input = 'Promote alpha to beta?'
tasks = [
pre: {
unarchive(mapping:['app/build/outputs/apk/' : '.'])
sh("ls -lA app/build/outputs/apk")
},
run: {
if ( env.DRYRUN != 'true' ) fastlane('android', 'beta')
/* },
on: 'android',
]
}
lazyStage {
name = 'production'
input = 'Promote beta to production?'
tasks = [
pre: {
unarchive(mapping:['app/build/outputs/apk/' : '.'])
sh("ls -lA app/build/outputs/apk")
},
run: {
if ( env.DRYRUN != 'true' ) fastlane('android', 'production')
*/ def currentVersion = [ name: getVersion('versionName') as String, code: getVersion('versionCode') as Integer ]
echo("currentVersion = ${currentVersion.toString()}")
withGitPassword('bot-ci-dgm', {
gitMerge(
"${env.BRANCH_NAME}",
"${releaseTo['branch']}",
"Merge changes from ${currentVersion['name']}-${currentVersion['code']} into ${releaseTo['branch']}"
)
gitPush(releaseTo['remote'], releaseTo['branch'])
gitTag("${currentVersion.name}", releaseTo['remote'])
})
},
on: 'android',
]
}
}