pipeline { agent any triggers { githubPush() // This will trigger the pipeline when a new commit is pushed to the 'main' branch. } environment { // Define your AWS environment variables here AWS_REGION='eu-west-2' ECR_REPO_NAME='arsit-ecr-repo' EKS_CLUSTER_NAME='Arsit-Eks' //These variables are for your certificate request if your public dns is on Cloudflare CLOUDFLARE_API_KEY='writeyourowncloudflareapikey' CLOUDFLARE_EMAIL='write@youremail.com' DOMAINAME="'*.arsit.tech'" CERT_NAME='arsit-cert' CERT_SECRET_NAME='arsit-tls' // Define your image url and pubic port number according to your design image_name='write_yourown_image_url' public_port='80' // Define Docker image name and version DOCKERIMAGE_NAME='pythonapp' DOCKERIMAGE_TAG_VERSION='latest' DOCKERIMAGE_ID = '' // Write here your kubernetes cluster namespace for your deployment NAMESPACE="default" DEPLOYMENTREPLICA="2" } stages { stage('Checkout') { steps { script { // Checkout the code from GitHub checkout scmGit(branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[credentialsId: 'b66dea26-cddc-4ad5-bc33-15595ceebe9f', url: 'https://github.com/semiharsan/pythondemoapp.git']]) } } } stage('Deploy Python Applicaton on EKS Cluster') { steps { script { withCredentials([[ $class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY', credentialsId: 'jenkins' ]]) { sh ''' aws eks update-kubeconfig --region $AWS_REGION --name $EKS_CLUSTER_NAME DEPLOYMENT_NAME=$(awk '/metadata:/ {f=1} f && /name:/ {print $2; exit}' awseks.yaml | tr -d '\r' 2>/dev/null) PODS=$(kubectl get pods -n $NAMESPACE -l app=$DEPLOYMENT_NAME -o custom-columns=NAME:.metadata.name --no-headers) if [ -z "$PODS" ]; then echo "Deployment '$DEPLOYMENT_NAME' not found. Let us create it" envsubst < cert-request-for-ekspod.yaml | kubectl apply -f - envsubst < awseks.yaml | kubectl apply -f - kubectl wait deployment/$DEPLOYMENT_NAME --for=condition=Available --timeout=120s echo "Deployment '${DEPLOYMENT_NAME}' created." else echo "There is already pods with name $PODS let us update deployment" NEW_REPLICA=$((DEPLOYMENTREPLICA + 1)) kubectl scale deployment/$DEPLOYMENT_NAME --replicas=$NEW_REPLICA kubectl get pods kubectl wait deployment/$DEPLOYMENT_NAME --for=condition=Available --timeout=120s echo Deleting pods: $PODS kubectl delete pod $PODS -n $NAMESPACE kubectl scale deployment/$DEPLOYMENT_NAME --replicas=$DEPLOYMENTREPLICA kubectl get pods fi ''' } } } } } }