Skip to content

move ip check on step downward #27

move ip check on step downward

move ip check on step downward #27

Workflow file for this run

name: Set up EKS With Terraform
on: push
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}
jobs:
LogInToAWS:
runs-on: ubuntu-latest
steps:
- name: Configure credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ env.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ env.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
TerraformPlan:
runs-on: ubuntu-latest
needs: LogInToAWS
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Initialize Terraform
run: terraform init
- name: Plan Terraform
run: terraform plan
TerraformApply:
runs-on: ubuntu-latest
needs: TerraformPlan
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Initialize Terraform (Again, if needed)
run: terraform init
- name: Apply Terraform
run: terraform apply -auto-approve
- name: Get EC2 Public IP
id: get_public_ip
run: |
terraform output -json > tf_output.json
EC2_PUBLIC_IP=$(jq -r '.ec2_public_ip.value' tf_output.json)
if [ -z "$EC2_PUBLIC_IP" ]; then
echo "Error: EC2 Public IP is empty."
exit 1
fi
echo "EC2_PUBLIC_IP=$EC2_PUBLIC_IP" >> $GITHUB_ENV
echo "Captured EC2 Public IP: $EC2_PUBLIC_IP"
- name: Ensure EC2 Public IP is Not Empty
if: ${{ env.EC2_PUBLIC_IP == '' }}
run: |
echo "Error: EC2 Public IP is empty. Exiting."
exit 1
- name: Verify EC2 Instance State
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
InstallTools:
runs-on: ubuntu-latest
needs: TerraformApply
steps:
#- name: Set AWS Environment Variables
#run: |
#echo "AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }}" >> $GITHUB_ENV
#echo "AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }}" >> $GITHUB_ENV
#echo "AWS_REGION=${{ secrets.AWS_REGION }}" >> $GITHUB_ENV
- name: Install SSH Client
run: sudo apt-get update && sudo apt-get install -y sshpass
- name: Setup SSH Key
run: |
echo "${{ secrets.EC2_SSH_PRIVATE_KEY }}" > /tmp/private_key
chmod 600 /tmp/private_key
- name: Wait for EC2 Instance to be Ready
run: sleep 100
- name: Print Environment Variables
run: |
echo "EC2_PUBLIC_IP=${{ env.EC2_PUBLIC_IP }}"
- name: Debug Public IP
run: |
echo "Public IP: ${{ env.EC2_PUBLIC_IP }}"
- name: SSH and Install AWS CLI and kubectl
run: |
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
kubectl version --client
aws eks update-kubeconfig --region ${{ env.AWS_REGION }} --name ${{ env.EKS_CLUSTER_NAME }}
EOF