pipeline { agent any environment { // Define your AWS EKS environment variables here region='eu-west-2' vpc_cidr='10.0.0.0/16' cluster_name='Arsit-Eks' iam_user_name='semiharsan' fargate_profile_name='Arsit-Eks-Fargate' // Define Linux folder paths for your terraform and applications codes JENKINS_WORKSPACE_FOR_TERRAFORMCODES='terraform' TERRAFORM_WORKSPACE='/var/lib/jenkins/workspace/iaas/terraform' } stages { stage('Create Aws EKS with Fargate Profile') { steps { script { withCredentials([[ $class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY', credentialsId: 'jenkins' ]]) { sh ''' set +e [ -d $JENKINS_WORKSPACE_FOR_TERRAFORMCODES ] && { echo "There is a $JENKINS_WORKSPACE_FOR_TERRAFORMCODES repository here. Let us remove it first" rm -rf $JENKINS_WORKSPACE_FOR_TERRAFORMCODES echo "$JENKINS_WORKSPACE_FOR_TERRAFORMCODES folder removed successfully. Let us download again with the latest version" git clone https://github.com/semiharsan/terraform.git cp -ar $JENKINS_WORKSPACE_FOR_TERRAFORMCODES/* $TERRAFORM_WORKSPACE chown -R jenkins:jenkins $TERRAFORM_WORKSPACE } || { echo "$JENKINS_WORKSPACE_FOR_TERRAFORMCODES folder not found. Let us download it from Git" git clone https://github.com/semiharsan/terraform.git cp -ar $JENKINS_WORKSPACE_FOR_TERRAFORMCODES/* $TERRAFORM_WORKSPACE chown -R jenkins:jenkins $TERRAFORM_WORKSPACE rm -rf $JENKINS_WORKSPACE_FOR_TERRAFORMCODES } set +e cd $TERRAFORM_WORKSPACE/AWS/EKS TF_VAR_region="$region" TF_VAR_vpc_cidr="$vpc_cidr" TF_VAR_cluster_name="$cluster_name" TF_VAR_iam_user_name="$iam_user_name" TF_VAR_fargate_profile_name="$fargate_profile_name" terraform init terraform apply -auto-approve -var="region=$TF_VAR_region" -var="vpc_cidr=$TF_VAR_vpc_cidr" -var="cluster_name=$TF_VAR_cluster_name" -var="fargate_profile_name=$TF_VAR_fargate_profile_name" -var="iam_user_name=$TF_VAR_iam_user_name" export awsLoadBalancerControllerRoleArn=$(terraform output -raw aws_load_balancer_controller_role_arn) envsubst < nginx-ingress-controller.yaml | kubectl apply -f - kubectl -n ingress-nginx wait ddeployment.apps/ingress-nginx-controller --for=condition=Available --timeout=60s kubectl get all --all-namespaces ''' } } } } } }