From d21d50f568df76fb51d8e87b361cb07ef893c840 Mon Sep 17 00:00:00 2001 From: Nipuna Perera Date: Mon, 4 Feb 2019 23:19:46 -0600 Subject: [PATCH] Draft create --- .dockerignore | 5 ++ .helmignore | 27 +++++++ Dockerfile | 14 ++++ Jenkinsfile | 81 +++++++++++++++++++ OWNERS | 4 + OWNERS_ALIASES | 6 ++ charts/nipuna-jx-demo-petclinic/.helmignore | 21 +++++ charts/nipuna-jx-demo-petclinic/Chart.yaml | 5 ++ charts/nipuna-jx-demo-petclinic/Makefile | 48 +++++++++++ charts/nipuna-jx-demo-petclinic/README.md | 1 + .../templates/NOTES.txt | 4 + .../templates/_helpers.tpl | 16 ++++ .../templates/deployment.yaml | 43 ++++++++++ .../templates/service.yaml | 23 ++++++ charts/nipuna-jx-demo-petclinic/values.yaml | 34 ++++++++ charts/preview/Chart.yaml | 5 ++ charts/preview/Makefile | 18 +++++ charts/preview/requirements.yaml | 16 ++++ charts/preview/values.yaml | 22 +++++ skaffold.yaml | 30 +++++++ watch.sh | 6 ++ 21 files changed, 429 insertions(+) create mode 100644 .dockerignore create mode 100644 .helmignore create mode 100644 Dockerfile create mode 100644 Jenkinsfile create mode 100644 OWNERS create mode 100644 OWNERS_ALIASES create mode 100755 charts/nipuna-jx-demo-petclinic/.helmignore create mode 100644 charts/nipuna-jx-demo-petclinic/Chart.yaml create mode 100755 charts/nipuna-jx-demo-petclinic/Makefile create mode 100755 charts/nipuna-jx-demo-petclinic/README.md create mode 100755 charts/nipuna-jx-demo-petclinic/templates/NOTES.txt create mode 100755 charts/nipuna-jx-demo-petclinic/templates/_helpers.tpl create mode 100755 charts/nipuna-jx-demo-petclinic/templates/deployment.yaml create mode 100755 charts/nipuna-jx-demo-petclinic/templates/service.yaml create mode 100755 charts/nipuna-jx-demo-petclinic/values.yaml create mode 100644 charts/preview/Chart.yaml create mode 100755 charts/preview/Makefile create mode 100755 charts/preview/requirements.yaml create mode 100755 charts/preview/values.yaml create mode 100644 skaffold.yaml create mode 100644 watch.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..762752c83 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +draft.toml +charts/ +NOTICE +LICENSE +README.md \ No newline at end of file diff --git a/.helmignore b/.helmignore new file mode 100644 index 000000000..747e6e94a --- /dev/null +++ b/.helmignore @@ -0,0 +1,27 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*~ +# Various IDEs +.project +.idea/ +*.tmproj +*.png + +# known compile time folders +target/ +node_modules/ +vendor/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..d00896232 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM openjdk:8-jdk-slim +ENV PORT 8080 +ENV CLASSPATH /opt/lib +EXPOSE 8080 + +# copy pom.xml and wildcards to avoid this command failing if there's no target/lib directory +COPY pom.xml target/lib* /opt/lib/ + +# NOTE we assume there's only 1 jar in the target dir +# but at least this means we don't have to guess the name +# we could do with a better way to know the name - or to always create an app.jar or something +COPY target/*.jar /opt/app.jar +WORKDIR /opt +CMD ["java", "-jar", "app.jar"] \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 000000000..5471442e7 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,81 @@ +pipeline { + agent { + label "jenkins-maven" + } + environment { + ORG = 'npradeeptha' + APP_NAME = 'nipuna-jx-demo-petclinic' + CHARTMUSEUM_CREDS = credentials('jenkins-x-chartmuseum') + } + stages { + stage('CI Build and push snapshot') { + when { + branch 'PR-*' + } + environment { + PREVIEW_VERSION = "0.0.0-SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER" + PREVIEW_NAMESPACE = "$APP_NAME-$BRANCH_NAME".toLowerCase() + HELM_RELEASE = "$PREVIEW_NAMESPACE".toLowerCase() + } + steps { + container('maven') { + sh "mvn versions:set -DnewVersion=$PREVIEW_VERSION" + sh "mvn install" + sh "skaffold version" + sh "export VERSION=$PREVIEW_VERSION && skaffold build -f skaffold.yaml" + sh "jx step post build --image $DOCKER_REGISTRY/$ORG/$APP_NAME:$PREVIEW_VERSION" + dir('charts/preview') { + sh "make preview" + sh "jx preview --app $APP_NAME --dir ../.." + } + } + } + } + stage('Build Release') { + when { + branch 'master' + } + steps { + container('maven') { + + // ensure we're not on a detached head + sh "git checkout master" + sh "git config --global credential.helper store" + sh "jx step git credentials" + + // so we can retrieve the version in later steps + sh "echo \$(jx-release-version) > VERSION" + sh "mvn versions:set -DnewVersion=\$(cat VERSION)" + sh "jx step tag --version \$(cat VERSION)" + sh "mvn clean deploy" + sh "skaffold version" + sh "export VERSION=`cat VERSION` && skaffold build -f skaffold.yaml" + sh "jx step post build --image $DOCKER_REGISTRY/$ORG/$APP_NAME:\$(cat VERSION)" + } + } + } + stage('Promote to Environments') { + when { + branch 'master' + } + steps { + container('maven') { + dir('charts/nipuna-jx-demo-petclinic') { + sh "jx step changelog --version v\$(cat ../../VERSION)" + + // release the helm chart + sh "jx step helm release" + + // promote through all 'Auto' promotion Environments + sh "jx promote -b --all-auto --timeout 1h --version \$(cat ../../VERSION)" + } + } + } + } + } + post { + always { + cleanWs() + } + } +} diff --git a/OWNERS b/OWNERS new file mode 100644 index 000000000..16f506903 --- /dev/null +++ b/OWNERS @@ -0,0 +1,4 @@ +approvers: +- npradeeptha +reviewers: +- npradeeptha diff --git a/OWNERS_ALIASES b/OWNERS_ALIASES new file mode 100644 index 000000000..adc59408c --- /dev/null +++ b/OWNERS_ALIASES @@ -0,0 +1,6 @@ +aliases: +- npradeeptha +best-approvers: +- npradeeptha +best-reviewers: +- npradeeptha diff --git a/charts/nipuna-jx-demo-petclinic/.helmignore b/charts/nipuna-jx-demo-petclinic/.helmignore new file mode 100755 index 000000000..f0c131944 --- /dev/null +++ b/charts/nipuna-jx-demo-petclinic/.helmignore @@ -0,0 +1,21 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*~ +# Various IDEs +.project +.idea/ +*.tmproj diff --git a/charts/nipuna-jx-demo-petclinic/Chart.yaml b/charts/nipuna-jx-demo-petclinic/Chart.yaml new file mode 100644 index 000000000..441be4207 --- /dev/null +++ b/charts/nipuna-jx-demo-petclinic/Chart.yaml @@ -0,0 +1,5 @@ +apiVersion: v1 +description: A Helm chart for Kubernetes +icon: https://raw.githubusercontent.com/jenkins-x/jenkins-x-platform/master/images/java.png +name: nipuna-jx-demo-petclinic +version: 0.1.0-SNAPSHOT diff --git a/charts/nipuna-jx-demo-petclinic/Makefile b/charts/nipuna-jx-demo-petclinic/Makefile new file mode 100755 index 000000000..0bea8031c --- /dev/null +++ b/charts/nipuna-jx-demo-petclinic/Makefile @@ -0,0 +1,48 @@ +CHART_REPO := http://jenkins-x-chartmuseum:8080 +CURRENT=$(pwd) +NAME := nipuna-jx-demo-petclinic +OS := $(shell uname) +RELEASE_VERSION := $(shell cat ../../VERSION) + +build: clean + rm -rf requirements.lock + helm dependency build + helm lint + +install: clean build + helm install . --name ${NAME} + +upgrade: clean build + helm upgrade ${NAME} . + +delete: + helm delete --purge ${NAME} + +clean: + rm -rf charts + rm -rf ${NAME}*.tgz + +release: clean + helm dependency build + helm lint + helm init --client-only + helm package . + curl --fail -u $(CHARTMUSEUM_CREDS_USR):$(CHARTMUSEUM_CREDS_PSW) --data-binary "@$(NAME)-$(shell sed -n 's/^version: //p' Chart.yaml).tgz" $(CHART_REPO)/api/charts + rm -rf ${NAME}*.tgz% + +tag: +ifeq ($(OS),Darwin) + sed -i "" -e "s/version:.*/version: $(RELEASE_VERSION)/" Chart.yaml + sed -i "" -e "s/tag:.*/tag: $(RELEASE_VERSION)/" values.yaml +else ifeq ($(OS),Linux) + sed -i -e "s/version:.*/version: $(RELEASE_VERSION)/" Chart.yaml + sed -i -e "s|repository:.*|repository: $(DOCKER_REGISTRY)\/npradeeptha\/nipuna-jx-demo-petclinic|" values.yaml + sed -i -e "s/tag:.*/tag: $(RELEASE_VERSION)/" values.yaml +else + echo "platfrom $(OS) not supported to release from" + exit -1 +endif + git add --all + git commit -m "release $(RELEASE_VERSION)" --allow-empty # if first release then no verion update is performed + git tag -fa v$(RELEASE_VERSION) -m "Release version $(RELEASE_VERSION)" + git push origin v$(RELEASE_VERSION) diff --git a/charts/nipuna-jx-demo-petclinic/README.md b/charts/nipuna-jx-demo-petclinic/README.md new file mode 100755 index 000000000..7c3946133 --- /dev/null +++ b/charts/nipuna-jx-demo-petclinic/README.md @@ -0,0 +1 @@ +# Java application \ No newline at end of file diff --git a/charts/nipuna-jx-demo-petclinic/templates/NOTES.txt b/charts/nipuna-jx-demo-petclinic/templates/NOTES.txt new file mode 100755 index 000000000..97823be26 --- /dev/null +++ b/charts/nipuna-jx-demo-petclinic/templates/NOTES.txt @@ -0,0 +1,4 @@ + +Get the application URL by running these commands: + +kubectl get ingress {{ template "fullname" . }} diff --git a/charts/nipuna-jx-demo-petclinic/templates/_helpers.tpl b/charts/nipuna-jx-demo-petclinic/templates/_helpers.tpl new file mode 100755 index 000000000..f0d83d2ed --- /dev/null +++ b/charts/nipuna-jx-demo-petclinic/templates/_helpers.tpl @@ -0,0 +1,16 @@ +{{/* vim: set filetype=mustache: */}} +{{/* +Expand the name of the chart. +*/}} +{{- define "name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}} +{{- end -}} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +*/}} +{{- define "fullname" -}} +{{- $name := default .Chart.Name .Values.nameOverride -}} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} +{{- end -}} diff --git a/charts/nipuna-jx-demo-petclinic/templates/deployment.yaml b/charts/nipuna-jx-demo-petclinic/templates/deployment.yaml new file mode 100755 index 000000000..a9b19a9f2 --- /dev/null +++ b/charts/nipuna-jx-demo-petclinic/templates/deployment.yaml @@ -0,0 +1,43 @@ +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + name: {{ template "fullname" . }} + labels: + draft: {{ default "draft-app" .Values.draft }} + chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}" +spec: + replicas: {{ .Values.replicaCount }} + template: + metadata: + labels: + draft: {{ default "draft-app" .Values.draft }} + app: {{ template "fullname" . }} +{{- if .Values.podAnnotations }} + annotations: +{{ toYaml .Values.podAnnotations | indent 8 }} +{{- end }} + spec: + containers: + - name: {{ .Chart.Name }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - containerPort: {{ .Values.service.internalPort }} + livenessProbe: + httpGet: + path: {{ .Values.probePath }} + port: {{ .Values.service.internalPort }} + initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }} + periodSeconds: {{ .Values.livenessProbe.periodSeconds }} + successThreshold: {{ .Values.livenessProbe.successThreshold }} + timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }} + readinessProbe: + httpGet: + path: {{ .Values.probePath }} + port: {{ .Values.service.internalPort }} + periodSeconds: {{ .Values.readinessProbe.periodSeconds }} + successThreshold: {{ .Values.readinessProbe.successThreshold }} + timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }} + resources: +{{ toYaml .Values.resources | indent 12 }} + terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }} diff --git a/charts/nipuna-jx-demo-petclinic/templates/service.yaml b/charts/nipuna-jx-demo-petclinic/templates/service.yaml new file mode 100755 index 000000000..8d2738948 --- /dev/null +++ b/charts/nipuna-jx-demo-petclinic/templates/service.yaml @@ -0,0 +1,23 @@ +apiVersion: v1 +kind: Service +metadata: +{{- if .Values.service.name }} + name: {{ .Values.service.name }} +{{- else }} + name: {{ template "fullname" . }} +{{- end }} + labels: + chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}" +{{- if .Values.service.annotations }} + annotations: +{{ toYaml .Values.service.annotations | indent 4 }} +{{- end }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.externalPort }} + targetPort: {{ .Values.service.internalPort }} + protocol: TCP + name: http + selector: + app: {{ template "fullname" . }} diff --git a/charts/nipuna-jx-demo-petclinic/values.yaml b/charts/nipuna-jx-demo-petclinic/values.yaml new file mode 100755 index 000000000..6dbef4c19 --- /dev/null +++ b/charts/nipuna-jx-demo-petclinic/values.yaml @@ -0,0 +1,34 @@ +# Default values for Maven projects. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +replicaCount: 1 +image: + repository: draft + tag: dev + pullPolicy: IfNotPresent +service: + name: nipuna-jx-demo-petclinic + type: ClusterIP + externalPort: 80 + internalPort: 8080 + annotations: + fabric8.io/expose: "true" + fabric8.io/ingress.annotations: "kubernetes.io/ingress.class: nginx" +resources: + limits: + cpu: 500m + memory: 512Mi + requests: + cpu: 400m + memory: 512Mi +probePath: /actuator/health +livenessProbe: + initialDelaySeconds: 60 + periodSeconds: 10 + successThreshold: 1 + timeoutSeconds: 1 +readinessProbe: + periodSeconds: 10 + successThreshold: 1 + timeoutSeconds: 1 +terminationGracePeriodSeconds: 10 \ No newline at end of file diff --git a/charts/preview/Chart.yaml b/charts/preview/Chart.yaml new file mode 100644 index 000000000..6b684313b --- /dev/null +++ b/charts/preview/Chart.yaml @@ -0,0 +1,5 @@ +apiVersion: v1 +description: A Helm chart for Kubernetes +icon: https://raw.githubusercontent.com/jenkins-x/jenkins-x-platform/master/images/java.png +name: preview +version: 0.1.0-SNAPSHOT diff --git a/charts/preview/Makefile b/charts/preview/Makefile new file mode 100755 index 000000000..497f5b4b3 --- /dev/null +++ b/charts/preview/Makefile @@ -0,0 +1,18 @@ +OS := $(shell uname) + +preview: +ifeq ($(OS),Darwin) + sed -i "" -e "s/version:.*/version: $(PREVIEW_VERSION)/" Chart.yaml + sed -i "" -e "s/version:.*/version: $(PREVIEW_VERSION)/" ../*/Chart.yaml + sed -i "" -e "s/tag:.*/tag: $(PREVIEW_VERSION)/" values.yaml +else ifeq ($(OS),Linux) + sed -i -e "s/version:.*/version: $(PREVIEW_VERSION)/" Chart.yaml + sed -i -e "s/version:.*/version: $(PREVIEW_VERSION)/" ../*/Chart.yaml + sed -i -e "s|repository:.*|repository: $(DOCKER_REGISTRY)\/npradeeptha\/nipuna-jx-demo-petclinic|" values.yaml + sed -i -e "s/tag:.*/tag: $(PREVIEW_VERSION)/" values.yaml +else + echo "platfrom $(OS) not supported to release from" + exit -1 +endif + echo " version: $(PREVIEW_VERSION)" >> requirements.yaml + jx step helm build diff --git a/charts/preview/requirements.yaml b/charts/preview/requirements.yaml new file mode 100755 index 000000000..63db76d11 --- /dev/null +++ b/charts/preview/requirements.yaml @@ -0,0 +1,16 @@ +# !! File must end with empty line !! +dependencies: +- alias: expose + name: exposecontroller + repository: http://chartmuseum.jenkins-x.io + version: 2.3.56 +- alias: cleanup + name: exposecontroller + repository: http://chartmuseum.jenkins-x.io + version: 2.3.56 + + # !! "alias: preview" must be last entry in dependencies array !! + # !! Place custom dependencies above !! +- alias: preview + name: nipuna-jx-demo-petclinic + repository: file://../nipuna-jx-demo-petclinic diff --git a/charts/preview/values.yaml b/charts/preview/values.yaml new file mode 100755 index 000000000..b53ceaa57 --- /dev/null +++ b/charts/preview/values.yaml @@ -0,0 +1,22 @@ + +expose: + Annotations: + helm.sh/hook: post-install,post-upgrade + helm.sh/hook-delete-policy: hook-succeeded + config: + exposer: Ingress + http: true + tlsacme: false + +cleanup: + Args: + - --cleanup + Annotations: + helm.sh/hook: pre-delete + helm.sh/hook-delete-policy: hook-succeeded + +preview: + image: + repository: + tag: + pullPolicy: IfNotPresent \ No newline at end of file diff --git a/skaffold.yaml b/skaffold.yaml new file mode 100644 index 000000000..eaa7caa17 --- /dev/null +++ b/skaffold.yaml @@ -0,0 +1,30 @@ +apiVersion: skaffold/v1beta2 +kind: Config +build: + artifacts: + - image: changeme + context: . + docker: {} + tagPolicy: + envTemplate: + template: '{{.DOCKER_REGISTRY}}/npradeeptha/nipuna-jx-demo-petclinic:{{.VERSION}}' + local: {} +deploy: + kubectl: {} +profiles: +- name: dev + build: + artifacts: + - docker: {} + tagPolicy: + envTemplate: + template: '{{.DOCKER_REGISTRY}}/npradeeptha/nipuna-jx-demo-petclinic:{{.DIGEST_HEX}}' + local: {} + deploy: + helm: + releases: + - name: nipuna-jx-demo-petclinic + chartPath: charts/nipuna-jx-demo-petclinic + setValueTemplates: + image.repository: '{{.DOCKER_REGISTRY}}/npradeeptha/nipuna-jx-demo-petclinic' + image.tag: '{{.DIGEST_HEX}}' diff --git a/watch.sh b/watch.sh new file mode 100644 index 000000000..38e50af8a --- /dev/null +++ b/watch.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +# watch the java files and continously deploy the service +mvn clean install +skaffold run -p dev +reflex -r "\.java$" -- bash -c 'mvn install && skaffold run -p dev'