Gitlab provides an official description of the .gitlab-ci.yml
file, but that could be a bit lengthy to get started out of the gate. For a basic project, you can use the following as a basis:
image: gradle:jdk11
before_script:
- export GRADLE_USER_HOME=`pwd`/.gradle
cache:
paths:
- .gradle/wrapper
- .gradle/caches
package:
stage: build
script:
- ./gradlew assemble
test:
stage: test
script:
- ./gradlew check
Note that the image
tag is set to maven:latest
in many examples, but in order for Gitlab to compile the project using JDK 11, the image
tag must be set to maven:3-jdk-11
. The GRADLE_USER_HOME
is set to the .gradle
of the current directory that the script is read from to ensure that the Gradle environment is properly configured before starting the build.
The cache
section defines the paths that Gitlab CI should look for cached artifacts and dependencies (which, for a Gradle build, is .gradle/wrapper
and .gradle/cache
). Builds may take a long time to execute, as each build requires downloading all dependencies each time the build is executed; to speed this up, caching can be included to remove the need to repeatedly download dependencies. The specifics for caching may vary for your project. See the official cache
documentation for more information.
The assemble
and check
steps simply run gradle assemble
and gradle check
, respectively. While gradle test
would be sufficient in many cases (as opposed to gradle check
, the check
step includes test
while also including other verification steps. For more information on the difference between check
and test
, see Gradle difference between test and check.
For more information, see the following:
Equivalent Maven example:
image: maven:3-jdk-11
variables:
MAVEN_CLI_OPTS: "--batch-mode"
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
cache:
paths:
- .m2/repository/
- target/
package:
stage: build
script:
- mvn $MAVEN_CLI_OPTS package
test:
stage: test
script:
- mvn $MAVEN_CLI_OPTS test
The --batch-mode
flag runs Maven with default values and does not prompt the user to select any defaults. The -Dmaven.repo.local=.m2/repository
sets the local repository to the location on the build server (Gitlab). which plays into the caching ability of Gitlab. Builds may take a long time to execute, as each build requires downloading all dependencies each time the build is executed; to speed this up, caching can be included to remove the need to repeatedly download dependencies. The specifics for caching may vary for your project. See the official cache
documentation for more information.
The package
and test
steps simply run mvn package
and mvn test
, respectively (with the Maven options described above).