Last active
August 22, 2023 17:31
-
-
Save mikkipastel/09255dfe4819f39e079586db5673d247 to your computer and use it in GitHub Desktop.
android jenkins pipeline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pipeline { | |
agent any | |
stages { | |
stage('Checkout') { | |
options { | |
retry(3) | |
} | |
steps { | |
//Checkout new source code | |
checkout([$class: 'GitSCM', | |
branches: [[name: "branch_name"]], | |
submoduleCfg: [], | |
userRemoteConfigs: [[url: '[email protected]:username/project-android.git']]]) | |
} | |
} | |
stage('Clean') { | |
steps { | |
sh './gradlew clean' | |
} | |
} | |
stage('Build') { | |
steps { | |
sh './gradlew assembleDevDebug' | |
archiveArtifacts '**/*.apk' | |
} | |
} | |
stage('Ktlint') { | |
steps { | |
sh './gradlew ktlint' | |
} | |
} | |
stage('Test') { | |
steps { | |
// Compile and run the unit tests for the app and its dependencies | |
sh './gradlew testDevDebugUnitTestCoverage' | |
// Analyse the test results and update the build result as appropriate | |
junit( | |
allowEmptyResults: false, | |
testResults: '**/build/test-results/**/*.xml' | |
) | |
} | |
} | |
stage('SonarQube') { | |
steps { | |
withSonarQubeEnv('Yavin') { | |
sh "./gradlew sonarqube" | |
} | |
} | |
} | |
} | |
post { | |
always { | |
script { | |
def JENKINS_VERSION = Jenkins.instance.getVersion().toString() | |
def DISCORD_NOTIFIER_VERSION = Jenkins.instance.pluginManager.getPlugin('discord-notifier').getVersion() | |
def changes = getChanges() | |
def artifacts = getArtifacts() | |
discordSend description: "**Build:** [${BUILD_NUMBER}](${BUILD_URL})\n**Status:** [${currentBuild.currentResult.toLowerCase()}](${BUILD_URL})\n\n**Changes:**\n"+changes+"\n\n**Artifacts:**\n"+artifacts+"\n", | |
link: env.BUILD_URL, | |
result: currentBuild.currentResult, | |
title: JOB_NAME + " #" + env.BUILD_NUMBER, | |
footer: "Jenkins v"+ JENKINS_VERSION +", Discord Notifier v"+ DISCORD_NOTIFIER_VERSION, | |
successful: currentBuild.resultIsBetterOrEqualTo('SUCCESS'), | |
webhookURL: "https://discord.com/api/webhooks/{webhook_id}" | |
} | |
} | |
} | |
} | |
@NonCPS | |
def getArtifacts() { | |
def msg = "" | |
def artifactUrl = env.BUILD_URL + "artifact/" | |
currentBuild.rawBuild.getArtifacts().each { | |
filename = it.getFileName() | |
msg += "- ${artifactUrl}${it.getFileName()}\n" | |
} | |
if (msg.isEmpty()) { | |
msg = "n/a" | |
} | |
return msg.trim() | |
} | |
@NonCPS | |
def getChanges() { | |
def changes = "" | |
def changeLogSets = currentBuild.changeSets | |
for (int i = 0; i < changeLogSets.size(); i++) { | |
def entries = changeLogSets[i].items | |
for (int j = 0; j < entries.length; j++) { | |
def entry = entries[j] | |
def commitId = entry.commitId.substring(0,8) | |
changes += "- `${commitId}` *${entry.msg} - ${entry.author}*\n" | |
} | |
} | |
if (changes.isEmpty()) { | |
changes = "No changes." | |
} | |
return changes.trim() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment