The process: Whenever a change is pushed to the NextMidas Gitlab project, a Gitlab job should be started for this project. Jobs can also be started manually. Inside Gitlab, the Docker-builder image will be pulled from the local private Docker registry to provide access to Gradle, OpenJDK, and Nexus. This project will run a Gradle build of NeXtMidas and publish all NextMidas Gradle jar files to the Nexus repo.
# This is the Gitlab configuration file for NeXtMidas CI
stages:
- build
before_script:
- export COMMIT_TIME="$(date --date=${CI_COMMIT_TIMESTAMP} +%Y%m%d%H%M%S)"
- export BUILD_TIME="$(date +%Y%m%d%H%M%S)"
- export TAG_PREFIX=`awk 'BEGIN { printf("%s/%s:%s",tolower(ENVIRON["DOCKER_REGISTRY"]),
tolower(ENVIRON["CI_PROJECT_NAME"]),
tolower(ENVIRON["CI_COMMIT_BRANCH"])); }' /dev/null`
- export IMAGE_TAG="${TAG_PREFIX}-${CI_COMMIT_SHORT_SHA}-${BUILD_TIME}"
- export GRADLE_TARGETS="clean build"
- export GRADLE_FLAGS="--refresh-dependencies -Dorg.gradle.internal.publish.checksums.insecure=true -x test"
- if [[ "${CI_COMMIT_BRANCH}" = "ops" ]]; then export GRADLE_TARGETS="${GRADLE_TARGETS} publish"; fi
variables:
# Location of the Docker registry
DOCKER_REGISTRY: ${DOCKER_REG_ADDR}
build:
# Grab the Docker image for the NeXtMidas build from the Docker Registry
# The Docker Registry Address (DOCKER_REG_ADDR), Docker image name (DOCKER_IMAGE_NAME),
# and Docker image tag (DOCKER_IMAGE_TAG) are specified as GitLab CI/CD project variables
image: ${DOCKER_REG_ADDR}/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}
stage: build
services:
# Provides access to the Docker daemon inside the current NeXtMidas Gitlab job
- ${DOCKER_REG_ADDR}/docker:dind
script:
# Update current Centos 7 environment
- yum update -y
# .repo file must be added back in after doing a yum update
- rm /etc/yum.repos.d/*
- cp /usr/local/centos-reposerver.repo /etc/yum.repos.d/
# install gcc-c++
- yum install -y gcc-c++
# Convert NeXtMidas to a Gradle project using softlinks
- export NMROOT=$(pwd)
- gradle/linkgradle.sh
# Print out environment configuration for the current NeXtMidas Gitlab job
- echo "==== Config ====";
echo "GITLAB_USER_NAME = ${GITLAB_USER_NAME}";
echo "CI_COMMIT_MESSAGE = ${CI_COMMIT_MESSAGE}";
echo "COMMIT_TIME = ${COMMIT_TIME}";
echo "BUILD_TIME = ${BUILD_TIME}";
echo "CI_BUILD_NAME = ${CI_BUILD_NAME}";
echo "IMAGE_TAG = ${IMAGE_TAG}";
echo "GRADLE_TARGETS = ${GRADLE_TARGETS}";
echo "GRADLE_FLAGS = ${GRADLE_FLAGS}";
# Run the NeXtMidas Gradle build using the Gradle env vars defined above
- echo "==== Starting Gradle Build ===="
- gradle --stop
- sleep 1
- find ${HOME}/.gradle -name "*.lock" | xargs rm
- gradle --no-daemon ${GRADLE_TARGETS} ${GRADLE_FLAGS}
- find -name '*.jar' | sort
# This is the Gitlab configuration file for building the Docker-builder Docker image which will be used by the NeXtMidas Gitlab project
stages:
- build
before_script:
- docker info
- export VERSION=$(date +%y-%m-%d)
- export BUILDER_TAG_NAME=${IMAGE_NAME}":builder-$VERSION"
variables:
# Name of the Docker-builder Docker image to be pushed to the Docker registry
IMAGE_NAME: "${DOCKER_REG_ADDR}/${DOCKER_IMAGE_NAME}"
build-full:
# Use the docker-in-docker client image as the base image for the current java-docker-image Gitlab job
image: ${DOCKER_REG_ADDR}/docker:latest
stage: build
script:
- echo $BUILDER_TAG_NAME
# Build the Docker-builder Docker image to be used by NeXtMidas
- docker build --network=host -f Dockerfile-builder -t $BUILDER_TAG_NAME .
# Tag and push the Docker-build Docker image to be used by NeXtMidas
- docker tag $BUILDER_TAG_NAME ${IMAGE_NAME}:"latest-builder"
- docker push ${IMAGE_NAME}":latest-builder"
- docker push $BUILDER_TAG_NAME
- echo $BUILDER_TAG_NAME
only:
- master
# Start with a basic Centos 7 Docker image
FROM [hostname]:[port]/centos:7
# Update repo based on the current environent and conditions/constraints
RUN rm /etc/yum.repos.d/*
COPY centos-reposerver.repo /usr/local/
RUN cp /usr/local/centos-reposerver.repo /etc/yum.repos.d/
# Install Openjdk 11 and unzip tools
RUN yum install java-11-openjdk-devel unzip -y
ENV JAVA_HOME /usr/lib/jvm/java-11-openjdk
ENV JAVAHOME ${JAVA_HOME}
ENV PATH $PATH:${JAVA_HOME}/bin
# Working directory for this image
WORKDIR /opt
# Install, configure, and run the Gradle tool
COPY gradle-6.3-all.zip ./
RUN jar -xf gradle-6.3-all.zip
RUN rm gradle-6.3-all.zip
RUN ln -sf /opt/gradle-6.3 /opt/gradle
ENV GRADLE_BIN /opt/gradle/bin
RUN chmod +x ${GRADLE_BIN}/gradle
ENV PATH $PATH:${GRADLE_BIN}
ENV JAVA_TOOL_OPTIONS -Dfile.encoding=UTF8
RUN gradle
# Copy gradle.properties file into this image
COPY gradle.properties /root/.gradle/
# Unzip SSL certificates to be used for Gradle
COPY ssl.zip /usr/local/ssl.zip
RUN unzip /usr/local/ssl.zip -d /usr/local/
RUN rm /usr/local/ssl.zip
sudo docker build --network=host -t docker:20.10 .
sudo docker build --network=host -t docker:latest .
sudo docker build --network=host -t docker:dind .
sudo docker build --network=host -t docker:dind-20.10 .
sudo vim /etc/docker/daemon.json
{
"storage-driver": "overlay2"
}
sudo systemctl restart docker
sudo docker network create gitlab-runner-net
sudo docker run -d --name gitlab-dind --privileged --restart always --network gitlab-runner-net -v /var/lib/docker docker:20.10-dind --storage-driver=overlay2
sudo docker build --network=host -t gitlab-runner:centos .
sudo docker run -d --name gitlab-runner --restart always --network gitlab-runner-net -v /etc/gitlab-runner/config.toml:/etc/gitlab-runner/config.toml -e DOCKER_HOST=tcp://gitlab-dind:2375 gitlab-runner:centos
sudo docker run --network=host -it --rm -v /etc/gitlab-runner/config.toml:/etc/gitlab-runner/config.toml -v /etc/pki/tls/certs:/etc/pki/tls/certs -v /etc/pki/tls/private:/etc/pki/tls/private gitlab-runner:centos gitlab-runner register --executor docker --docker-image [host]:[port]/docker:20.10 --docker-volumes /var/run/docker.sock:/var/run/docker.sock
sudo vim /etc/gitlab-runner/config.toml