I have written a Jenkins + Android packaging practice before, but it is sometimes inconvenient to rely on the host system;

For example, if the colleague is Windows, relevant shell code needs to be rewritten into BAT,and Android SDK needs to be re-installed and configured.

So I want to consider the use of Dockerfile, a compilation of everywhere to use, online search did not find satisfactory, hand one;

Overall, it is much easier to use, but much slower than running the Jenkins War package directly. I wonder if there is something wrong with my configuration or everyone else does.

Environment: macOS:10.13/Docker Desktop :2.0.0.3

jenkins_android_dockerfile

Common docker commands

#Start docker service: docker.app before using docker command

#Search the mirror
docker search image_name

#Download mirror
docker pull image_name

#Based on an image to create and run the container, the reference: http://www.runoob.com/docker/docker-run-command.htmlDocker run -i -t Ubuntu :15.10 /bin/bash#-I allows you to interact with the standard input (STDIN) inside the container
#-t Specifies a dummy terminal or terminal in the new container
# -dThe background
#-p Port mapping format: Host host port: container port
#--name specifies a name for the container, for example --name="hello_container"
#-- DNS Specifies the DNS server for the container. By default, it is the same as the host, for example, -- DNS 8.8.8.8
#- v/etc/localtime: / etc/localtime let containers and servers the same time Settings

#The list of local mirrors is displayed
docker iamges

#Delete an image, assuming an image exists
docker rmi [image_name | image_id]

#Restart a stopped container docker start container_info -i
docker start [container_name | container_id] [-option]
docker restart [container_name | container_id]

#Stop a running container
docker stop [container_name | container_id]

#Displays running containers
docker ps

#Display all containers (including stopped ones)
docker ps -a

#Delete multiple containers
docker rm container_id1 container_id2

#Batch delete all exited containers
sudo docker rm $(sudo docker ps -qf status=exited)

#View logs within the specified container
# -fReal-time trace log
docker logs [-f] container_name

#Save the changes to the container and generate a new iAMGE locally
docker commit [-m "msg"] [-a "author_name"] [container_name | container_id] new_image_name

#Go to the running container consoleDocker exec - it [the -u root] [container_name | container_id] / bin/bash # by -u specifies the login account docker attach [container_name | Container_id] # CTRL + C will stop container_id.
#Copies files between host host and containerDocker cp [host_file_path] [container_id:target_path_on_container] # Copy the host file/directory to the specified path of the container [container_id:target_path_on_container] [host_file_path] # Copy the specified file in the container to the specified path on the host
#Create an image from the Dockerfile file in the current directory
#Cache is used by default, with --no-cache =trueTo disableDocker build [--no-cache=true] -t [image_name]. Dockerfile docker build -t [image_name] -f ./dockerfile_name .
#Look at the environment variables in the container
docker exec -it [container_name | container_id] env
docker inspect [container_name | container_id]
Copy the code

The docker version Jenkins

Go to the Jenkins official website download list, click docker version, will jump to docker Hub, according to the instructions know the image name: Jenkins/Jenkins;

In addition, Jenkins is developed in Java, and the image of Jenkins should already have the basic system and JDK environment, so we don’t need to install it in the future.

At the same time, Jenkins plug-in and global tool configuration options, you can directly install gradle/ JDK /git and other tools, so only the Android SDK needs to be installed when creating the image.

Android SDK installation method

Method 1: Download and install the software online

The official website does not provide the full SDK package directly, but provides the sdkManager tool, which can be used to download other required components;

Sdkmanager download address

Sdkmanager command description

Sdkmanager - list # list of the available packages sdkmanager "platform - the tools" "platforms; Android-28 "# Install the specified package and it will be automatically downloaded to the tools/ equivalent directory of sdkManagerCopy the code

Method 2: Copy it from the host host

DockerFile command ADD can copy a file on the host to the container, so you can download the Android SDK on the host in advance, and then use this command to copy it.

Dockerfile is written and run

Official document

Complete dockerfile

# Build a new image based on the existing mirror Jenkins/Jenkins
FROM jenkins/jenkins

# set variable
ENV USR_LOCAL="/usr/local" \
     ANDROID_HOME="${USR_LOCAL}/AndroidSdk" \
     SDK_TOOL_URL="https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip"

Create the Android SDK directory and download sdkManager
RUN mkdir -p ${ANDROID_HOME} \
     && cd $ANDROID_HOME \
     && curl -o sdk.zip $SDK_TOOL_URL \
     && unzip sdk.zip \
     && rm sdk.zip

Install other packages of Android SDK. Enter Y because there will be a licence here and the installation will only be done after the user agrees
RUN echo y | ${ANDROID_HOME}/tools/bin/sdkmanager "platform-tools" "platforms; android-28" "build-tools; 28.0.3"

# Set environment variables: Add the Android SDK PATH to the PATH
ENV PATH ${ANDROID_HOME}/tools:${ANDROID_HOME}/tools/bin:${ANDROID_HOME}/platform-tools:${PATH}
Copy the code

After creating the dockerfile, execute:

#If the dockerfile file name is jenkins_android_dockerfile, the my_Jenkins image will be created
docker build -t my_jenkins -f ./jenkins_android_dockerfile .

#Check whether the my_Jenkins mirror is created
docker images | grep my_jenkins

#Build a new container (Container_name_jenkins_Android) based on the image you just created (my_jenkins) and run it
#-p Port mapping format: Host host port: container port
#--name Specifies a name for the container
#-v Directory mount format: Host Host directory path: mount path in the container
docker run -itd -p 8080:8080 -p 50000:50000 --name container_name_jenkins_android --privileged=true  -v /Users/lynxz/host_path:/var/jenkins_home my_jenkins

#Finally, you can use Jenkins by clicking on the website http://localhost:8080 in your browser
Copy the code

The /var/jenkins_home directory (the specific path can be obtained by checking the jenkins_home environment variable), and the entire Jenkins plugin /job configuration can be directly copied when the migration is needed. I tested jobs/ is ok.