diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..762752c --- /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 0000000..747e6e9 --- /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/Jenkinsfile b/Jenkinsfile index 56b3354..4bad4c3 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,102 +1,79 @@ - import groovy.json.* - import groovy.json.JsonOutput - pipeline { - agent any - - environment { - VERSION = 'latest' - PROJECT = 'hello-world' - IMAGE = 'hello-world:latest' - ECRURL = 'https://856233045188.dkr.ecr.us-east-1.amazonaws.com/hello-world' - REGION='us-east-1' - REPOSITORY_NAME='hello-world' - CLUSTER='getting-started' - def FAMILY= sh ( - script: "sed -n \'s/.*\"family\": \"\\(.*\\)\",/\\1/p\' taskdef.json", - returnStdout: true - ).trim() - def NAME= sh ( - script: "sed -n \'s/.*\"name\": \"\\(.*\\)\",/\\1/p\' taskdef.json", - returnStdout: true - ).trim() - SERVICE_NAME="${NAME}-service-name" - - } - stages { - stage('Build preparations') { - steps { - script { - // calculate GIT lastest commit short-hash - gitCommitHash = sh(returnStdout: true, script: 'git rev-parse HEAD').trim() - shortCommitHash = gitCommitHash.take(7) - // calculate a sample version tag - VERSION = shortCommitHash - // set the build display name - currentBuild.displayName = "#${BUILD_ID}-${VERSION}" - IMAGE = "$PROJECT:$VERSION" - } - } - } - stage('Docker build') { - steps { - script { - //Get Login ECR - sh("eval \$(aws ecr get-login --no-include-email --region us-east-1 | sed 's|https://||')") - // Build the docker image using a Dockerfile - docker.build("$IMAGE") - } - } - } - stage('Push image to ECR') { - steps { - script { - docker.withRegistry(ECRURL) { - docker.image(IMAGE).push() - } - } - } - } - stage('Deploy to ECS') { - steps { - script { - //Store the repositoryUri as a variable - def REPOSITORY_URI = sh ( - script: "aws ecr describe-repositories --repository-names ${REPOSITORY_NAME} --region ${REGION} | jq .repositories[].repositoryUri | tr -d \'\"\'", - returnStdout: true - ).trim() - echo "REPOSITORY_URI: ${REPOSITORY_URI}" - //Replace the build number and respository URI placeholders with the constants above - sh "sed -e \"s;%BUILD_NUMBER%;${VERSION};g\" -e \"s;%REPOSITORY_URI%;${REPOSITORY_URI};g\" taskdef.json > ${NAME}-v_${BUILD_NUMBER}.json" - //Register the task definition in the repository - sh "aws ecs register-task-definition --family ${FAMILY} --cli-input-json file://${WORKSPACE}/${NAME}-v_${BUILD_NUMBER}.json --region ${REGION}" - def SERVICES= sh ( - script: "aws ecs describe-services --services ${SERVICE_NAME} --cluster ${CLUSTER} --region ${REGION} | jq .failures[]", - returnStdout: true - ).trim() - echo "SERVICES: ${SERVICES}" - //Get latest revision - def REVISION= sh ( - script: "aws ecs describe-task-definition --task-definition ${NAME} --region ${REGION} | jq .taskDefinition.revision", - returnStdout: true - ).trim() - echo "REVISION: ${REVISION}" - //Create or update service - if ( "${SERVICES}" == "" ) { - echo "entered existing service" - def DESIRED_COUNT= sh ( - script: "aws ecs describe-services --services ${SERVICE_NAME} --cluster ${CLUSTER} --region ${REGION} | jq .services[].desiredCount", - returnStdout: true - ).trim() - if ( "${DESIRED_COUNT}" == "0" ) { - DESIRED_COUNT="1" - } - sh "aws ecs update-service --cluster ${CLUSTER} --region ${REGION} --service ${SERVICE_NAME} --task-definition ${FAMILY}:${REVISION} --desired-count ${DESIRED_COUNT}" - } else { - echo "entered new service" - sh "aws ecs create-service --service-name ${SERVICE_NAME} --desired-count 1 --task-definition ${FAMILY} --cluster ${CLUSTER} --region ${REGION}" - } - } - } - } - } - } +pipeline { + agent { + label "jenkins-nodejs" + } + environment { + ORG = 'hoangmnsd' + APP_NAME = 'ecsjenkinsecrgithub' + 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('nodejs') { + sh "npm install" + sh "CI=true DISPLAY=:99 npm test" + 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('nodejs') { + + // 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 "jx step tag --version \$(cat VERSION)" + sh "npm install" + sh "CI=true DISPLAY=:99 npm test" + 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('nodejs') { + dir('./charts/ecsjenkinsecrgithub') { + sh "jx step changelog --batch-mode --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 0000000..c37c1b0 --- /dev/null +++ b/OWNERS @@ -0,0 +1,4 @@ +approvers: +- hoangmnsd +reviewers: +- hoangmnsd diff --git a/OWNERS_ALIASES b/OWNERS_ALIASES new file mode 100644 index 0000000..7ff3d04 --- /dev/null +++ b/OWNERS_ALIASES @@ -0,0 +1,6 @@ +aliases: +- hoangmnsd +best-approvers: +- hoangmnsd +best-reviewers: +- hoangmnsd diff --git a/charts/ecsjenkinsecrgithub/.helmignore b/charts/ecsjenkinsecrgithub/.helmignore new file mode 100755 index 0000000..f0c1319 --- /dev/null +++ b/charts/ecsjenkinsecrgithub/.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/ecsjenkinsecrgithub/Chart.yaml b/charts/ecsjenkinsecrgithub/Chart.yaml new file mode 100644 index 0000000..14e7ac4 --- /dev/null +++ b/charts/ecsjenkinsecrgithub/Chart.yaml @@ -0,0 +1,5 @@ +apiVersion: v1 +description: A Helm chart for Kubernetes +icon: https://raw.githubusercontent.com/jenkins-x/jenkins-x-platform/d273e09/images/nodejs.png +name: ecsjenkinsecrgithub +version: 0.1.0-SNAPSHOT diff --git a/charts/ecsjenkinsecrgithub/Makefile b/charts/ecsjenkinsecrgithub/Makefile new file mode 100755 index 0000000..d291180 --- /dev/null +++ b/charts/ecsjenkinsecrgithub/Makefile @@ -0,0 +1,48 @@ +CHART_REPO := http://jenkins-x-chartmuseum:8080 +CURRENT=$(pwd) +NAME := ecsjenkinsecrgithub +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)\/hoangmnsd\/ecsjenkinsecrgithub|" 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/ecsjenkinsecrgithub/README.md b/charts/ecsjenkinsecrgithub/README.md new file mode 100755 index 0000000..83de828 --- /dev/null +++ b/charts/ecsjenkinsecrgithub/README.md @@ -0,0 +1 @@ +# Javascript application \ No newline at end of file diff --git a/charts/ecsjenkinsecrgithub/templates/NOTES.txt b/charts/ecsjenkinsecrgithub/templates/NOTES.txt new file mode 100755 index 0000000..97823be --- /dev/null +++ b/charts/ecsjenkinsecrgithub/templates/NOTES.txt @@ -0,0 +1,4 @@ + +Get the application URL by running these commands: + +kubectl get ingress {{ template "fullname" . }} diff --git a/charts/ecsjenkinsecrgithub/templates/_helpers.tpl b/charts/ecsjenkinsecrgithub/templates/_helpers.tpl new file mode 100755 index 0000000..f0d83d2 --- /dev/null +++ b/charts/ecsjenkinsecrgithub/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/ecsjenkinsecrgithub/templates/deployment.yaml b/charts/ecsjenkinsecrgithub/templates/deployment.yaml new file mode 100755 index 0000000..a9b19a9 --- /dev/null +++ b/charts/ecsjenkinsecrgithub/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/ecsjenkinsecrgithub/templates/service.yaml b/charts/ecsjenkinsecrgithub/templates/service.yaml new file mode 100755 index 0000000..8d27389 --- /dev/null +++ b/charts/ecsjenkinsecrgithub/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/ecsjenkinsecrgithub/values.yaml b/charts/ecsjenkinsecrgithub/values.yaml new file mode 100755 index 0000000..cd0b069 --- /dev/null +++ b/charts/ecsjenkinsecrgithub/values.yaml @@ -0,0 +1,34 @@ +# Default values for node 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: ecsjenkinsecrgithub + type: ClusterIP + externalPort: 80 + internalPort: 8080 + annotations: + fabric8.io/expose: "true" + fabric8.io/ingress.annotations: "kubernetes.io/ingress.class: nginx" +resources: + limits: + cpu: 400m + memory: 256Mi + requests: + cpu: 200m + memory: 128Mi +probePath: / +livenessProbe: + initialDelaySeconds: 60 + periodSeconds: 10 + successThreshold: 1 + timeoutSeconds: 1 +readinessProbe: + periodSeconds: 10 + successThreshold: 1 + timeoutSeconds: 1 +terminationGracePeriodSeconds: 10 diff --git a/charts/preview/Chart.yaml b/charts/preview/Chart.yaml new file mode 100644 index 0000000..6b68431 --- /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 0000000..dcee8f1 --- /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)\/hoangmnsd\/ecsjenkinsecrgithub|" 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 0000000..35d16f0 --- /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.92 +- alias: cleanup + name: exposecontroller + repository: http://chartmuseum.jenkins-x.io + version: 2.3.92 + + # !! "alias: preview" must be last entry in dependencies array !! + # !! Place custom dependencies above !! +- alias: preview + name: ecsjenkinsecrgithub + repository: file://../ecsjenkinsecrgithub diff --git a/charts/preview/values.yaml b/charts/preview/values.yaml new file mode 100755 index 0000000..b53ceaa --- /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 0000000..e428cb2 --- /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}}/hoangmnsd/ecsjenkinsecrgithub:{{.VERSION}}' + local: {} +deploy: + kubectl: {} +profiles: +- name: dev + build: + artifacts: + - docker: {} + tagPolicy: + envTemplate: + template: '{{.DOCKER_REGISTRY}}/hoangmnsd/ecsjenkinsecrgithub:{{.DIGEST_HEX}}' + local: {} + deploy: + helm: + releases: + - name: ecsjenkinsecrgithub + chartPath: charts/ecsjenkinsecrgithub + setValueTemplates: + image.repository: '{{.DOCKER_REGISTRY}}/hoangmnsd/ecsjenkinsecrgithub' + image.tag: '{{.DIGEST_HEX}}'