diff --git a/.k8s/deployment.yaml b/.k8s/deployment.yaml index a231a4c..e224008 100644 --- a/.k8s/deployment.yaml +++ b/.k8s/deployment.yaml @@ -1,37 +1,40 @@ +--- apiVersion: apps/v1 -kind: Deployment +kind: Deployment # Kubernetes resource kind we are creating metadata: - name: nodeapp-deployment - labels: - app: nodeapp - type: front-end + name: cicd-java-spring-boot-deployment + namespace: prototype-loafle-net spec: - template: - metadata: - name: nodejsapp-pod - labels: - app: nodejsapp - type: front-end - spec: - containers: - - name: nodejsapp-erp - image: "bharathirajatut/erp:1.0" - replicas: 1 selector: matchLabels: - type: front-end + app: cicd-java-spring-boot-app + replicas: 1 # Number of replicas that will be created for this deployment + template: + metadata: + labels: + app: cicd-java-spring-boot-app + spec: + containers: + - name: cicd-java-spring-boot-app + image: __REGISTRY_URL__/__IMAGE_NAME__:__IMAGE_TAG__ # Image that will be used to containers in the cluster + imagePullPolicy: Always + ports: + - containerPort: 8080 + env: + - name: DEPLOY_DATE + value: '__DEPLOY_DATE__' --- -apiVersion: v1 -kind: Service -metadata: - name: nodejs-service +apiVersion: v1 # Kubernetes API version +kind: Service # Kubernetes resource kind we are creating +metadata: # Metadata of the resource kind we are creating + name: cicd-java-spring-boot-service + namespace: prototype-loafle-net spec: selector: - app: nodejsapp - type: LoadBalancer + app: cicd-java-spring-boot-app ports: - - protocol: TCP - targetPort: 3000 - port: 80 - nodePort: 30000 \ No newline at end of file + - protocol: "TCP" + port: 8080 # The port that the service is running on in the cluster + targetPort: 8080 # The port exposed by the service + \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile index 56dc00d..8878a6c 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -77,13 +77,33 @@ podTemplate( credentialsId: 'harbor.loafle.net', usernameVariable: 'HARBOR_USER', passwordVariable: 'HARBOR_PASSWORD']]) { + env.CONTAINER_IMAGE_NAME = prototype/${env.PROJECT_NAME} sh """ docker login -u "${HARBOR_USER}" -p "${HARBOR_PASSWORD}" ${dockerRegistryUrl} - docker build -t ${dockerRegistryUrl}/prototype/${env.PROJECT_NAME}:${env.PROJECT_VERSION} --build-arg PROJECT_NAME=${env.PROJECT_NAME} --build-arg PROJECT_VERSION=${env.PROJECT_VERSION} . - docker push ${dockerRegistryUrl}/prototype/${env.PROJECT_NAME}:${env.PROJECT_VERSION} + docker build -t ${dockerRegistryUrl}/${env.CONTAINER_IMAGE_NAME}:${env.PROJECT_VERSION} --build-arg PROJECT_NAME=${env.PROJECT_NAME} --build-arg PROJECT_VERSION=${env.PROJECT_VERSION} . + docker push ${dockerRegistryUrl}/${env.CONTAINER_IMAGE_NAME}:${env.PROJECT_VERSION} """ } } } + + stage('Deploy Application on K8s') { + container('kubectl'){ + withKubeConfig([ + credentialsId: 'microk8s-cluster', + serverUrl: env.K8s_SERVER_URL, + contextName: env.K8s_CONTEXT_NAME, + clusterName: env.K8s_CLUSTER_NAME]){ + sh """ + DEPLOY_DATE=$(date +"%Y-%m-%d %H:%M:%S") + sed -i.bak 's#__REGISTRY_URL__#'"${dockerRegistryUrl}"'#' ./.k8s/deployment.yaml + sed -i.bak 's#__IMAGE_NAME__#'"${env.CONTAINER_IMAGE_NAME}"'#' ./.k8s/deployment.yaml + sed -i.bak 's#__IMAGE_TAG__#'"${env.PROJECT_VERSION}"'#' ./.k8s/deployment.yaml + sed -i.bak 's#__DEPLOY_DATE__#'"${DEPLOY_DATE}"'#' ./.k8s/deployment.yaml + kubectl apply -f ./.k8s/deployment.yaml + """ + } + } + } } }