Skip to content

Instantly share code, notes, and snippets.

@ngandrass
Created May 15, 2022 11:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngandrass/9862fada8c095dc4cb16e6c30322d0a5 to your computer and use it in GitHub Desktop.
Save ngandrass/9862fada8c095dc4cb16e6c30322d0a5 to your computer and use it in GitHub Desktop.
Simple GitLab CI/CD .gitlab-ci.yml configuration for Gradle (Java / JDK 17) projects. With Docker build and push to repository container registry.
image: gradle:7.4-jdk17-alpine
stages:
- build
- test
- packaging
# Disable the Gradle daemon for Continuous Integration servers as correctness
# is usually a priority over speed in CI environments. Using a fresh
# runtime for each build is more reliable since the runtime is completely
# isolated from any previous builds.
variables:
GRADLE_OPTS: "-Dorg.gradle.daemon=false"
before_script:
- GRADLE_USER_HOME="$(pwd)/.gradle"
- export GRADLE_USER_HOME
build:
stage: build
script: gradle --build-cache assemble
artifacts:
paths:
- build/distributions/*
cache:
key: "$CI_COMMIT_REF_NAME"
policy: push
paths:
- build
- .gradle
test:
stage: test
script: gradle check
cache:
key: "$CI_COMMIT_REF_NAME"
policy: pull
paths:
- build
- .gradle
docker-build:
image: docker:latest
stage: packaging
services:
- docker:dind
before_script:
# Credentials are automatically passed in by GitLab CI runner
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
# Default branch leaves tag empty (= latest tag)
# All other branches are tagged with the escaped branch name (commit ref slug)
script:
- |
if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then
tag=""
echo "Running on default branch '$CI_DEFAULT_BRANCH': tag = 'latest'"
else
tag=":$CI_COMMIT_REF_SLUG"
echo "Running on branch '$CI_COMMIT_BRANCH': tag = $tag"
fi
- docker build --pull -t "$CI_REGISTRY_IMAGE${tag}" .
- docker push "$CI_REGISTRY_IMAGE${tag}"
rules:
- if: $CI_COMMIT_BRANCH
exists:
- Dockerfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment