diff --git a/.gitignore b/.gitignore index b7bac7f..4f2818f 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ plato .DS_Store *.sw* launch.json -dist \ No newline at end of file +dist +target \ No newline at end of file diff --git a/.npmrc b/.npmrc deleted file mode 100644 index c6ad20f..0000000 --- a/.npmrc +++ /dev/null @@ -1 +0,0 @@ -registry = "http://nexus3-misanche-nexus.apps.na39.openshift.opentlc.com/repository/npm_group" diff --git a/assembly/configuration.xml b/assembly/configuration.xml new file mode 100644 index 0000000..32249a7 --- /dev/null +++ b/assembly/configuration.xml @@ -0,0 +1,21 @@ + + package + + zip + + true + + + ${project.basedir} + / + + src/** + test/** + dist/** + package.json + package-lock.json + + + + \ No newline at end of file diff --git a/openshift/pipeline/01-pvc.yml b/openshift/pipeline/01-pvc.yml new file mode 100644 index 0000000..2f2e53a --- /dev/null +++ b/openshift/pipeline/01-pvc.yml @@ -0,0 +1,11 @@ +apiVersion: "v1" +kind: "PersistentVolumeClaim" +metadata: + name: "git-claim" +spec: + accessModes: + - "ReadWriteOnce" + resources: + requests: + storage: "20Gi" + volumeName: "pv0002" \ No newline at end of file diff --git a/openshift/pipeline/02-serviceaccount.yml b/openshift/pipeline/02-serviceaccount.yml new file mode 100644 index 0000000..749e03b --- /dev/null +++ b/openshift/pipeline/02-serviceaccount.yml @@ -0,0 +1,51 @@ +apiVersion: v1 +kind: ServiceAccount +metadata: + name: tekton-sa + +--- + +kind: Role +apiVersion: rbac.authorization.k8s.io/v1 +metadata: + name: pipeline-role +rules: +# Permissions for every EventListener deployment to function +- apiGroups: ["triggers.tekton.dev"] + resources: ["eventlisteners", "triggerbindings", "triggertemplates"] + verbs: ["get"] +# Permissions to create resources in associated TriggerTemplates +- apiGroups: ["tekton.dev"] + resources: ["pipelineruns", "pipelineresources", "taskruns"] + verbs: ["create"] +- apiGroups: ["extensions", "apps", ""] + resources: ["services", "deployments", "pods", "configmaps", "secrets", "serviceaccounts"] + verbs: ["get", "create", "update", "patch", "list", "delete", "watch"] + +--- + +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + name: pipeline-role-binding +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: Role + name: pipeline-role +subjects: +- kind: ServiceAccount + name: tekton-sa + +--- + +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + name: registry-editor +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: registry-editor +subjects: +- kind: ServiceAccount + name: tekton-sa \ No newline at end of file diff --git a/openshift/pipeline/03-triggerbinding.yml b/openshift/pipeline/03-triggerbinding.yml new file mode 100644 index 0000000..c7e4726 --- /dev/null +++ b/openshift/pipeline/03-triggerbinding.yml @@ -0,0 +1,12 @@ +apiVersion: tekton.dev/v1alpha1 +kind: TriggerBinding +metadata: + name: nodejs-template-binding +spec: + params: + - name: git-revision + value: $(body.head_commit.id) + - name: git-repo-url + value: $(body.repository.url) + - name: git-repo-name + value: $(body.repository.name) \ No newline at end of file diff --git a/openshift/pipeline/04-eventlistener.yml b/openshift/pipeline/04-eventlistener.yml new file mode 100644 index 0000000..3390e15 --- /dev/null +++ b/openshift/pipeline/04-eventlistener.yml @@ -0,0 +1,30 @@ +apiVersion: v1 +data: + secret: dGVzdGhvb2s= +kind: Secret +metadata: + name: nodejs-github-hook-secret +type: Opaque + +--- + +apiVersion: tekton.dev/v1alpha1 +kind: EventListener +metadata: + name: nodejs-template-listener-interceptor +spec: + serviceAccountName: tekton-sa + triggers: + - name: nodejs-github-trigger + interceptors: + - github: + secretRef: + secretName: nodejs-github-hook-secret + secretKey: secret + eventTypes: + - pull_request + - push + bindings: + - name: nodejs-template-binding + template: + name: nodejs-template \ No newline at end of file diff --git a/openshift/pipeline/05-task.yml b/openshift/pipeline/05-task.yml new file mode 100644 index 0000000..45e2dd6 --- /dev/null +++ b/openshift/pipeline/05-task.yml @@ -0,0 +1,26 @@ +apiVersion: tekton.dev/v1alpha1 +kind: Task +metadata: + name: build-task +spec: + inputs: + resources: + - name: source + type: git + steps: + - name: install + image: node:12 + command: + - /bin/sh + - -c + args: + - npm install + workingDir: /workspace/source + - name: list + image: node:12 + command: + - /bin/sh + - -c + args: + - ls + workingDir: /workspace/source \ No newline at end of file diff --git a/openshift/pipeline/06-pipeline.yml b/openshift/pipeline/06-pipeline.yml new file mode 100644 index 0000000..1a49e96 --- /dev/null +++ b/openshift/pipeline/06-pipeline.yml @@ -0,0 +1,20 @@ +apiVersion: tekton.dev/v1alpha1 +kind: Pipeline +metadata: + name: build +spec: + resources: + - name: git-repo + type: git + params: + - name: deployment-name + type: string + description: name of the deployment to be patched + tasks: + - name: build-task + taskRef: + name: build-task + resources: + inputs: + - name: source + resource: git-repo diff --git a/openshift/pipeline/07-triggertemplate.yaml b/openshift/pipeline/07-triggertemplate.yaml new file mode 100644 index 0000000..999ca7b --- /dev/null +++ b/openshift/pipeline/07-triggertemplate.yaml @@ -0,0 +1,40 @@ +apiVersion: tekton.dev/v1alpha1 +kind: TriggerTemplate +metadata: + name: nodejs-template +spec: + params: + - name: git-repo-url + description: The git repository url + - name: git-revision + description: The git revision + default: master + - name: git-repo-name + description: The name of the deployment to be created / patched + resourcetemplates: + - apiVersion: tekton.dev/v1alpha1 + kind: PipelineResource + metadata: + name: $(params.git-repo-name)-git-repo-$(uid) + spec: + type: git + params: + - name: revision + value: $(params.git-revision) + - name: url + value: $(params.git-repo-url) + - apiVersion: tekton.dev/v1alpha1 + kind: PipelineRun + metadata: + name: build-$(params.git-repo-name)-$(uid) + spec: + serviceAccountName: tekton-sa + pipelineRef: + name: build + resources: + - name: git-repo + resourceRef: + name: $(params.git-repo-name)-git-repo-$(uid) + params: + - name: deployment-name + value: $(params.git-repo-name) \ No newline at end of file diff --git a/openshift/pipeline/JenkinsFile b/openshift/pipeline/JenkinsFile deleted file mode 100644 index 86976f6..0000000 --- a/openshift/pipeline/JenkinsFile +++ /dev/null @@ -1,11 +0,0 @@ -node('nodejs') { -stage 'build' -openshiftBuild(namespace: 'prima-poc' ,buildConfig: 'nodejssample', showBuildLogs: 'true') -stage 'deploy' -openshiftDeploy(namespace: 'prima-poc',deploymentConfig: 'nodejssample') -openshiftScale(namespace: 'prima-poc',deploymentConfig: 'nodejssample',replicaCount: '1') -stage 'deployInTesting' -openshiftTag(namespace: 'prima-poc', sourceStream: 'nodejssample', sourceTag: 'latest', destinationStream: 'nodejssample', destinationTag: 'promoteToQA') -openshiftDeploy(namespace: 'test-project', deploymentConfig: 'nodejssample', ) -openshiftScale(namespace: 'test-project', deploymentConfig: 'nodejssample',replicaCount: '1') -} \ No newline at end of file diff --git a/openshift/pipeline/Readme.md b/openshift/pipeline/Readme.md deleted file mode 100644 index 4b6d4bb..0000000 --- a/openshift/pipeline/Readme.md +++ /dev/null @@ -1,13 +0,0 @@ -This directory contains a Jenkinsfile which can be used to build -nodejsApp using an OpenShift build pipeline. - -To do this, run: - -```bash - -# Pipeline should be create under jenkins project -# create the pipeline build controller from the openshift/pipeline -# subdirectory -oc project <> -oc new-app < \ - --context-dir=openshift/pipeline --name <> \ No newline at end of file diff --git a/openshift/pipeline/pipelineresource.yml b/openshift/pipeline/pipelineresource.yml new file mode 100644 index 0000000..986ce9e --- /dev/null +++ b/openshift/pipeline/pipelineresource.yml @@ -0,0 +1,11 @@ +apiVersion: tekton.dev/v1alpha1 +kind: PipelineResource +metadata: + name: nodejs-template-git-master +spec: + type: git + params: + - name: revision + value: tekton + - name: url + value: https://github.com/rhappdev/nodejs-template.git \ No newline at end of file diff --git a/openshift/templates/nodejs-app.json b/openshift/templates/nodejs-app.json deleted file mode 100644 index bf30e0f..0000000 --- a/openshift/templates/nodejs-app.json +++ /dev/null @@ -1,291 +0,0 @@ -{ - "kind": "Template", - "apiVersion": "v1", - "metadata": { - "name": "nodejs-app", - "annotations": { - "openshift.io/display-name": "Node.js", - "description": "An example Node.js application with no database. For more information about using this template, including OpenShift considerations, see https://github.com/openshift/nodejs-ex/blob/master/README.md.", - "tags": "quickstart,nodejs", - "iconClass": "icon-nodejs", - "openshift.io/long-description": "This template defines resources needed to develop a NodeJS application, including a build configuration and application deployment configuration. It does not include a database.", - "openshift.io/provider-display-name": "Red Hat, Inc.", - "openshift.io/documentation-url": "https://github.com/openshift/nodejs-ex", - "openshift.io/support-url": "https://access.redhat.com" - } - }, - "message": "The following service(s) have been created in your project: ${NAME}.\n\nFor more information about using this template, including OpenShift considerations, see https://github.com/openshift/nodejs-ex/blob/master/README.md.", - "labels": { - "template": "nodejs-example" - }, - "objects": [ - { - "kind": "Service", - "apiVersion": "v1", - "metadata": { - "name": "${NAME}", - "annotations": { - "description": "Exposes and load balances the application pods" - } - }, - "spec": { - "ports": [ - { - "name": "web", - "port": 8080, - "targetPort": 8080 - } - ], - "selector": { - "name": "${NAME}" - } - } - }, - { - "kind": "Route", - "apiVersion": "v1", - "metadata": { - "name": "${NAME}", - "annotations": { - "template.openshift.io/expose-uri": "http://{.spec.host}{.spec.path}" - } - }, - "spec": { - "host": "${APPLICATION_DOMAIN}", - "to": { - "kind": "Service", - "name": "${NAME}" - } - } - }, - { - "kind": "ImageStream", - "apiVersion": "v1", - "metadata": { - "name": "${NAME}", - "annotations": { - "description": "Keeps track of changes in the application image" - } - } - }, - { - "kind": "BuildConfig", - "apiVersion": "v1", - "metadata": { - "name": "${NAME}", - "annotations": { - "description": "Defines how to build the application", - "template.alpha.openshift.io/wait-for-ready": "true" - } - }, - "spec": { - "source": { - "type": "Git", - "git": { - "uri": "${SOURCE_REPOSITORY_URL}", - "ref": "${SOURCE_REPOSITORY_REF}" - }, - "contextDir": "${CONTEXT_DIR}" - }, - "strategy": { - "type": "Source", - "sourceStrategy": { - "from": { - "kind": "ImageStreamTag", - "namespace": "${NAMESPACE}", - "name": "nodejs:6" - }, - "env": [ - { - "name": "NPM_MIRROR", - "value": "${NPM_MIRROR}" - } - ] - } - }, - "output": { - "to": { - "kind": "ImageStreamTag", - "name": "${NAME}:latest" - } - }, - "triggers": [ - { - "type": "ImageChange" - }, - { - "type": "ConfigChange" - }, - { - "type": "GitHub", - "github": { - "secret": "${GITHUB_WEBHOOK_SECRET}" - } - }, - { - "type": "Generic", - "generic": { - "secret": "${GENERIC_WEBHOOK_SECRET}" - } - } - ], - "postCommit": { - "script": "npm test" - } - } - }, - { - "kind": "DeploymentConfig", - "apiVersion": "v1", - "metadata": { - "name": "${NAME}", - "annotations": { - "description": "Defines how to deploy the application server", - "template.alpha.openshift.io/wait-for-ready": "true" - } - }, - "spec": { - "strategy": { - "type": "Rolling" - }, - "triggers": [ - { - "type": "ImageChange", - "imageChangeParams": { - "automatic": true, - "containerNames": [ - "nodejs-example" - ], - "from": { - "kind": "ImageStreamTag", - "name": "${NAME}:latest" - } - } - }, - { - "type": "ConfigChange" - } - ], - "replicas": 1, - "selector": { - "name": "${NAME}" - }, - "template": { - "metadata": { - "name": "${NAME}", - "labels": { - "name": "${NAME}" - } - }, - "spec": { - "containers": [ - { - "name": "nodejs-app", - "image": " ", - "ports": [ - { - "containerPort": 8080 - } - ], - "readinessProbe": { - "timeoutSeconds": 3, - "initialDelaySeconds": 3, - "httpGet": { - "path": "/", - "port": 8080 - } - }, - "livenessProbe": { - "timeoutSeconds": 3, - "initialDelaySeconds": 30, - "httpGet": { - "path": "/", - "port": 8080 - } - }, - "resources": { - "limits": { - "memory": "${MEMORY_LIMIT}" - } - }, - "env": [ - ], - "resources": { - "limits": { - "memory": "${MEMORY_LIMIT}" - } - } - } - ] - } - } - } - } - ], - "parameters": [ - { - "name": "NAME", - "displayName": "Name", - "description": "The name assigned to all of the frontend objects defined in this template.", - "required": true, - "value": "nodejs-example" - }, - { - "name": "NAMESPACE", - "displayName": "Namespace", - "description": "The OpenShift Namespace where the ImageStream resides.", - "required": true, - "value": "openshift" - }, - { - "name": "MEMORY_LIMIT", - "displayName": "Memory Limit", - "description": "Maximum amount of memory the container can use.", - "required": true, - "value": "512Mi" - }, - { - "name": "SOURCE_REPOSITORY_URL", - "displayName": "Git Repository URL", - "description": "The URL of the repository with your application source code.", - "required": true, - "value": "https://github.com/openshift/nodejs-ex.git" - }, - { - "name": "SOURCE_REPOSITORY_REF", - "displayName": "Git Reference", - "description": "Set this to a branch name, tag or other ref of your repository if you are not using the default branch." - }, - { - "name": "CONTEXT_DIR", - "displayName": "Context Directory", - "description": "Set this to the relative path to your project if it is not in the root of your repository." - }, - { - "name": "APPLICATION_DOMAIN", - "displayName": "Application Hostname", - "description": "The exposed hostname that will route to the Node.js service, if left blank a value will be defaulted.", - "value": "" - }, - { - "name": "GITHUB_WEBHOOK_SECRET", - "displayName": "GitHub Webhook Secret", - "description": "Github trigger secret. A difficult to guess string encoded as part of the webhook URL. Not encrypted.", - "generate": "expression", - "from": "[a-zA-Z0-9]{40}" - }, - { - "name": "GENERIC_WEBHOOK_SECRET", - "displayName": "Generic Webhook Secret", - "description": "A secret string used to configure the Generic webhook.", - "generate": "expression", - "from": "[a-zA-Z0-9]{40}" - }, - { - "name": "NPM_MIRROR", - "displayName": "Custom NPM Mirror URL", - "description": "The custom NPM mirror URL", - "value": "" - } - ] - } \ No newline at end of file diff --git a/openshift/templates/nodejs-template.yml b/openshift/templates/nodejs-template.yml new file mode 100644 index 0000000..50be6d1 --- /dev/null +++ b/openshift/templates/nodejs-template.yml @@ -0,0 +1,115 @@ + +apiVersion: v1 +kind: Template +metadata: + name: nodejs-template + annotations: + description: "Description" + iconClass: "icon-nodejs" + tags: "nodejs,microservice" +objects: +- apiVersion: v1 + kind: Service + metadata: + annotations: + labels: + app: ${APP_NAME} + name: ${APP_NAME} + spec: + ports: + - name: 8080-tcp + port: 8080 + protocol: TCP + targetPort: 8080 + selector: + app: ${APP_NAME} + deploymentconfig: ${APP_NAME} + sessionAffinity: None + type: ClusterIP +- apiVersion: apps.openshift.io/v1 + kind: DeploymentConfig + metadata: + annotations: + labels: + app: ${APP_NAME} + name: ${APP_NAME} + spec: + replicas: 1 + revisionHistoryLimit: 10 + selector: + app: ${APP_NAME} + deploymentconfig: ${APP_NAME} + strategy: + activeDeadlineSeconds: 21600 + resources: {} + rollingParams: + intervalSeconds: 1 + maxSurge: 25% + maxUnavailable: 25% + timeoutSeconds: 600 + updatePeriodSeconds: 1 + type: Rolling + template: + metadata: + labels: + app: ${APP_NAME} + deploymentconfig: ${APP_NAME} + spec: + containers: + - image: ${APP_NAME}:latest + imagePullPolicy: Always + name: ${APP_NAME} + ports: + - containerPort: 8080 + protocol: TCP + resources: {} + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + dnsPolicy: ClusterFirst + restartPolicy: Always + schedulerName: default-scheduler + securityContext: {} + terminationGracePeriodSeconds: 30 + test: false + triggers: + - imageChangeParams: + automatic: true + containerNames: + - ${APP_NAME} + from: + kind: ImageStreamTag + name: ${APP_NAME}:latest + type: ImageChange + - type: ConfigChange +- apiVersion: image.openshift.io/v1 + kind: ImageStream + metadata: + annotations: + labels: + app: ${APP_NAME} + name: ${APP_NAME} + spec: + lookupPolicy: + local: false +- apiVersion: route.openshift.io/v1 + kind: Route + metadata: + annotations: + labels: + app: ${APP_NAME} + name: ${APP_NAME} + spec: + host: + port: + targetPort: 8080-tcp + to: + kind: Service + name: ${APP_NAME} + weight: 100 + wildcardPolicy: None +parameters: + - name: APP_NAME + displayName: Application Name + description: The name of the application + value: nodejs + required: true diff --git a/package.json b/package.json index fbf31c9..43fa538 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,6 @@ "version": "1.0.0", "description": "", "main": "dist/src/index.js", - "publishConfig": { - "registry": "http://nexus3-misanche-nexus.apps.na39.openshift.opentlc.com/repository/npm_internal/" - }, "dependencies": { "bluebird": "^3.5.1", "cls-hooked": "^4.2.2", @@ -54,7 +51,8 @@ "lint": "tslint -p tsconfig.json", "updateSwagger": "node scripts/updateSwagger.js", "updateAll": "node scripts/updateCode.js", - "testOne": "TS_NODE_CACHE=false ./node_modules/.bin/mocha --compilers ts:ts-node/register " + "testOne": "TS_NODE_CACHE=false ./node_modules/.bin/mocha --compilers ts:ts-node/register", + "postinstall": "npm run clean && npm run lint && tsc -p tsconfig.json && npm run build:copy" }, "engines": { "node": ">6.11.0" diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..75a392c --- /dev/null +++ b/pom.xml @@ -0,0 +1,36 @@ + + 4.0.0 + + com.redhat + nodejs-template + pom + 1.0.0-SNAPSHOT + nodejs-template + NodeJS template + + + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.0.0 + + + make-assembly + package + + single + + + + assembly/configuration.xml + + true + + + + + + +