在Linux打包Android应用APK,并配置Jenkins构建

配置Jenkins打包

知道了整个打包的流程,就可以配置Jenkins任务了,创建一个简单的Pipeline任务,输入内容:

pipeline {

  agent any

    environment {
        //设置环境变量
        Pub_env = "test"
        JAVA_HOME = "/usr/local/jdk-11.0.17"
        GRADLE_HOME = "/usr/local/gradle-7.2"
        PATH = "${JAVA_HOME}/bin:${GRADLE_HOME}/bin:$PATH"
        ANDROID_HOME = "/usr/local/android_sdk"

        // git仓库信息
        git_repo_url = '[email protected]:android/jekins-test.git'
    }

    stages {
        //利用shell命令构建特定branch
        stage('Listing Branches') {
            steps {
                script {
                    // 获取branch清单,git branch -r
                    branch_liste = sh(returnStdout: true, script: "git ls-remote -h ${git_repo_url} | awk '{print \$2}' ORS='\\n' | cut -d '/' -f 3 | sort -r").trim()
                    echo "${branch_liste}"
                }
            }
        }

        stage('Input Build Branch and Merge Branch') {
            steps {
                timeout(time: 2, unit: 'MINUTES') {
                    script {
                        BRANCH_NAME = input message: 'Please choose the branch to build ', ok: '确认',
                            parameters: [
                                choice(name: 'BRANCH_NAME', choices: "${branch_liste}", description: 'Branch to build?')
                            ]
                        echo "${BRANCH_NAME}"
                    }
                }
            }
        }

        stage('Checkout') {
            steps {
                git branch: "${BRANCH_NAME}", url: "${git_repo_url}"
            }
        }

        stage("Build Test") {
            parallel {
                stage('Build') {
                    steps {
                        sh "gradle app:clean"
                        sh "gradle app:assembleRelease"
                    }
                }

                stage('Test') {
                    steps {
                        echo 'Test Unused'
                    }
                }
            }
        }
    }
  
    post {
        success {
            echo 'I succeeeded :)'
        }
        failure {
            echo 'I failed :('
        }
    }
}

创建后执行:

Tips:这里仅打配置了打成APK包的操作;打包后再需执行的其它操作(如上传发布),可根据具体需要再做配置。获取Gitlab分支这里调用了shell执行git命令,也可以使用Jenkins插件Git Parameter来达到同样效果。

发表评论

error: Content is protected !!