From 8eb99588a826c1c31c992e6b80fb8548a562b38d Mon Sep 17 00:00:00 2001 From: Oloruntobi Olurombi Date: Thu, 29 Aug 2024 23:01:25 +0100 Subject: [PATCH] add conditions to install tools step --- .github/workflows/eks-setup.yaml | 40 +++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/.github/workflows/eks-setup.yaml b/.github/workflows/eks-setup.yaml index 7a5e6c2..90b9123 100644 --- a/.github/workflows/eks-setup.yaml +++ b/.github/workflows/eks-setup.yaml @@ -87,25 +87,39 @@ jobs: set -x ssh -o StrictHostKeyChecking=no -i /tmp/private_key ec2-user@${{ env.EC2_PUBLIC_IP }} << 'EOF' sudo yum install -y unzip - curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" - unzip awscliv2.zip - sudo ./aws/install - curl -O "https://s3.us-west-2.amazonaws.com/amazon-eks/1.28.11/2024-07-12/bin/linux/amd64/kubectl" - chmod +x ./kubectl - mkdir -p $HOME/bin && cp ./kubectl $HOME/bin/kubectl && export PATH=$HOME/bin:$PATH + # Check if AWS CLI exists + if ! command -v aws &> /dev/null + then + echo "AWS CLI not found, installing..." + curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" + unzip awscliv2.zip + sudo ./aws/install + else + echo "AWS CLI already installed" + fi + + # Check if kubectl exists + if ! command -v kubectl &> /dev/null + then + echo "kubectl not found, installing..." + curl -O "https://s3.us-west-2.amazonaws.com/amazon-eks/1.28.11/2024-07-12/bin/linux/amd64/kubectl" + chmod +x ./kubectl + mkdir -p $HOME/bin && cp ./kubectl $HOME/bin/kubectl && export PATH=$HOME/bin:$PATH + else + echo "kubectl already installed" + fi + kubectl version --client aws eks update-kubeconfig --region ${{ env.AWS_REGION }} --name ${{ env.EKS_CLUSTER_NAME }} - EOF - + EOF + - name: Verify EC2 Instance State - run: | + run: | INSTANCE_STATE=$(aws ec2 describe-instances --instance-ids $(terraform output -raw ec2_instance_id) --query 'Reservations[*].Instances[*].State.Name' --output text) echo "EC2 Instance State: $INSTANCE_STATE" if [[ "$INSTANCE_STATE" != "running" ]]; then echo "Error: EC2 instance is not running." exit 1 - fi - - - + fi + \ No newline at end of file