GitLab CI/CD pipeline configuration for deploying a Node.js application using Helm charts to a GKE cluster

(.gitlab-ci.yml) for deploying a Node.js application

stages:
  - build
  - deploy

variables:
  # Define your variables like GKE_CLUSTER_NAME, GCP_PROJECT_ID, etc.
  GKE_CLUSTER_NAME: "your-gke-cluster"
  GCP_PROJECT_ID: "your-gcp-project"
  HELM_CHART_PATH: "path/to/your/helm/chart"

before_script:
  - # Set up any prerequisites or dependencies here

build:
  stage: build
  script:
    - # Add commands to build and test your Node.js application

deploy:
  stage: deploy
  script:
    - # Install Helm
    - curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3
    - chmod 700 get_helm.sh
    - ./get_helm.sh

    - # Authenticate with GKE
    - gcloud auth activate-service-account --key-file=service-account.json
    - gcloud container clusters get-credentials $GKE_CLUSTER_NAME --region=your-gke-region --project=$GCP_PROJECT_ID

    - # Deploy Helm chart
    - cd $HELM_CHART_PATH
    - helm upgrade --install your-app-release .

  only:
    - master

Remember to replace placeholder values such as your-gke-cluster, your-gcp-project, and adjust the paths accordingly. Also, ensure you have the necessary configurations like service-account.json securely stored.

For the Helm chart structure, create a directory with a structure like:

your-helm-chart/
|-- Chart.yaml
|-- values.yaml
|-- templates/
|   |-- deployment.yaml
|-- ...

In the deployment.yaml file, define your Kubernetes Deployment with the necessary specifications for your Node.js application.

This is a basic template, and you may need to modify it based on your application's specifics and desired CI/CD flow.

Helm chart for deploying a Node.js application.

The structure includes a Chart.yaml, values.yaml, and a templates directory with a deployment.yaml file.

# Chart.yaml
apiVersion: v2
name: your-app
description: A Helm chart for deploying your Node.js application
version: 0.1.0

# values.yaml
replicaCount: 1
image:
  repository: your-docker-registry/your-node-app
  tag: latest
  pullPolicy: IfNotPresent
service:
  name: your-app-service
  type: ClusterIP
  port: 80
# templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "your-app.fullname" . }}
  labels:
    app: {{ include "your-app.name" . }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ include "your-app.name" . }}
  template:
    metadata:
      labels:
        app: {{ include "your-app.name" . }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 3000
          env:
            # Add environment variables as needed for your Node.js app

---
apiVersion: v1
kind: Service
metadata:
  name: {{ .Values.service.name }}
spec:
  selector:
    app: {{ include "your-app.name" . }}
  ports:
    - protocol: TCP
      port: {{ .Values.service.port }}
      targetPort: 3000
  type: {{ .Values.service.type }}

Make sure to replace placeholders like your-app, your-docker-registry, and adjust the configurations based on your application's requirements. This is a basic example, and you may need to add more settings or customize it further based on your specific needs.