Skip to content

Instantly share code, notes, and snippets.

@Frechet
Last active November 2, 2019 21:44
Show Gist options
  • Select an option

  • Save Frechet/41ebaa3c2ac588c0ccf1d38c1e1e942c to your computer and use it in GitHub Desktop.

Select an option

Save Frechet/41ebaa3c2ac588c0ccf1d38c1e1e942c to your computer and use it in GitHub Desktop.
gitlab-ci-maven-deploy
# This template will build and test your projects
# * Caches downloaded dependencies and plugins between invocation.
# * Verify but don't deploy merge requests.
# * For master branch push sonar report about app to SonarQube server and deploy docker image of app to docker registry.
variables:
# This will suppress any download for dependencies and plugins or upload messages which would clutter the console log.
# `showDateTime` will show the passed time in milliseconds. You need to specify `--batch-mode` to make this work.
MAVEN_OPTS: "-Dhttps.protocols=TLSv1.2 -Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"
# As of Maven 3.3.0 instead of this you may define these options in `.mvn/maven.config` so the same config is used
# when running from the command line.
# `installAtEnd` and `deployAtEnd` are only effective with recent version of the corresponding plugins.
MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"
# When using dind service, we need to instruct docker, to talk with
# the daemon started inside of the service. The daemon is available
# with a network connection instead of the default
# /var/run/docker.sock socket. docker:19.03 does this automatically
# by setting the DOCKER_HOST in
# https://github.com/docker-library/docker/blob/d45051476babc297257df490d22cbd806f1b11e4/19.03/docker-entrypoint.sh#L23-L29
#
# The 'docker' hostname is the alias of the service container as described at
# https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#accessing-the-services.
#
# Note that if you're using the Kubernetes executor, the variable
# should be set to tcp://localhost:2375 because of how the
# Kubernetes executor connects services to the job container
# DOCKER_HOST: tcp://localhost:2375
#
# For non-Kubernetes executors, we use tcp://docker:2376
# Fot docker:^19 see https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#docker-cannot-connect-to-the-docker-daemon-at-tcpdocker2375-is-the-docker-daemon-running
DOCKER_HOST: tcp://docker:2376
# Specify to Docker where to create the certificates, Docker will
# create them automatically on boot, and will create
# `/certs/client` that will be shared between the service and job
# container, thanks to volume mount from config.toml
DOCKER_TLS_CERTDIR: "/certs"
# Using the OverlayFS driver. See https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#using-the-overlayfs-driver
DOCKER_DRIVER: overlay2
# For deploy we must create own image with docker and application platforms.
DOCKER_CI_IMAGE: docker-18-openjdk-11-node
# Cache downloaded dependencies and plugins between builds.
# To keep cache across branches add 'key: "$CI_JOB_NAME"'
cache:
paths:
- .m2/repository
# Declare stages
stages:
- verify
- deploy
# For merge requests do not `deploy` but only run `verify`.
# See https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
verify:
stage: verify
image: openjdk:11-slim
script:
- ./mvnw $MAVEN_CLI_OPTS verify
except:
- master
# Deploy only master branch
.deploy-script: &deploy-script |
./mvnw $MAVEN_CLI_OPTS deploy -Psonar,docker
# Skip maven deploy plugin execution for phase deploy, because we push images to docker registry only
-Dmaven.deploy.skip=true
# Sonar params
-Dsonar.host.url=$SONAR_URL -Dsonar.login=$SONAR_TOKEN -Dsonar.projectName=my-super-project
# Docker registry params
-Ddockerfile.repository=$DOCKER_REGISTRY_URL$DOCKER_REGISTRY_IAMGE -Ddockerfile.tag=$DOCKER_REGISTRY_TAG -Ddockerfile.username=$DOCKER_REGISTRY_LOGIN -Ddockerfile.password=$DOCKER_REGISTRY_PASS
deploy:
stage: deploy
image: $DOCKER_REGISTRY_URL$DOCKER_CI_IMAGE
# Add docker service with "dind" version for docker-in-docker mechanisms
# See https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#what-is-a-service
# See https://docs.gitlab.com/ee/ci/docker/using_docker_build.html
services:
- docker:18-dind
before_script:
- docker info
script:
- *deploy-script
only:
- master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment