Skip to content

Instantly share code, notes, and snippets.

@jimi008
Created June 11, 2024 11:39
Show Gist options
  • Save jimi008/f708392cc1ddc4cd3640582dc569ccac to your computer and use it in GitHub Desktop.
Save jimi008/f708392cc1ddc4cd3640582dc569ccac to your computer and use it in GitHub Desktop.
Jenkins Shell script to deploy React / NodeJs application on Oracle remote staging server using Kubernetes
#!/bin/bash
# Ensure BUILD_NUMBER is a string
BUILD_NUMBER="Build_$BUILD_NUMBER"
# Use SSH credentials to connect to a remote server
eval $(ssh-agent)
ssh-add $oracle_Instance_Key_file
# SSH into Minikube machine and run the deployment commands
sshpass -P passphrase -f <(printf '%s\n' $oracle_Instance_Passphrase) ssh -o "StrictHostKeyChecking no" -i $oracle_Instance_Key_file $Oracle_Machine_Ubuntu_User_IP "
env BUILD_NUMBER="$BUILD_NUMBER"
echo "The BUILD_NUMBER is:"; echo $BUILD_NUMBER;
echo "OS is:"; uname -a
# Remove old config
rm -f k8s-deployment.yaml
# Create Kubernetes deployment configuration file
cat <<EOL > k8s-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: staging-deploy
build: $BUILD_NUMBER
name: staging-deploy
annotations:
kubernetes.io/change-cause: "Reapply at $(date)"
spec:
replicas: 2
minReadySeconds: 30
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 2
selector:
matchLabels:
app: staging-deploy
template:
metadata:
creationTimestamp: null
labels:
app: staging-deploy
Build: $BUILD_NUMBER
spec:
containers:
- image: node:10.15.3
imagePullPolicy: Always
name: node
command: ["/bin/sh", "-c"]
args:
- |
git clone https://github.com/GermaVinsmoke/bmi-calculator
cd bmi-calculator
# Fetch the latest changes from the remote repository
git fetch
# Reset the local repository to match the remote repository
git reset --hard origin/master
git fetch
npm install
npm start
ports:
- containerPort: 3000
env:
- name: BUILD_NUMBER
value: $BUILD_NUMBER
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
name: react-app-service
spec:
selector:
app: staging-deploy
ports:
- protocol: TCP
port: 80
targetPort: 3000
nodePort: 30080
type: NodePort
EOL
# Delete the previous deployment
#kubectl delete -f k8s-deployment.yaml
# Apply the Kubernetes deployment
kubectl apply -f k8s-deployment.yaml
# List Kubernetes resources to verify
kubectl get all"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment