What should be the .gitlab-ci.yml file that activates Gitlab to build a project and run tests?

Pasha :

I have a Gradle Java 11 project that resides into Gitlab. I wanted to introduce gitlab-ci.yml to force Gitlab to build a project and run tests on each push to a remote branch.

What should it look like?

I'm pretty sure that a similar question already has been asked but I couldn't find it, so I appreciate your help.

Justin Albano :

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).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=116067&siteId=1