In today’s fast-paced development environment, continuous integration and continuous deployment (CI/CD) are crucial for ensuring quick and reliable releases. In this guide, I’ll walk you through setting up a CI/CD pipeline for an Android project using GitLab, and Jenkins, and deploying it to Google Play Store’s internal testing track.
Prerequisites
- GitLab Repository: Host your Android project on GitLab.
- Jenkins: Have a Jenkins server with the necessary plugins installed.
- Google Play Console: Access with an API service account for publishing.
- Keystore: A Keystore file for signing the APK.
- Credentials: API credentials for Google Play, GitLab, and Jenkins.
Step 1: Set Up Your GitLab Repository
Ensure your GitLab repository is properly structured and contains your Android project files, such as:
/app
: Main Android app directory./build.gradle
: Build configuration file.
Step 2: Jenkins Configuration
- Install Jenkins and Required Plugins:
- Jenkins GitLab Plugin
- Gradle Plugin
- Google Play Android Publisher Plugin
- Credentials Plugin
- Create Jenkins Pipeline Job:
- Navigate to Jenkins Dashboard > New Item > Pipeline.
- Name your pipeline and select “Pipeline” as the type.
Step 3: Google Play API Setup
- Create a Service Account in Google Play Console:
- Go to Play Console > Settings > Developer Account > API Access > Create New Project.
- In the Google Cloud Console, create a new service account and grant it “Service Account User” and “API Access” roles.
- Download the JSON key file.
- Add JSON Key to Jenkins:
- Go to Jenkins Dashboard > Credentials > System > Global credentials (unrestricted) > Add Credentials.
- Select “Google Service Account from private key” and upload the JSON key file.
Step 4: Create Keystore Credentials in Jenkins
- Add Keystore File to Jenkins:
- Go to Jenkins Dashboard > Credentials > System > Global credentials (unrestricted) > Add Credentials.
- Select “Secret file” and upload your keystore file.
- Add Keystore Passwords:
- Add separate “Secret text” credentials for the keystore password, alias, and key password.
Step 5: Jenkins Pipeline Configuration
Configure your Jenkins pipeline with the following stages: checkout, build, sign APK, zipalign APK, and upload to Play Store. Below is a sample Jenkinsfile
:
pipeline {
agent any
environment {
GRADLE_HOME = tool name: 'gradle', type: 'hudson.plugins.gradle.GradleInstallation'
JAVA_HOME = tool name: 'jdk', type: 'hudson.model.JDK'
KEYSTORE_CREDENTIALS_ID = 'android-keystore'
PLAY_STORE_CREDENTIALS_ID = 'play-store-json'
}
stages {
stage('Checkout') {
steps {
git url: 'https://gitlab.com/your-repo.git', branch: 'main'
}
}
stage('Build') {
steps {
script {
def gradleHome = env.GRADLE_HOME
def javaHome = env.JAVA_HOME
sh "${gradleHome}/bin/gradle clean assembleRelease"
}
}
}
stage('Sign APK') {
steps {
withCredentials([file(credentialsId: KEYSTORE_CREDENTIALS_ID, variable: 'KEYSTORE'),
string(credentialsId: 'KEYSTORE_PASSWORD', variable: 'KEYSTORE_PASSWORD'),
string(credentialsId: 'KEY_ALIAS', variable: 'KEY_ALIAS'),
string(credentialsId: 'KEY_PASSWORD', variable: 'KEY_PASSWORD')]) {
sh """
jarsigner -verbose -keystore $KEYSTORE -storepass $KEYSTORE_PASSWORD -keypass $KEY_PASSWORD app/build/outputs/apk/release/app-release-unsigned.apk $KEY_ALIAS
"""
}
}
}
stage('ZipAlign APK') {
steps {
script {
def buildToolsVersion = '29.0.3' // Update with your build tools version
sh "${ANDROID_HOME}/build-tools/${buildToolsVersion}/zipalign -v 4 app/build/outputs/apk/release/app-release-unsigned.apk app/build/outputs/apk/release/app-release.apk"
}
}
}
stage('Upload to Play Store') {
steps {
withCredentials([file(credentialsId: PLAY_STORE_CREDENTIALS_ID, variable: 'PLAY_STORE_SERVICE_ACCOUNT_JSON')]) {
googlePlayPublisher apkFilesPattern: 'app/build/outputs/apk/release/app-release.apk',
trackName: 'internal',
serviceAccountCredentialsId: PLAY_STORE_CREDENTIALS_ID
}
}
}
}
}
Explanation
- Checkout: Clones the GitLab repository.
- Build: Gradle is used to clean and assemble the release APK.
- Sign APK: Sign the APK using the provided keystore credentials.
- ZipAlign APK: Optimizes the APK for performance.
- Upload to Play Store: Upload the signed APK to the Google Play Console using the
googlePlayPublisher
plugin.
Step 6: GitLab Configuration
Set up a webhook in your GitLab repository to trigger the Jenkins pipeline:
- Go to GitLab Project > Settings > Webhooks.
- Add a new webhook with the URL of your Jenkins job (e.g.,
http://your-jenkins-server/job/your-pipeline/build?token=your-token
). - Set the trigger to “Push events” or any other relevant events.
Final Steps
- Test the Pipeline: Push changes to your GitLab repository and verify that Jenkins triggers the pipeline and successfully builds and deploys your app.
- Monitor and Troubleshoot: Check Jenkins logs for any issues and adjust configurations as necessary.
Following these steps, you should have a fully automated CI/CD pipeline that builds, tests, and deploys your Android application to the Google Play Store’s internal testing track.
Leave a Reply