Android CI/CD with GitLab — Build, Test, Sign, Upload to Dropbox and send an Email

Click download"}Send a POST request, with this JSON, to the URL that was given by Zapier.

Let’s say your URL was https://hooks.

zapier.

com/hooks/catch/12345/abcd/, the cURL will look like this:Type your real email address — we will use it for tests later.

Now click Ok, I did this and Zapier will show you the incoming hook.

Select it and click “Continue”.

Caught HookWe’ve got the hook, now we need the action step.

Click the message that suggests to add the action step, select Gmail and choose to Send Email.

Zapier will ask you to connect your account — do it and then check that it’s working with the Test button.

Zapier Gmail actionClick the Continue button and fill the fields To, From Name, Subject, Body.

Select the data that was received from our POST inside To, Subject, Body fields:Each Zappier field matches Hook fieldWrite whatever you want in the From Name field.

My setup ended up looking like this:Zapier fields can match Hook fields and hardcoded valuesHit the Continue button and Send To Test Gmail.

If you get the green message — everything is ok (you should also receive an email to provided address).

Hit Finish, name your Zap and turn it on.

Now let’s make the real test — use your Zap URL to run the cURL request (or postman) again.

Try different values in subject and body fields.

Make sure you received an email from your connected Gmail account and we are ready for the next step.

6.

Add Dropbox and Zapier variablesNow that we have our Dropbox token and Zapier webhook, we will add them to CI/CD variables.

DROPBOX_TOKEN EMAIL_TARGET ZAPIER_EMAIL_HOOK7.

DeployThe last job will be a deploy script that relies on previously saved artifacts, values and template files.

deployRelease: image: python:3.

5.

2 stage: deploy script: – pip install requests – git clone https://github.

com/mega-arbuz/apk-release-deploy.

git – python apk-release-deploy/deploy.

py –release.

dir=app/build/outputs/apk/release –app.

name=BestAppEver –dropbox.

token=$DROPBOX_TOKEN –dropbox.

folder=release –changelog.

file=CHANGELOG –template.

file=TEMPLATE –zapier.

hook=$ZAPIER_EMAIL_HOOK –email.

to=$EMAIL_TARGET cache: {}This job uses python:3.

5.

2 image because we just want to run a simple python script that will do all the work — no need for the large Android image.

The cache, from previous jobs, is disabled because we don’t run Gradle builds here.

The first step is to install requests module (you can change the image to a python image that already has requests installed), then we clone the deploy script that will do the APK upload and email sending.

We will dive into the script in the next stage, right now let’s prepare the required arguments.

–release.

dir path to app release directory that was saved in artifacts.

–app.

name app name that will be used in emails.

–dropbox.

token use $DROPBOX_TOKEN CI/CD variable here.

–dropbox.

folder dropbox folder name for app builds.

–zapier.

hook use $ZAPIER_HOOK CI/CD variable.

–email.

to list of recipients, use $EMAIL_TARGET CI/CD variable.

–changelog.

file path to CHANGELOG file with a special format — see next chapter about formatting this file.

–template.

file path to TEMPLATE file with email template — see next chapter about formatting this file.

The paths in the job are matching my project structure from the GitLab sample project.

Visit the project for a better understanding of how to customize this.

The .

gitlab-ci.

yml file is completed and the result should look like this:8.

CHANGELOG and TEMPLATEThe deploy script uses CHANGELOG and TEMPLATE files for email composing.

These files are also documented in the deploy script GitHub.

CHANGELOGThis file is used for storing app changes.

The deploy script will extract the latest version change and use it in the email.

Each version change is separated by ## and all the lines starting with #are ignored, so you can use the # char for version number marks, which will make the file readable.

# Version 1.

05Removed Google Maps FragmentAdded: – Settings crash – ANR when loading### Version 1.

04Added Google Maps FragmentFixed: – Settings crash – ANR when loading##The script will ignore the # Version 1.

05 line and will extract the content before the first ##.

This content will be used inside the email.

There is another way to make auto changelog — use GitLab API and extract the relevant tickets from commit messages.

This is too much for one post, I will cover this in a different article.

TEMPLATEThe template file is used for composing the email.

We want to send some information about the app, but our email format preferences may be different.

Use the template file with the following variables to compose the email:{app_name} will be replaced with the real app name.

{app_version} will be replaced with app version from this build.

{app_download_url} will be replaced with the Dropbox download URL.

{change_log} will be replaced with the changes extracted from the CHANGELOG file.

Email subject and body are separated with #subject and #body lines.

My final TEMPLATE file looks like this:#subjectNew {app_name} release, version {app_version}#bodyNew version is available for download:{app_download_url}Changes:{change_log}This email was sent automatically, please do not reply.

EmailThis is how the email, with all previous configuration, will arrive.

After the pipeline is finished, we will get this email:It’s also possible to compose the email with HTML template9.

Deploy ScriptNow when our CI/CD works, let’s see what do we have in the deploy script.

Full ScriptThe full script is available in this GitHub project.

App Version and FileBefore we send anything, we want to know the version of our build and the path to the APK file.

This information is stored in the output.

json file, generated in the release directory.

Luckily, we received the path to release directory from previous steps.

Uploaded File NameWe want our file name to be safe (without spaces and dots) and have some useful info, like app name and version.

For an app called “Best App Ever” and build version 1.

05, we will get the name: bestappever_1_05.

apk.

Dropbox UploadDropbox supports add and overwrite upload modes — we will use the overwrite mode, just in case.

There is also an issue with getting shareable URLs — if the file was shared earlier, the request will return an error, which means that we have to make another call to check if the file was shared.

This is a bit dangerous because our overwritten file will have the same URL as the old one.

In order to avoid all these issues, the script will send a delete request at the beginning, so that we start the process knowing that there are no files with the same name.

We configure our Dropbox request arguments: mode=overwrite, autorename=True and requested_visibility=public.

The upload method constructs the file path, using dropbox.

folder argument and app name, sends a delete request, uploads the file and sends a request for shareable URL.

The returned URL is taking us to Dropbox download page — we don’t want this, so we change the last digit from ‘0’ to ‘1’, which will take us to direct download.

ChangesWe get the changes from the CHANGELOG file.

The process was described in previous steps so here is the code:Compose EmailNow we have our app name, app version, download URL and the latest changes.

All this information goes to our TEMPLATE file.

The result will be — subject and body for our email.

Send EmailThis one is simple — we use our data structure from the Zapier webhook setup.

10.

ResourcesGitLab project with everything described in this article.

Oleg Shnaydman / Android CI-CDProject with Android CI/CD pipelines that will build, test, release, sign, upload to Dropbox and send an email with a…gitlab.

comGitHub project with the deploy script.

mega-arbuz/apk-release-deployDeploy apk file with changelog for Android projects with CI/CD.

– mega-arbuz/apk-release-deploygithub.

com.. More details

Leave a Reply