Example Jenkinsfile

A Jenkinsfile with some common instructions.

Example

pipeline {

    agent {
        node {
        	// Use the label to determine which node this pipeline can run on
            label('android')
        }
    }

    options {
    	
    	// Bail as soon as a stage fails
        skipStagesAfterUnstable()

        // And don't keep everything forever
        buildDiscarder(logRotator(numToKeepStr:'10'))
    }

    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }

        stage('Prepare') {
            steps {
            	// Build another job
                // build job: '/Path/To/Job'
                // Run a shell script
                // sh './some/script.sh'
            }
        }

        stage('Build') {
            steps {

            	// Example for Android apps, APK-style
                sh './gradlew clean build assembleStageRelease assembleProdRelease --stacktrace --refresh-dependencies'
                
                // Example for Android apps, app bundle style
                sh './gradlew clean build bundleStageRelease bundleProdRelease --stacktrace --refresh-dependencies'
                
                // Note:
                // '--refresh-dependencies ensure each build is started completely clean,
                // but may cause problems if the CI has connection issues.
                // It also adds significantly to the build times.


                // Example for Android libraries
                sh './gradlew clean build install'
            }
        }

        stage('Check') {
            steps {
            	// Example for Android code
                sh './gradlew lint check'
            }
        }

        stage('Test') {
            steps {
            	
            	// Example for running instrumentation tests on an Android emulator
                lock('emulator') {
                    sh './gradlew connectedCheck'
                }
                junit '**/test-results/**/*.xml'
            }
        }

        stage('Archive') {
            steps {
            	// Example for Android apps:
                // APK only, no product flavor name:
                archiveArtifacts 'app/build/outputs/apk/release/*.apk'
                // APK and Bundle, with product flavor:
                archiveArtifacts artifacts: 'app/build/outputs/apk/**/release/*.apk, app/build/outputs/bundle/**/release/*.aab'
            }
        }

        stage('Publish') {
        	// Example for Android apps.
        	// If you have Crashlytics beta setup up, use its Gradle task to upload beta builds
            environment {
                GOOGLE_APPLICATION_CREDENTIALS="/var/lib/jenkins/path/to/google-credentials.json"
            }
            steps {
                sh "./gradlew crashlyticsUploadDistributionRelease"
            }
        }
    }

    post {

    	// If the Slack plugin (https://plugins.jenkins.io/slack/) is configured,
    	// this sends a message to the channel
        success {
            slackSend channel: '#jenkins', color: 'good', message: "This build is OK: ${env.BUILD_URL}"
        }
        unstable {
            slackSend channel: '#jenkins', color: 'bad', message: "This build is unstable: ${env.BUILD_URL}"
        }
        failure {
            slackSend channel: '#jenkins', color: 'bad', message: "This build is broken: ${env.BUILD_URL}"
        }

        // Publish the test results
        always {
            junit testResults: '**/build/reports/lint-results.xml'

            // https://github.com/jenkinsci/warnings-ng-plugin/blob/master/doc/Documentation.md#pipeline-configuration
            // Note: java() also picks up Kotlin reports
            recordIssues enabledForFailure: true, tools: [java()]

            // Notify Jira of status
            jiraSendBuildInfo site: 'pixplicity.atlassian.net'
        }
    }
}