Skip to content

Commit

Permalink
Merge pull request #6 from Eccoar/186_pipeline_user
Browse files Browse the repository at this point in the history
186 pipeline user
  • Loading branch information
devsalula authored May 3, 2021
2 parents ee37d84 + 47a42b3 commit 34b3978
Show file tree
Hide file tree
Showing 14 changed files with 577 additions and 1 deletion.
204 changes: 204 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
stages:
- code
- tests
- quality scan
- quality metrics
- build
- build chart
- deploy

.only_branches: &only_branches
only:
- /^(\d{2,})(\_[a-zA-Z0-9]+)+$/
- /^((hotfix)\_\d{2,})(\_[a-zA-Z0-9]+)+$/
- main
- devel
- tags

########### Code ###########

eslint:
image: node:14-slim
stage: code
before_script:
- npm install
script:
- npm run lint-report
artifacts:
public: true
paths:
- lint-report.html
extends:
- .only_branches

########### Tests ###########

unit_test:
image: node:14-slim
stage: tests
before_script:
- npm install
- cp $FIREBASE_SERVICE service-account.json
- cp $FIREBASE_CLIENT firebase-account.json
script:
- npm run test
artifacts:
public: true
paths:
- test-report.xml
- coverage/lcov.info
extends:
- .only_branches

########### Quality Scan ###########

sonar_scanner:
image: sonarsource/sonar-scanner-cli:4.4
stage: quality scan
script:
- sonar-scanner -Dproject.settings=$CI_PROJECT_DIR/sonar-project.properties -Dsonar.login=$SONAR_TOKEN
extends:
- .only_branches
dependencies:
- unit_test

sonar metrics:
image: debian
stage: quality metrics
variables:
METRICS_URL: https://sonarcloud.io/api/measures/component_tree?component=Eccoar_2020.2-Eccoar_Users&metricKeys=files,functions,complexity,comment_lines_density,duplicated_lines_density,security_rating,tests,test_success_density,test_execution_time,reliability_rating,ncloc&ps=500
before_script:
- apt update
- apt install wget git -y
- mkdir tmp && cd tmp
script:
- wget $METRICS_URL -O eccoar-user-$CI_COMMIT_BRANCH-metrics.json
only:
- main
- tags
- 186_pipeline_user
artifacts:
public: true
paths:
- tmp/*.json

########### Build ###########

.build_:
image: docker:19.03.12
services:
- docker:19.03.12-dind
stage: build
before_script:
- docker login -u $DOCKER_USER -p $DOCKER_PASSWORD
- export DOCKER_IMAGE=$(echo "$DOCKER_IMAGE:$DOCKER_TAG" | tr '[:upper:]' '[:lower:]')
- cp $FIREBASE_SERVICE service-account.json
- cp $FIREBASE_CLIENT firebase-account.json
script:
- docker build . -t $DOCKER_IMAGE
- docker push $DOCKER_IMAGE
extends:
- .only_branches

build general:
variables:
DOCKER_TAG: $CI_COMMIT_SHORT_SHA
DOCKER_IMAGE: $DOCKER_USER/$CI_PROJECT_NAME
extends:
- .only_branches
- .build_

build homolog:
variables:
DOCKER_TAG: homolog
DOCKER_IMAGE: $DOCKER_USER/$CI_PROJECT_NAME
only:
- main
- 186_pipeline_user
extends:
- .build_

build prod:
variables:
DOCKER_TAG: $CI_COMMIT_TAG
DOCKER_IMAGE: $DOCKER_USER/$CI_PROJECT_NAME
only:
- tags
extends:
- .build_

########## Chart ##########

.helm_build:
stage: build chart
image: dtzar/helm-kubectl:3.5.0
script:
- cd chart
- kubectl get services -n $AMBIENTE
- helm lint ./$CHART_NAME
- echo $CHART_VERSION
- helm package ./$CHART_NAME --app-version $APP_VERSION --version $CHART_VERSION
artifacts:
public: true
paths:
- chart/$CHART_NAME-$CHART_VERSION.tgz

helm hm:
variables:
CHART_NAME: eccoar-user
AMBIENTE: homolog
APP_VERSION: $CI_COMMIT_SHORT_SHA
CHART_VERSION: 1.0.0-hm.$CI_COMMIT_REF_SLUG.$CI_COMMIT_SHORT_SHA
only:
- 186_pipeline_user
- main
extends:
- .helm_build

helm prod:
variables:
CHART_NAME: eccoar-user
AMBIENTE: prod
APP_VERSION: $CI_COMMIT_TAG
CHART_VERSION: $CI_COMMIT_TAG
only:
- tags
extends:
- .helm_build

########## Deploy ##########

.deploy_:
image: dtzar/helm-kubectl:3.5.0
stage: deploy
before_script:
- export CHART_PATH=chart/$CHART_NAME-$CHART_VERSION.tgz
script:
- kubectl get services -n $AMBIENTE
- helm upgrade $WORKLOAD_NAME $CHART_PATH --install --namespace $AMBIENTE --set image.tag=$DOCKER_TAG

deploy hm:
variables:
DOCKER_TAG: homolog
WORKLOAD_NAME: eccoar-user
AMBIENTE: homolog
CHART_NAME: eccoar-user
CHART_VERSION: 1.0.0-hm.$CI_COMMIT_REF_SLUG.$CI_COMMIT_SHORT_SHA
only:
- 186_pipeline_user
- main
extends:
- .deploy_

deploy prod:
variables:
DOCKER_TAG: $CI_COMMIT_TAG
WORKLOAD_NAME: eccoar-user
AMBIENTE: prod
APP_VERSION: $CI_COMMIT_TAG
CHART_NAME: eccoar-user
CHART_VERSION: $CI_COMMIT_TAG
only:
- tags
extends:
- .deploy_
23 changes: 23 additions & 0 deletions chart/eccoar-user/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
18 changes: 18 additions & 0 deletions chart/eccoar-user/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: v2
name: eccoar-user
description: A Helm chart for Kubernetes

type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "0.1.0"

icon: https://fga-eps-mds.github.io/2020.2-Eccoar/assets//img/logo.png
22 changes: 22 additions & 0 deletions chart/eccoar-user/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
1. Get the application URL by running these commands:
{{- if .Values.ingress.enabled }}
{{- range $host := .Values.ingress.hosts }}
{{- range .paths }}
http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }}
{{- end }}
{{- end }}
{{- else if contains "NodePort" .Values.service.type }}
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "eccoar-user.fullname" . }})
export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
{{- else if contains "LoadBalancer" .Values.service.type }}
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "eccoar-user.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "eccoar-user.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}")
echo http://$SERVICE_IP:{{ .Values.service.port }}
{{- else if contains "ClusterIP" .Values.service.type }}
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "eccoar-user.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT
{{- end }}
62 changes: 62 additions & 0 deletions chart/eccoar-user/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "eccoar-user.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).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "eccoar-user.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "eccoar-user.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "eccoar-user.labels" -}}
helm.sh/chart: {{ include "eccoar-user.chart" . }}
{{ include "eccoar-user.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "eccoar-user.selectorLabels" -}}
app.kubernetes.io/name: {{ include "eccoar-user.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "eccoar-user.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "eccoar-user.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
58 changes: 58 additions & 0 deletions chart/eccoar-user/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "eccoar-user.fullname" . }}
labels:
{{- include "eccoar-user.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
selector:
matchLabels:
{{- include "eccoar-user.selectorLabels" . | nindent 6 }}
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "eccoar-user.selectorLabels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 8 }}
containers:
- name: {{ .Chart.Name }}
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: http
containerPort: {{ .Values.app.app_port }}
protocol: TCP
env:
- name: APP_PORT
valueFrom:
configMapKeyRef:
name: eccoar-config
key: APP_PORT
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
Loading

0 comments on commit 34b3978

Please sign in to comment.