Skip to content

Delete EKS Cluster #932

Delete EKS Cluster

Delete EKS Cluster #932

name: Delete EKS Cluster
on:
delete:
branches: '**'
workflow_dispatch:
inputs:
cluster-name:
description: "Cluster Name"
required: true
type: string
env:
AWS_REGION: "eu-north-1"
EKSCTL_VERSION: "v0.124.0"
FLUX_VERSION: "v0.36.0"
jobs:
delete-cluster:
if: ${{ startsWith(github.event.ref, 'cluster-') || (github.event_name == 'workflow_dispatch')}}
runs-on: ubuntu-latest
outputs:
CLUSTER_NAME: ${{ steps.outputs.outputs.CLUSTER_NAME }}
STATUS: ${{ job.status }}
STATUS_EMOJI: ${{ steps.outputs.outputs.STATUS_EMOJI }}
SLACK_MESSAGE: ${{ steps.outputs.outputs.SLACK_MESSAGE }}
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v3
- name: Install aws-cli
run: |
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install --update
aws --version
- name: Install eksctl
run: |
curl --silent --location "https://github.com/weaveworks/eksctl/releases/download/${{ env.EKSCTL_VERSION }}/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp
sudo mv /tmp/eksctl /usr/local/bin
eksctl version
- name: Install helm
uses: azure/[email protected]
with:
version: v3.8.2
- name: Install kubectl
uses: Azure/[email protected]
with:
version: v1.23.7
- name: Install flux
run: |
VER=$(echo ${{ env.FLUX_VERSION }} | sed 's/^[^0-9]*//')
curl --silent --location https://github.com/fluxcd/flux2/releases/download/${{ env.FLUX_VERSION }}/flux_${VER}_$(uname -s)_amd64.tar.gz | tar xz -C /tmp
sudo mv /tmp/flux /usr/local/bin
flux version --client
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1-node16
with:
role-to-assume: arn:aws:iam::894516026745:role/WeaveEksGithubActions
aws-region: ${{ env.AWS_REGION }}
- name: Export Cluster Name if Branch Deleted
if: ${{ startsWith(github.event.ref, 'cluster-') }}
run: |
export BRANCH_NAME=${{ github.event.ref }}
export CLUSTER_NAME=${BRANCH_NAME#cluster-}
echo "CLUSTER_NAME=${CLUSTER_NAME}" >> $GITHUB_ENV
- name: Export Cluster Name if Triggered Manually
if: ${{ (github.event_name == 'workflow_dispatch') }}
run: |
echo "CLUSTER_NAME=${{ inputs.cluster-name }}" >> $GITHUB_ENV
- name: kubeconfig
run: |
echo "Cluster: ${{ env.CLUSTER_NAME }}"
eksctl utils write-kubeconfig --region ${{ env.AWS_REGION }} --cluster ${{ env.CLUSTER_NAME }} --kubeconfig=$HOME/.kube/config
kubectl get nodes
- name: Delete Cluster
run: |
export GITHUB_TOKEN=${{ secrets.WEAVE_GITOPS_BOT_TOKEN_CLUSTERS_CONFIG }}
echo "Deleting ${{ env.CLUSTER_NAME }} cluster ..."
$GITHUB_WORKSPACE/eksctl-clusters/scripts/destroy-cluster.sh --cluster-name ${{ env.CLUSTER_NAME }}
- name: Check running clusters
run: |
eksctl get clusters --region ${{ env.AWS_REGION }}
- name: outputs
if: always()
id: outputs
run: |
echo "CLUSTER_NAME=${{ env.CLUSTER_NAME }}" >> $GITHUB_OUTPUT
if [ "${{ job.status }}" == "success" ]; then
echo "STATUS_EMOJI=greentick" >> $GITHUB_OUTPUT
echo "SLACK_MESSAGE=Cluster has been deleted successfully!" >> $GITHUB_OUTPUT
elif [ "${{ job.status }}" == "failure" ]; then
echo "STATUS_EMOJI=failed" >> $GITHUB_OUTPUT
echo "SLACK_MESSAGE=Cluster failed to be deleted!!" >> $GITHUB_OUTPUT
fi
slack-notifications:
if: ${{ always() && ( startsWith(github.event.ref, 'cluster-') || (github.event_name == 'workflow_dispatch') ) }}
needs:
- delete-cluster
uses: ./.github/workflows/slack-notification.yaml
with:
header-text: "DELETE CLUSTER: ${{ needs.delete-cluster.outputs.CLUSTER_NAME }}"
message: " :${{ needs.delete-cluster.outputs.STATUS_EMOJI }}: ${{ needs.delete-cluster.outputs.SLACK_MESSAGE }}"
secrets:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}