Jenkins ile Android Uygulamaların Build edilmesi – Pipeline

Android uygulamanızı aşağıdaki pipeline kodları ile Debug ve Release olacak şekilde build edebilirsiniz. Jenkins sunucusunda Android SDK ve Java kurulumunun yapılması gerektiğini hatırlatmak isterim. Aşağıdaki URL’ den yardım alabilirsiniz.

https://medium.com/appgambit/setup-android-sdk-on-centos-9a420b928e35

Ardından “ANDROID_HOME” değişkenini Android SDK kurulumunu yaptığınız dizinle değiştirmelisiniz.

Debug

//Gitlab kaynak kod adresi
String gitlabUrl = "http://xxx.com"

//Gitlab user
String gitlabUser = "xxx"

pipeline {
    agent any
    
    stages {
        stage('Preparation') { 
            steps {
                checkout([
                    $class: 'GitSCM', 
                    branches: [[name: 'master']], 
                    doGenerateSubmoduleConfigurations: false, 
                    extensions: [], 
                    submoduleCfg: [], 
                    userRemoteConfigs: [[credentialsId: "$gitlabUser", url: "${gitlabUrl}"]]])
            }
        }
      
        stage('Dependencies') {
            steps {
            sh 'export JAVA_HOME=/opt/jdk'
            sh 'export JRE_HOME=/opt/jdk/jre'
            sh 'export PATH=$PATH:/opt/jdk/bin:/opt/jdk/jre/bin'
            sh 'export ANDROID_HOME=/var/lib/jenkins/android-sdk'
            }
        }
        
        stage('Clean') {
            steps {
            // Clean
            sh './gradlew clean'
            }
        }
        
        stage('Compile') {
            steps {
            // Compile the app and its dependencies
            sh './gradlew compileDebugSources'
            }
        }
        stage('Unit test') {
            steps {
            // Compile and run the unit tests for the app and its dependencies
            sh './gradlew testDebugUnitTest'

            // Analyse the test results and update the build result as appropriate
            //junit '**/TEST-*.xml'
            }
        }
        stage('Build APK') {
            steps {
            // Finish building and packaging the APK
            sh './gradlew assembleDebug'

            // Archive the APKs so that they can be downloaded from Jenkins
            archiveArtifacts '**/*.apk'
            }
        }
    }
    post { 
        always { 
            cleanWs()
        }
    }
}

Release

//Gitlab kaynak kod adresi
String gitlabUrl = "xxx.com"

//Gitlab user
String gitlabUser = "xxx"

pipeline {
    agent any
    environment {
        //appName = 'jenkins-blog'

        KEY_PASSWORD = credentials('keyPassword')
        KEY_ALIAS = credentials('keyAlias')
        KEYSTORE = credentials('keystore')
        STORE_PASSWORD = credentials('storePassword')
    }
    stages {
        stage('Preparation') { 
            steps {
                checkout([
                    $class: 'GitSCM', 
                    branches: [[name: 'master']], 
                    doGenerateSubmoduleConfigurations: false, 
                    extensions: [], 
                    submoduleCfg: [], 
                    userRemoteConfigs: [[credentialsId: "$gitlabUser", url: "${gitlabUrl}"]]])
            }
        }
      
        stage('Dependencies') {
            steps {
            sh 'export JAVA_HOME=/opt/jdk'
            sh 'export JRE_HOME=/opt/jdk/jre'
            sh 'export PATH=$PATH:/opt/jdk/bin:/opt/jdk/jre/bin'
            sh 'export ANDROID_HOME=/var/lib/jenkins/android-sdk'
            }
        }
        
        stage('Clean') {
            steps {
            // Clean
            sh './gradlew clean'
            }
        }
        
        stage('Compile') {
            steps {
            // Compile the app and its dependencies
            sh './gradlew compileReleaseSources'
            }
        }
        stage('Unit test') {
            steps {
            // Compile and run the unit tests for the app and its dependencies
            sh './gradlew testReleaseUnitTest'

            // Analyse the test results and update the build result as appropriate
            //junit '**/TEST-*.xml'
            }
        }
        stage('Build APK') {
            steps {
            // Finish building and packaging the APK
            sh './gradlew assembleRelease -Pandroid.injected.signing.store.file=${KEYSTORE} -Pandroid.injected.signing.store.password=${STORE_PASSWORD} -Pandroid.injected.signing.key.alias=${KEY_ALIAS} -Pandroid.injected.signing.key.password=${KEY_PASSWORD}'

            // Archive the APKs so that they can be downloaded from Jenkins
            archiveArtifacts '**/*.apk'
            }
        }
    }
    post { 
        always { 
           cleanWs()
        }
    }
}
Back To Top