diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 90132e3..f897b31 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -4,6 +4,10 @@ on: push: branches: - main + pull_request: + branches: + - main + workflow_dispatch: jobs: sonarcloud: @@ -47,13 +51,9 @@ jobs: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_PASSWORD }} - - name: Set current timestamp - id: timestamp - run: echo "TIMESTAMP=$(date +%s)" >> $GITHUB_ENV - - name: Build and push uses: docker/build-push-action@v3 with: context: . push: true - tags: amits64/crud-app:${{ env.TIMESTAMP }} + tags: amits64/crud-app:${{ github.run_number }} diff --git a/Jenkinsfile b/Jenkinsfile index e70a4a6..0b40872 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -3,44 +3,41 @@ pipeline { agent any + parameters { + choice(name: 'NAMESPACE', choices: ['cde', 'ncde'], description: 'Select Namespace') + choice(name: 'ENVIRONMENT', choices: ['dev', 'qa', 'prod'], description: 'Select the deployment environment') + string(name: 'IMAGE_TAG', defaultValue: '', description: 'Docker image tag') + } + environment { registry = 'amits64' registryCredential = 'dockerhub' image = 'crud-app' - tag = "v${BUILD_NUMBER}" - sonarHostUrl = 'http://192.168.2.20:9000/' - repoUrl = 'https://github.com/Amits64/crud-app.git' // Add your repository URL here + tag = "${params.IMAGE_TAG}" // Use IMAGE_TAG parameter + kubeConfigPath = "/etc/kubernetes/${params.ENVIRONMENT}/config" // Adjusted for dynamic environment } stages { - stage('CI') { - steps { - // Call the shared library function for CI - ciPipeline( - registry: registry, - registryCredential: registryCredential, - image: image, - tag: tag, - sonarHostUrl: sonarHostUrl, - repoUrl: repoUrl - ) - } - } - stage('Deploying Container to Kubernetes') { steps { script { - dir('crud-app') { - // Debugging step to print Helm version - sh 'helm version' + sh 'helm version' - def releaseExists = sh(returnStatus: true, script: 'helm ls | grep -q ${image}') == 0 - if (releaseExists) { - sh 'helm delete ${image}' - } + def releaseExists = sh(returnStatus: true, script: "helm ls --kubeconfig ${kubeConfigPath} | grep -q ${image}") == 0 - sh "helm install ${image} ./ --set appimage=${registry}/${image}:${tag} --set-file ca.crt=/etc/ca-certificates/update.d/jks-keystore" + if (releaseExists) { + echo "Existing Helm release found. Deleting release: ${image}" + sh "helm delete ${image} --kubeconfig ${kubeConfigPath}" + } else { + echo "No existing Helm release found for ${image}. Proceeding with installation." } + + sh """ + helm upgrade --install ${image} ./ \ + --kubeconfig ${kubeConfigPath} \ + --set appimage=${registry}/${image}:${tag} \ + --namespace ${params.NAMESPACE} + """ } } } @@ -51,6 +48,8 @@ pipeline { script { if (currentBuild.result != 'SUCCESS') { error("Container deployment failed!") + } else { + echo "Container deployment succeeded!" } } } diff --git a/crud-app/values-dev.yaml b/crud-app/values-dev.yaml new file mode 100644 index 0000000..9ad7131 --- /dev/null +++ b/crud-app/values-dev.yaml @@ -0,0 +1,83 @@ +# Default values for crud-app in the dev environment. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 # Typically lower for development environments + +image: + repository: amits64/crud-app + pullPolicy: IfNotPresent + tag: "dev-${BUILD_NUMBER}" # Use a specific tag for dev + +imagePullSecrets: + - name: my-registry-secret # Optional, if you use private registry + +nameOverride: "crud-app-dev" +fullnameOverride: "crud-app-dev" + +serviceAccount: + create: true + annotations: {} + name: "crud-app-dev-sa" # Specific service account for dev + +podAnnotations: + kubernetes.io/cluster-service: "true" + +podSecurityContext: + fsGroup: 2000 + +securityContext: + capabilities: + drop: + - ALL + readOnlyRootFilesystem: false + runAsNonRoot: false + runAsUser: 1000 + +service: + type: ClusterIP + port: 80 + +ingress: + enabled: false # Ingress typically not used in dev + className: "" + annotations: {} + hosts: + - host: dev.chart-example.local + paths: + - path: / + pathType: ImplementationSpecific + tls: [] + +resources: + limits: + cpu: 100m + memory: 128Mi + requests: + cpu: 50m + memory: 64Mi + +autoscaling: + enabled: false # Autoscaling usually disabled in dev + minReplicas: 1 + maxReplicas: 2 + targetCPUUtilizationPercentage: 80 + +nodeSelector: + environment: dev + +tolerations: + - key: "environment" + operator: "Equal" + value: "dev" + effect: "NoSchedule" + +affinity: + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: "kubernetes.io/role" + operator: "In" + values: + - "dev" diff --git a/crud-app/values-qa.yaml b/crud-app/values-qa.yaml new file mode 100644 index 0000000..253d590 --- /dev/null +++ b/crud-app/values-qa.yaml @@ -0,0 +1,83 @@ +# Default values for crud-app in the qa environment. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 # Typically lower for development environments + +image: + repository: amits64/crud-app + pullPolicy: IfNotPresent + tag: "qa-${BUILD_NUMBER}" # Use a specific tag for qa + +imagePullSecrets: + - name: my-registry-secret # Optional, if you use private registry + +nameOverride: "crud-app-qa" +fullnameOverride: "crud-app-qa" + +serviceAccount: + create: true + annotations: {} + name: "crud-app-qa-sa" # Specific service account for qa + +podAnnotations: + kubernetes.io/cluster-service: "true" + +podSecurityContext: + fsGroup: 2000 + +securityContext: + capabilities: + drop: + - ALL + readOnlyRootFilesystem: false + runAsNonRoot: false + runAsUser: 1000 + +service: + type: ClusterIP + port: 80 + +ingress: + enabled: false # Ingress typically not used in qa + className: "" + annotations: {} + hosts: + - host: qa.chart-example.local + paths: + - path: / + pathType: ImplementationSpecific + tls: [] + +resources: + limits: + cpu: 100m + memory: 128Mi + requests: + cpu: 50m + memory: 64Mi + +autoscaling: + enabled: false # Autoscaling usually disabled in qa + minReplicas: 1 + maxReplicas: 2 + targetCPUUtilizationPercentage: 80 + +nodeSelector: + environment: qa + +tolerations: + - key: "environment" + operator: "Equal" + value: "qa" + effect: "NoSchedule" + +affinity: + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: "kubernetes.io/role" + operator: "In" + values: + - "qa" diff --git a/crud-app/values.prod.yaml b/crud-app/values.prod.yaml new file mode 100644 index 0000000..5b70aa1 --- /dev/null +++ b/crud-app/values.prod.yaml @@ -0,0 +1,83 @@ +# Default values for crud-app in the prod environment. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 # Typically lower for development environments + +image: + repository: amits64/crud-app + pullPolicy: IfNotPresent + tag: "prod-${BUILD_NUMBER}" # Use a specific tag for prod + +imagePullSecrets: + - name: my-registry-secret # Optional, if you use private registry + +nameOverride: "crud-app-prod" +fullnameOverride: "crud-app-prod" + +serviceAccount: + create: true + annotations: {} + name: "crud-app-prod-sa" # Specific service account for prod + +podAnnotations: + kubernetes.io/cluster-service: "true" + +podSecurityContext: + fsGroup: 2000 + +securityContext: + capabilities: + drop: + - ALL + readOnlyRootFilesystem: false + runAsNonRoot: false + runAsUser: 1000 + +service: + type: ClusterIP + port: 80 + +ingress: + enabled: false # Ingress typically not used in prod + className: "" + annotations: {} + hosts: + - host: prod.chart-example.local + paths: + - path: / + pathType: ImplementationSpecific + tls: [] + +resources: + limits: + cpu: 100m + memory: 128Mi + requests: + cpu: 50m + memory: 64Mi + +autoscaling: + enabled: false # Autoscaling usually disabled in prod + minReplicas: 1 + maxReplicas: 2 + targetCPUUtilizationPercentage: 80 + +nodeSelector: + environment: prod + +tolerations: + - key: "environment" + operator: "Equal" + value: "prod" + effect: "NoSchedule" + +affinity: + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: "kubernetes.io/role" + operator: "In" + values: + - "prod" diff --git a/crud-app/values.yaml b/crud-app/values.yaml deleted file mode 100644 index 21bb5b5..0000000 --- a/crud-app/values.yaml +++ /dev/null @@ -1,82 +0,0 @@ -# Default values for crud-app. -# This is a YAML-formatted file. -# Declare variables to be passed into your templates. - -replicaCount: 1 - -image: - repository: amits64/crud-app - pullPolicy: IfNotPresent - # Overrides the image tag whose default is the chart appVersion. - tag: "" - -imagePullSecrets: [] -nameOverride: "" -fullnameOverride: "" - -serviceAccount: - # Specifies whether a service account should be created - create: true - # Annotations to add to the service account - annotations: {} - # The name of the service account to use. - # If not set and create is true, a name is generated using the fullname template - name: "" - -podAnnotations: {} - -podSecurityContext: {} - # fsGroup: 2000 - -securityContext: {} - # capabilities: - # drop: - # - ALL - # readOnlyRootFilesystem: true - # runAsNonRoot: true - # runAsUser: 1000 - -service: - type: ClusterIP - port: 80 - -ingress: - enabled: false - className: "" - annotations: {} - # kubernetes.io/ingress.class: nginx - # kubernetes.io/tls-acme: "true" - hosts: - - host: chart-example.local - paths: - - path: / - pathType: ImplementationSpecific - tls: [] - # - secretName: chart-example-tls - # hosts: - # - chart-example.local - -resources: {} - # We usually recommend not to specify default resources and to leave this as a conscious - # choice for the user. This also increases chances charts run on environments with little - # resources, such as Minikube. If you do want to specify resources, uncomment the following - # lines, adjust them as necessary, and remove the curly braces after 'resources:'. - # limits: - # cpu: 100m - # memory: 128Mi - # requests: - # cpu: 100m - # memory: 128Mi - -autoscaling: - enabled: false - minReplicas: 1 - maxReplicas: 100 - targetCPUUtilizationPercentage: 80 - # targetMemoryUtilizationPercentage: 80 - -nodeSelector: {} - -tolerations: [] - -affinity: {}