Pipelines is an automatic build, test and deploy service built into Bitbucket.
Install Docker CE
Bitbucket Pipelines uses docker to manage their builds. So, we first need to get docker up and running. Here we are using Docker CE on Ubuntu, see Get Docker CE for Ubuntu for the detailed instructions.
$ sudo apt-get remove docker docker-engine docker.io
$ sudo apt-get update && sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
$ sudo apt-get update && sudo apt-get install docker-ce
In order to be able to run docker as a non-root user, execute the following:
$ sudo groupadd docker
$ sudo usermod -aG docker $USER
Log out and log in back.
For details, see Post-installation steps for Linux.
Build Docker image
Copy and save the following as a Dockerfile
in your machine:
# https://confluence.atlassian.com/bitbucket/use-docker-images-as-build-environments-792298897.html
FROM atlassian/default-image:2
# Sets language to UTF8 : this works in pretty much all cases
ENV LANG en_US.UTF-8
# Never ask for confirmations
ENV DEBIAN_FRONTEND noninteractive
# Install Android SDK
RUN mkdir -p /root/.android && touch /root/.android/repositories.cfg
RUN rm -rf /usr/local/android-sdk/ \
&& mkdir -p /usr/local/android-sdk/.android/ \
&& touch /usr/local/android-sdk/.android/repositories.cfg
ENV ANDROID_HOME /usr/local/android-sdk
RUN wget https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip
RUN unzip sdk-tools-linux-*.zip -d $ANDROID_HOME/
RUN rm sdk-tools-linux-*.zip
RUN yes | $ANDROID_HOME/tools/bin/sdkmanager --licenses
RUN $ANDROID_HOME/tools/bin/sdkmanager "platforms;android-28" "extras;android;m2repository"
# Etc
ENV ANDROID_SDK_HOME $ANDROID_HOME
ENV PATH ${INFER_HOME}/bin:${PATH}
ENV PATH $PATH:$ANDROID_SDK_HOME/tools
ENV PATH $PATH:$ANDROID_SDK_HOME/platform-tools
ENV JAVA_OPTS "-Xms1536m -Xmx1536m"
RUN mkdir -p /root/.gradle \
&& echo $'org.gradle.caching=true' > /root/.gradle/gradle.properties
Run the following command to build it:
$ docker build \
--memory=4g --memory-swap=4g \
-t bozoraka/android-build:latest \
-f <path_to>/Dockerfile
--memory=4g --memory-swap=4g
options are to simulate memory limitations in Bitbucket Pipelines.-t bozoraka/android-build:latest
is the image tag, it can be changed to anything you like.-f <path_to>/Dockerfile
needs to point to the above savedDockerfile
. If the file is in the current directory, you may simply pass.
Run Docker
$ docker run -it \
--volume=/home/dev/bozoraka-android:/root/build \
--workdir=/root/build \
--memory=4g --memory-swap=4g --memory-swappiness=0 \
--entrypoint=/bin/bash \
bozoraka/android-build
--volume=/home/dev/bozoraka-android
is the location of local development source code.--memory=4g --memory-swap=4g --memory-swappiness=0
is to simulate Bitbucket Pipelines memory limitations.- The last argument must be the same tag as used in the build process. Can be image ID as outputed by
$ docker images
command.
When the above command is run, you will dropped to docker shell. From there execute the scripts in bitbucket-pipelines.yml
file in your repo.
Monitor memory usage
To monitor memory usage, first connect to the running docker in a separete terminal.
docker exec -it <container_id> bash
You can find the running container ID with docker ps
command.
Then execute the following script:
#!/bin/sh
cat "/sys/fs/cgroup/memory/memory.stat"
while true; do ps -auxf; sleep 1; done &
while true; do echo "Memory usage:" && cat /sys/fs/cgroup/memory/memory.usage_in_bytes | awk '{$1=$1/1024^3; print $1,"GB";}'; sleep 1; done &