Get Started
Building Docker images with GitLab CI/CD
First, we need to create a package install configuration file Dockerfile.
Next, we need to create a Docker Image through written the Dockerfile.
docker build -t registry.example.com/group/project:tag .
e.g. docker build -t registry.silkrode.com.tw/team_mobile/jys:1.0.1 .
Finally, we push the Docker image to Gitlab Container Registry.
docker push registry.example.com/group/project:tag
e.g. docker push registry.silkrode.com.tw/team_mobile/jys:1.0.1

Setup GitLab CI/CD Pipelines
We add a Gitlab CI configuration file .gitlab-ci.yml inside our Android project root. This file essentially contains all the instructions for our CI/CD pipeline.
Let’s understand some of the basic GitLab CI keywords and the code in the YML config file.
- image:Gitlab runners will use specify a Docker image to run the pipeline.
- stages:These are globally defined levels that a pipeline run. It is used by jobs and run in the same order as their definition and each stage can contain different jobs. When one job fails the rest of the jobs in other stages will not be executed.
- stage:This signifies which stage the job belongs to.
- tags:List of tags that are used to select a runner.
- script:The shell script executed by the Gitlab Runner.
- cache:List of files that should be cached between subsequent runs.
- untracked:Cache all files that are untracked in your Git repository.
- only:Define all the branch names for which the job will run.

Setup Fastlane Scripts
Navigate your terminal to your project’s directory and run
fastlane init
It will automatically generate a configuration. You can see the newly created ./fastlane
directory, with the following files:
- Appfile:Defines configuration information that is global to your app.
- Fastfile:Defines the
lanes
that drive the behavior of fastlane.
How to automatically build Android APK, then upload the generated APK to Slack?
First, create local.properties file and clean project, define init_env
lane:
Step 1:Writing Android SDK path in local.properties file.
Step 2:Delete the contents of the build directory, and run
gradle(task: "clean")
Next, build Android APK.
gradle(
task: "assemble",
flavor: options[:flavor],
build_type: options[:build_type],
properties: {
"android.injected.signing.store.file" => ENV["KEYSTORE_PATH"],
"android.injected.signing.store.password" => ENV["KEY_PASSWORD"],
"android.injected.signing.key.alias" => ENV["KEY_ALIAS"],
"android.injected.signing.key.password" => ENV["KEY_PASSWORD"],
}
)
Finally, upload the generated APK to Slack.
file_path = lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH]
file_name = file_path.gsub(/\/.*\//,"")sh "echo Uploading " + file_name + " to Slack"
sh "curl https://slack.com/api/files.upload -F token=[SLACK_TOKEN] -F channels=\"#team_mobile\" -F title=\"" + file_name + "\" -F filename=\"" + file_name + "\" -F file=@" + file_path

Thanks for reading this and I hope this helped you in setting up CI/CD for your project.