/Docker-GradleTasks-Template

Dockerize gradle project and run gradle tasks with different configurations

Primary LanguageJava

Docker-GradleTasks-Template

A demo project shows how to dockerize a Gradle project and build & run Gradle Tasks in Docker.

Actually it's not necessary to run Gradle Tasks in Docker since Gradle already handle the project dependencies for you.

This demo project is for practice only.

Contents

Gradle Project Overview

There are 2 tasks in this Gradle project: testGoogle and testYahoo, And there are 3 parameters have to be set in this Gradle project: browser, env and machine.

The value of browser could be: Chrome or IE, which means the tasks would be run on which browser.

The value of env could be: SIT or Stage, which means the tasks would test which environment.

The value of machine could be: local or remote, which means the tasks would be on a local or remote machine.

We will use Gradle -P flag to pass these parameters as project properties. e.g.

gradle clean build -Pbrowser=Chrome -Penv=SIT -Pmachine=remote run testGoogle testYahoo copyTestResults

will set browser = Chrome, env = SIT and machine = remote, ans store these values in a .properties file.

Dockerize Gradle Project

We'd like to know whether we can dockerize this Gradle project to run the mentioned Gradle tasks in a docker container and still:

  • Benefit from Gradle Build Cache

Otherwise, every Gradle build in the docker container would fetch the dependencies again, and it could take a long time.

  • Archive the test results

We want to archive the test results on our host machine before the docker container killed.

Gradle Build Cache

To make every docker container share the same Gradle Build Cache, we have to create a docker volume on our own, and mount it on the gradle build cache path of the docker container. e.g.

docker volume create --name gradle-cache
docker run -v gradle-cache:/home/molly/.gradle

Archive Test Results

To store every test results on our host machine, we just need to bind mount a host machine directory with the docker container. e.g.

docker run -v /C/Code/docker-gradle-template/archive:/home/molly/app/archive

Demo

The content below demonstrates 3 different scenarios and shows how to

3 Scenarios

  • Scenario I

Pass browser = Chrome, env = SIT, machine = remote to Gradle, and run testGoogle, testYahoo tasks, finally copy the test result to archive folder.

  • Scenario II

Pass browser = Chrome, env = Stage, machine = remote to Gradle, and run testGoogle, testYahoo tasks, finally copy the test result to archive folder.

  • Scenario III

Pass browser = IE, env = SIT, machine = local to Gradle, and run testGoogle, testYahoo tasks, finally copy the test result to archive folder.

Run with Gradle

  • Scenario I
gradle clean build -Pbrowser=Chrome -Penv=SIT -Pmachine=remote run testGoogle testYahoo copyTestResults
  • Scenario II
gradle clean build -Pbrowser=Chrome -Penv=Stage -Pmachine=remote run testGoogle testYahoo copyTestResults
  • Scenario III
gradle clean build -Pbrowser=IE -Penv=SIT -Pmachine=local run testGoogle testYahoo copyTestResults

Run with Docker

We have to mount a gradle cache volume and bind mount a test results archive volume.

Don't worry about the repeat "docker volume create". If a docker volume trying to be created with an existing name, the existing one would be directly used, no new volume would be created.

  • Scenario I
docker volume create --name gradle-cache
docker build -t browser-test .
docker run -v gradle-cache:/home/molly/.gradle -v /C/Code/docker-gradle-template/archive:/home/molly/app/archive -it browser-test SIT Chrome remote
  • Scenario II
docker volume create --name gradle-cache
docker build -t browser-test .
docker run -v gradle-cache:/home/molly/.gradle -v /C/Code/docker-gradle-template/archive:/home/molly/app/archive -it browser-test Stage Chrome remote
  • Scenario III
docker volume create --name gradle-cache
docker build -t browser-test .
docker run -v gradle-cache:/home/molly/.gradle -v /C/Code/docker-gradle-template/archive:/home/molly/app/archive -it browser-test SIT IE local

Run with docker-compose

We still have to mount a gradle cache volume and bind mount a test results archive volume.

We can hide the mount syntax in the docker-compose.yml file, but the creating external docker volume command cannot be written in docker-compose.yml file, we still have to execute it explicitly.

  • Scenario I
docker volume create --name gradle-cache
docker-compose -f docker-compose.yml -f docker-compose.sit.chrome.yml up --build
  • Scenario II
docker volume create --name gradle-cache
docker-compose -f docker-compose.yml -f docker-compose.stage.chrome.yml up --build
  • Scenario III
docker volume create --name gradle-cache
docker-compose -f docker-compose.yml -f docker-compose.sit.ie.local.yml up --build