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.

πŸ‘† -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 πŸ‘‡
(click on image to expand)
  • 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πŸ‘‡
(click on image to zoom)

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:

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β†’

--

--