Add Gitlab variables and .ππππππ-ππ.π’ππ file
Part III β Automate App release to Google Play | Story 2 - Gitlab CI/CD variables and .ππππππ-ππ.π’ππ file
In the previous post, we created a Google Play Service account that would be needed for GPP plugin to access the Google Play API to automate the app bundling and release.
In this post, we would set up Gitlab environments and the yml file (.gitlab-ci.yml
) that would contain the script to automate the app bundling and release process.
Gitlab CI / CD Environment variables
Earlier in Part II of the series, when creating our React-native Android appβs bundle .aab
file manually, we created an upload key/keystore file for our app as required by Google Play store.
That time, we stored this upload key/keystore file in the in the projectβs android/app
folder.
Now, as we automate the process of creating and publishing the appβs bundle file from Gitlab CI-CD pipeline, instead of saving the Upload keystore file in project folder, we can leverage Gitlabβs CI/CD variables to store the keystore file and its credentials/alias.
This way our keystore file remains secured as its not stored in the projectβs file repository. The Gitlabβs CI/CD environment variables can be set to be accessible by admins only.
Gitlabβs CI/CD process can read the value of its CI/CD variables at runtime when running the automation script in gitlab-ci.yml
file, where we code our build and publish automation script. In this automation script, we will write code to create the Upload keystore file on the fly during build runtime reading its value from the Gitlab CI/CD variable.
Along with the Upload Keystore file, we would also save the passwords for Upload keystore and key, and the Upload key Alias in Gitlab variables and get those removed from android/gradle.properties
file that we did when manually creating the bundle file.
So, letβs get to setting up Gitlab CI/CD variables. First thing we need is the base64 string value of the contents of the Upload keystore file to save it as the variableβs value.
Create Base64 string of the Upload keystore file
On a terminal window, cd to the folder where you have the upload keystore file created/saved (should be in projectβs android/app folder if you have followed the example of this series) and run below command.
$ cd android/app
$ sudo base64 -i rnTSgitlabCICDExample-upload.keystore -o rnTSgitlabCICDExample-upload-b64.txt
π -i is the input file i.e. our upload keystore file
-o specifies the output file to which the command would write the base64 string of the input file contents.
Once the command completes, it would generate the specified output file with the base64 string of our upload keystore file.
Now that we have the base64 string of our Upload keystore file available in the text file, the Keystore file is no longer needed to be stored in project folder, move it from the project to some secured place (I store it in my secured cloud folder) just in case if ever needed later and copy-paste the base64 string value to a Gitlab CI/CD variable as explained below π
Adding variables to Gitlab CI/CD settings
- Login to your Gitlab project with admin account.
- Go to Settings β CI/CD from the left side menu and click Expand button next to Variables section of CI/CD settings page π
- Next, click Add Variable
- On the Add variable pop-up form:
- Enter a name for the variable
- Select File as the variable type.
- Now copy the base64 string contents of our Upload keystore from the text file we created above, and paste it in the value field of the Gitlab variable being added
- click the Add variable buttonπ
Once the variable is added, you can delete the base64 output text file as well from the project (rnTSgitlabCICDExample-upload-b64.txt
in above example.)
We will also add following variables for:
- PLAY_STORE_UPLOAD_KEYSTORE_PASSWORD
β type=variable, value = Upload keystore file password
- PLAY_STORE_UPLOAD_KEYSTORE_ALIAS
β type=variable, value = Upload key alias
- PLAY_STORE_UPLOAD_KEY_PASSWORD
β type=variable, value = Upload key password (remember the password we specified when creating the Upload key)
While at it, letβs also create below two more variables -
GCP_PLAYSTORE_SERIVCE_ACCOUNT_KEY_JSON
:
Earlier when we created our service account and linked it to our Play store console, we created its JSON key and downloaded it.
Copy the contents from the service accountβs key JSON file and set it as a value of this variable π
GCP_PLAYSTORE_SERIVCE_ACCOUNT_EMAIL
: set the GCP Service accountβs email address as value of this variable π
At this point, the Gitlab variables list should look something like below π
.ππππππ-ππ.π’ππ file
In order to configure CI/CD with gitlab, we need a .gitlab-ci.yml
file in our projects root folder. Add the file under projectβs root folder as below:
Letβs go through the above code to understand whatβs happening here:
line# 1
: image
β here we have specified which docker image gitlab should use to spin the VM for creating the android build/bundle.
line# 3 - 6
: variables
- here we specified few variables that we will use down in the commands we would run to download Android SDK ( in line # 36β40
).
The versions specified Android Compile SDK, Build Tools and SDK Tools are the stable versions as the writing of this post. Update those as appropriate.
line# 8β50
: before_script
commands - commands we want to run before running the build release script. These are as below:
line #10β16
: In these commands, we are creating a new file globalSettings.json
under android/app
folder (at line # 10) and then add some keys to this json
file, setting their values read from the Gitlab CI/CD variables that we configured above.
line #20-21
: Here we read our service account keyβs JSON and save it in a file serviceAccountKey.json
under the folder android/app
.
(When Gitlab would run the pipeline, it would run the build process in a VM and when these above commands are run, the files would be created on the fly under the folder android/app
, so that, when building the app bundle, appβs build.gradle
(android/app/build.gradle
) file can read these files directly within the folder without having to specify any relative paths for them.)
line #55
: we declared a stage build_release_andorid
, which is defined at line # 58β67
.
In the build_release_andorid
, the script runs below commands line# 62β65
:
- base64 -d $PLAY_STORE_UPLOAD_KEYSTORE_FILE_BASE64 > android/app/upload.keystore
- cd android
- ./gradlew bundleRelease
- ./gradlew publishApkRelease
line #62
: here we create the upload keystore file upload.keystore
from its contentβs base64 string that we created earlier and stored in Gitlabβs CI/CD variable PLAY_STORE_UPLOAD_KEYSTORE_FILE_BASE64
. GPP would use this upload.keystore
to sign the AAB app bundle file as required by the Android Play store to upload a AAB/APK.
line #63β64
: Then we cd
to android
folder and the run the command ./gradlew bundleRelease
to build the bundle the release. This will create our release bundle AAB file.
line #65
: Finally we run the command ./gradlew publishApkRelease
, with this command, GPP will publish the AAB file created above to Google play store as per the configuration that we will setup in the app/build.gradle
fileβs play
settings. Thatβs in the next post.
Alright, so we got the Gitlab CI/CD variables added and ready with the .ππππππ-ππ.π’ππ script. Finally we need to make few changes to πππππππ/πππππ.ππππππ and πππππππ/πππ/πππππ.ππππππ files to complete the pipeline automation setup. Thatβs what we would do in the next post.
βPrev:GCP Service Account
βπ β Next:Play settings in build.gradle
β