Before we start, let’s brush up on our memory as to what GitLab is?
GitLab is an open-source code repository and collaborative software development platform for large DevOps and DevSecOps projects. GitLab is free for individuals.
GitLab is a DevOps software package that combines the ability to develop, secure, and operate software in a single application
Let’s begin with creating a pipeline for the android apk. We are using the flutter framework to create an android apk.
We are going to create a signed apk. There are some prerequisites required before we start.
- AWS Simple Storage Service (S3) for storing the keystore file.
- AWS Simple Storage Service (S3) for storing an apk
- We also need to save the store password, key alias, and key password in variables.
Click on the + option to create a new file in your desired branch. You will need to name the file “gitlab-ci.yml”
We will write all our scripts in the gitlab-ci.yml file to create an android apk.
Step 1: We will mention the image which we want to use. In this case, I’m using openjdk:8-jdk.
Note: Depending upon which image you would use the commands will change.
Step 2: Now we need to define our variables. You can check the image below to see what variables we are using.
variables: ANDROID_COMPILE_SDK: "30" ANDROID_BUILD_TOOLS: "30.0.2" ANDROID_SDK_TOOLS: "4333796" FLUTTER_VERSION: "https://storage.googleapis.com/flutter_infra_release/releases/stable/linux/flutter_linux_2.8.1-stable.tar.xz" STORE_PASSWORD: <STORE_PASSWORD> KEY_ALIAS: <KEY_ALIAS> KEY_PASSWORD: <KEY_PASSWORD> AWS_ACCESS_KEY_ID: <AWS_ACCESS_KEY_ID> AWS_SECRET_ACCESS_KEY: <AWS_SECRET_ACCESS_KEY> REGION: <AWS_DEFAULT_REGION>
Step 3: These are steps to enter variables in CI/CD settings variable part.
Step 4: After variables are mentioned, we move towards our before script. We will install Android SDK and set its path, mention Flutter SDK setup, and finally export the values needed in key.properties file.
Step 5: We have also created a key.properties file in android file. This is created to store the key alias, key password, store password, and store file.
Step 6: We will also need to mention steps to install awscli in before script. Installation of awscli is required for coping the keystore file from S3 and also to copy the apk in desired S3.
before_script: - apt-get --quiet update --yes - apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1 - wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip - unzip -d android-sdk-linux android-sdk.zip - echo y | android-sdk-linux/tools/bin/sdkmanager "platforms;android-28" >/dev/null - echo y | android-sdk-linux/tools/bin/sdkmanager "platform-tools" >/dev/null - echo y | android-sdk-linux/tools/bin/sdkmanager "build-tools;28.0.2" >/dev/null - export ANDROID_HOME=$PWD/android-sdk-linux - export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/ # temporarily disable checking for EPIPE error and use yes to accept all licenses - set +o pipefail - yes | android-sdk-linux/tools/bin/sdkmanager --licenses - set -o pipefail # flutter sdk setup - wget --output-document=flutter-sdk.tar.xz $FLUTTER_VERSION - tar -xf flutter-sdk.tar.xz - echo "export PATH=$PATH:$PWD/flutter/bin" - export PATH=$PATH:$PWD/flutter/bin - echo flutter.sdk=$PWD/flutter > android/local.properties - chmod +x ./android/build.gradle
Step 7: For this pipeline, we have only mentioned one stage and that is the build stage.
Now let’s move on to the build part.
Step 8: You would have to set a default region of AWS in the build script. The next part is to write the AWS S3 copy command that will copy the keystore file in our android folder. After this step, we will mention our keystore file path(storefile) in our key.properties with the help of echo.
# key store - export STORE_PASSWORD=$STORE_PASSWORD - export KEY_ALIAS=$KEY_ALIAS - export KEY_PASSWORD=$KEY_PASSWORD - echo "Preparing key.properties file" - echo STORE_PASSWORD=$STORE_PASSWORD >> android/key.properties - echo KEY_ALIAS=$KEY_ALIAS >> android/key.properties - echo KEY_PASSWORD=$KEY_PASSWORD >> android/key.properties - cat android/key.properties
Step 9: We will mention the flutter build command for our apk. After the build is complete we need to store it in AWS S3. So we are mentioning AWS S3 cp command to copy it to our desired S3.
Here is the final code of gitlab-ci.yml file
image: openjdk:8-jdk variables: ANDROID_COMPILE_SDK: "3" ANDROID_BUILD_TOOLS: "30.0.2" ANDROID_SDK_TOOLS: "4333796" FLUTTER_VERSION: "https://storage.googleapis.com/flutter_infra_release/releases/stable/linux/flutter_linux_2.8.1-stable.tar.xz" STORE_PASSWORD: <STORE_PASSWORD> KEY_ALIAS: <KEY_ALIAS> KEY_PASSWORD: <KEY_PASSWORD> AWS_ACCESS_KEY_ID: <AWS_ACCESS_KEY_ID> AWS_SECRET_ACCESS_KEY: <AWS_SECRET_ACCESS_KEY> REGION: <AWS_DEFAULT_REGION> before_script: - apt-get --quiet update --yes - apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1 - wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip - unzip -d android-sdk-linux android-sdk.zip - echo y | android-sdk-linux/tools/bin/sdkmanager "platforms;android-28" >/dev/null - echo y | android-sdk-linux/tools/bin/sdkmanager "platform-tools" >/dev/null - echo y | android-sdk-linux/tools/bin/sdkmanager "build-tools;28.0.2" >/dev/null - export ANDROID_HOME=$PWD/android-sdk-linux - export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/ # temporarily disable checking for EPIPE error and use yes to accept all licenses - set +o pipefail - yes | android-sdk-linux/tools/bin/sdkmanager --licenses - set -o pipefail # flutter sdk setup - wget --output-document=flutter-sdk.tar.xz $FLUTTER_VERSION - tar -xf flutter-sdk.tar.xz - echo "export PATH=$PATH:$PWD/flutter/bin" - export PATH=$PATH:$PWD/flutter/bin - echo flutter.sdk=$PWD/flutter > android/local.properties - chmod +x ./android/build.gradle # key store - export STORE_PASSWORD=$STORE_PASSWORD - export KEY_ALIAS=$KEY_ALIAS - export KEY_PASSWORD=$KEY_PASSWORD - echo "Preparing key.properties file" - echo STORE_PASSWORD=$STORE_PASSWORD >> android/key.properties - echo KEY_ALIAS=$KEY_ALIAS >> android/key.properties - echo KEY_PASSWORD=$KEY_PASSWORD >> android/key.properties - cat android/key.properties - curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" - unzip awscliv2.zip - ./aws/install stages: - build build:apk: stage: build script: - echo "aws configure set region "${AWS_DEFAULT_REGION}"" - aws configure set region "${AWS_DEFAULT_REGION}" - aws s3 cp s3://stoxbox-keystorefile/BPWEALTH.keystore /builds/texple-dev/bpwealth/stoxbox/bpwealth-flutter/android - echo storeFile=/builds/texple-dev/bpwealth/stoxbox/bpwealth-flutter/android/BPWEALTH.keystore >> android/key.properties - cat android/key.properties - flutter build apk - aws s3 cp build/app/outputs/flutter-apk/app-release.apk s3://stoxbox-apk-release artifacts: paths: - build/app/outputs/apk only: - develop
Just follow these steps and you can create your own GitLab Android Pipeline.
Hope this blog helps you.
That’s all Folks!!