From 159b9de19a4d0fdd83a75956251bfc3096b40d03 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Wed, 13 May 2020 18:20:57 +0200 Subject: [PATCH] Create a auto build docker image Version of application is calculated by the git tags --- .dockerignore | 13 +++++++++++++ .gitignore | 4 +++- Makefile | 38 ++++++++++++++++++++++++++++++++++++++ README.md | 22 ++++++++++++++++++++++ build.gradle.kts | 42 +++++++++++++++++++++++++++++++++++++++++- docker-compose.yml | 17 ++++++++++++----- docker/app/Dockerfile | 11 +++++++++-- hook/version.sh | 12 ++++++++++++ 8 files changed, 150 insertions(+), 9 deletions(-) create mode 100644 .dockerignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 hook/version.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..6dce35f --- /dev/null +++ b/.dockerignore @@ -0,0 +1,13 @@ +build +out +GH_TOKEN.txt +Makefile +var +gradle +.idea +.gradle +docker +gradlew +gradlew.bat +docker-compose.yml +src/test \ No newline at end of file diff --git a/.gitignore b/.gitignore index 22dc0bf..7d14bd0 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,6 @@ *.ipr *.iws dcproject.iml -/var \ No newline at end of file +/var +GH_TOKEN.txt +allSQL.sql \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..033a2ce --- /dev/null +++ b/Makefile @@ -0,0 +1,38 @@ +.EXPORT_ALL_VARIABLES: +VERSION=$(shell ./hook/version.sh) +GITHUB_USERNAME=$(shell git config user.email) +GITHUB_TOKEN=$(shell cat ./GH_TOKEN.txt) + +# HELP +# This will output the help for each task +# thanks to https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html +.PHONY: help + +help: ## This help. + @awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) + +.DEFAULT_GOAL := help + +build-docker: ## Build the docker image of application + docker build -t dc-project -f docker/app/Dockerfile . + +publish-docker: build-docker ## Publish docker image of application to Github + git diff --quiet --exit-code || (echo "The git is DIRTY !!! You cannot publish this crap!" && exit 1) + cat ./GH_TOKEN.txt | docker login docker.pkg.github.com -u ${GITHUB_USERNAME} --password-stdin + docker tag dc-project docker.pkg.github.com/flecomte/dc-project/dc-project:${VERSION} + docker push docker.pkg.github.com/flecomte/dc-project/dc-project:${VERSION} + +run-docker: ## Build and Run all docker services + docker-compose up -d --build + +publish-jar: ## Publish JAR file to Github + git diff --quiet --exit-code || (echo "The git is DIRTY !!! You cannot publish this crap!" && exit 1) + gradlew publish + +fixtures: ## Import fixtures + bash src/main/resources/sql/fixtures/fixtures.sh + +v: vertion + +vertion: ## Show current version + @echo ${VERSION} diff --git a/README.md b/README.md new file mode 100644 index 0000000..4c10cd9 --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ +# Installation + +## On windows +1. Install git + - Download and install: https://git-scm.com/download/win +2. Install Make + - Go to [ezwinports](https://sourceforge.net/projects/ezwinports/files/). + - Download `make-4.1-2-without-guile-w32-bin.zip` (get the version without guile). + - Extract zip. + - Copy the contents to your `Git\mingw64\` merging the folders, but **do NOT overwrite/replace** any existing files. + +## Run dockers + + +```bash +$ make run-docker +``` + +## Add fixtures +```bash +$ make fixtures +``` \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 8ab36d7..92609e5 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,6 +1,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile import org.owasp.dependencycheck.reporting.ReportGenerator import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar +import org.slf4j.LoggerFactory val ktor_version: String by project val kotlin_version: String by project @@ -11,7 +12,13 @@ val jackson_version: String by project val cucumber_version: String by project group = "com.github.flecomte" -version = "0.0.1" +version = versioning.info.run { + if (dirty) { + versioning.info.full + } else { + versioning.info.lastTag + } +} plugins { jacoco @@ -24,6 +31,7 @@ plugins { id("org.jlleitschuh.gradle.ktlint") version "8.2.0" id("org.owasp.dependencycheck") version "5.1.0" id("org.sonarqube") version "2.7" + id("net.nemerosa.versioning") version "2.13.1" } application { @@ -48,6 +56,38 @@ tasks.withType { tasks { named("shadowJar") { mergeServiceFiles("META-INF/services") + archiveFileName.set("${archiveBaseName.get()}-latest-all.${archiveExtension.get()}") + } +} + +val sourcesJar by tasks.creating(Jar::class) { + archiveClassifier.set("sources") + from(sourceSets.getByName("main").allSource) +} + +publishing { + if (versioning.info.dirty == false) { + repositories { + maven { + name = "dc-project" + group = "com.github.flecomte" + url = uri("https://maven.pkg.github.com/flecomte/dc-project") + credentials { + username = System.getenv("GITHUB_USERNAME") + password = System.getenv("GITHUB_TOKEN") + } + } + } + + publications { + create("dc-project") { + from(components["java"]) + artifact(sourcesJar) + } + } + } else { + LoggerFactory.getLogger("gradle") + .warn("The git is DIRTY !!! You cannot publish this crap! (${versioning.info.full})") } } diff --git a/docker-compose.yml b/docker-compose.yml index f08040b..de35080 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -36,8 +36,8 @@ services: app: container_name: app_${NAME} build: - context: ./build - dockerfile: ../docker/app/Dockerfile + context: . + dockerfile: docker/app/Dockerfile restart: always ports: - ${APP_PORT}:8080 @@ -60,8 +60,12 @@ services: ports: - ${ELASTIC_REST}:9200 - ${ELASTIC_NODES}:9300 + healthcheck: + test: ["CMD", "curl", "-f", "http://elasticsearch:9200"] + interval: 3s + timeout: 2s + retries: 20 - # Database db: container_name: postgresql_${NAME} build: @@ -75,11 +79,14 @@ services: POSTGRES_DB: ${DB_PWD} volumes: - ./var/log/postgresql:/var/log/postgresql:rw - - ./var/postgresql/data:/var/lib/postgresql/data:rw + - db-data:/var/lib/postgresql/data:rw depends_on: - elasticsearch healthcheck: - test: ["CMD", "curl", "-f", "http://elasticsearch:9200/"] + test: [ "CMD", "pg_isready", "-q", "-d", "${DB_NAME}", "-U", "${DB_USER}" ] interval: 3s timeout: 2s retries: 20 + +volumes: + db-data: \ No newline at end of file diff --git a/docker/app/Dockerfile b/docker/app/Dockerfile index 71c4948..0c9401f 100644 --- a/docker/app/Dockerfile +++ b/docker/app/Dockerfile @@ -1,5 +1,12 @@ -FROM adoptopenjdk/openjdk11:jre-11.0.4_11-alpine +#### BUILD #### +FROM gradle:5.6.4-jdk11 AS build +COPY --chown=gradle:gradle . /home/gradle/src +WORKDIR /home/gradle/src +RUN gradle build -x test -x ktlintKotlinScriptCheck -x ktlintTestSourceSetCheck -x ktlintMainSourceSetCheck --no-daemon + +#### RUN #### +FROM adoptopenjdk/openjdk11:jre-11.0.4_11-alpine ENV APPLICATION_USER ktor RUN adduser -D -g '' $APPLICATION_USER @@ -8,7 +15,7 @@ RUN chown -R $APPLICATION_USER /app USER $APPLICATION_USER -COPY ./libs/dcproject-0.0.1-all.jar /app/dcproject.jar +COPY --from=build /home/gradle/src/build/libs/dcproject-latest-all.jar /app/dcproject.jar WORKDIR /app CMD ["java", "-server", "-XX:+UnlockExperimentalVMOptions", "-XX:InitialRAMFraction=2", "-XX:MinRAMFraction=2", "-XX:MaxRAMFraction=2", "-XX:+UseG1GC", "-XX:MaxGCPauseMillis=100", "-XX:+UseStringDeduplication", "-jar", "dcproject.jar"] diff --git a/hook/version.sh b/hook/version.sh new file mode 100644 index 0000000..8ac219a --- /dev/null +++ b/hook/version.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +set -e + +if [[ $(git describe --tags --dirty) =~ ^V?([0-9][0-9.]*(-dirty)?)$ ]]; then + VERSION="${BASH_REMATCH[1]}" +elif [[ $(git describe --tags --dirty) =~ ^V?([0-9][0-9.]*)-([0-9]+)-g(.+(-dirty)?)$ ]]; then + VERSION="${BASH_REMATCH[1]}-${BASH_REMATCH[3]}" +else + exit 1 +fi + +echo $VERSION