Skip to content

Instantly share code, notes, and snippets.

@gianpaolof
Forked from mikkipastel/Jenkinsfile
Created April 13, 2023 18:50
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 gianpaolof/552ab1c04533e0e5be02cdac09d258bf to your computer and use it in GitHub Desktop.
Save gianpaolof/552ab1c04533e0e5be02cdac09d258bf to your computer and use it in GitHub Desktop.
android jenkins pipeline
pipeline {
agent any
stages {
stage('Checkout') {
options {
retry(3)
}
steps {
//Checkout new source code
checkout([$class: 'GitSCM',
branches: [[name: "branch_name"]],
submoduleCfg: [],
userRemoteConfigs: [[url: 'git@gitlab.com: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