Skip to content

Commit 673f0e8

Browse files
committed
feat: k8_cluster_destroy workflow
1 parent d003b75 commit 673f0e8

File tree

4 files changed

+96
-17
lines changed

4 files changed

+96
-17
lines changed

.github/workflows/k8_cluster_apply.yml

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ permissions:
1010
actions: read
1111

1212
jobs:
13-
k8-apply:
14-
name: 'Kubernetes Apply'
13+
k8-cluster-apply:
14+
name: 'K8: Apply Cluster'
1515
runs-on: ubuntu-latest
1616
environment: dev
1717
env:
18-
KUBERNETES_NAMESPACE: "${{ vars.KUBERNETES_NAMESPACE }}"
18+
KUBELOGIN_VERSION: "v0.0.25"
19+
KUSTOMIZE_VERSION: "${{ vars.KUSTOMIZE_VERSION }}"
20+
AZURE_RESOURCE_GROUP: "${{ vars.AZURE_RESOURCE_GROUP }}"
1921
KUBERNETES_CLUSTER_NAME: "${{ vars.KUBERNETES_CLUSTER_NAME }}"
22+
KUBERNETES_NAMESPACE: "${{ vars.KUBERNETES_NAMESPACE }}"
2023
KUBERNETES_MANIFEST_PATH: "${{ vars.KUBERNETES_MANIFEST_PATH }}"
21-
AZURE_RESOURCE_GROUP: "${{ vars.AZURE_RESOURCE_GROUP }}"
2224
steps:
2325
# Checkout the repository to the GitHub Actions runner
2426
- name: Checkout
@@ -27,7 +29,6 @@ jobs:
2729
- name: GitHub Configuration
2830
run: git config --global url."https://oauth2:${{ secrets.TOKEN_GITHUB }}@github.com".insteadOf https://github.com
2931

30-
3132
# Install the latest version of Kubernetes CLI and configure the Kubernetes CLI configuration file with a Kubernetes Cloud user API token
3233
- name: Azure Login
3334
uses: Azure/login@v2
@@ -40,30 +41,32 @@ jobs:
4041
- name: Set up kubelogin for non-interactive login
4142
uses: azure/use-kubelogin@v1
4243
with:
43-
kubelogin-version: 'v0.0.25'
44+
kubelogin-version: ${{ env.KUBELOGIN_VERSION }}
4445

4546
- uses: azure/aks-set-context@v3
4647
with:
4748
resource-group: ${{ env.AZURE_RESOURCE_GROUP }}
4849
cluster-name: ${{ env.KUBERNETES_CLUSTER_NAME }}
4950
admin: 'false'
5051
use-kubelogin: 'true'
51-
52-
# Deploys application based on given manifest file
53-
# - name: Deploys application
54-
# uses: Azure/k8s-deploy@v4
55-
# with:
56-
# action: deploy
57-
# manifests: ${{ env.KUBERNETES_MANIFEST_PATH }}
58-
# namespace: ${{ env.KUBERNETES_NAMESPACE }}
59-
52+
6053
# Installs kustomize using a custom script
6154
- name: Install Kustomize
6255
shell: bash
56+
env:
57+
INSTALL_KUSTOMIZE_SH: "cicd-deployment-scripts/k8-cluster/install_kustomize.sh"
6358
run: |
6459
git clone https://github.com/code-kern-ai/cicd-deployment-scripts.git
65-
bash cicd-deployment-scripts/k8-cluster/install_kustomize.sh
60+
bash ${{ env.INSTALL_KUSTOMIZE_SH }} -v ${{ env.KUSTOMIZE_VERSION }}
6661
6762
- name: Apply Kustomization
6863
shell: bash
6964
run: kubectl apply -k ${{ env.KUBERNETES_MANIFEST_PATH }}
65+
66+
# Deploys application based on given manifest file
67+
# - name: Deploys application
68+
# uses: Azure/k8s-deploy@v4
69+
# with:
70+
# action: deploy
71+
# manifests: ${{ env.KUBERNETES_MANIFEST_PATH }}
72+
# namespace: ${{ env.KUBERNETES_NAMESPACE }}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: 'Kubernetes Destroy'
2+
3+
on:
4+
workflow_call:
5+
6+
# Special permissions required for OIDC authentication
7+
permissions:
8+
id-token: write
9+
contents: read
10+
actions: read
11+
12+
jobs:
13+
k8-cluster-destroy:
14+
name: 'K8: Destroy Deployments & Services'
15+
runs-on: ubuntu-latest
16+
environment: dev
17+
env:
18+
KUBERNETES_NAMESPACE: "${{ vars.KUBERNETES_NAMESPACE }}"
19+
KUBERNETES_CLUSTER_NAME: "${{ vars.KUBERNETES_CLUSTER_NAME }}"
20+
KUBERNETES_MANIFEST_PATH: "${{ vars.KUBERNETES_MANIFEST_PATH }}"
21+
AZURE_RESOURCE_GROUP: "${{ vars.AZURE_RESOURCE_GROUP }}"
22+
steps:
23+
# Checkout the repository to the GitHub Actions runner
24+
- name: Checkout
25+
uses: actions/checkout@v4
26+
27+
- name: GitHub Configuration
28+
run: git config --global url."https://oauth2:${{ secrets.TOKEN_GITHUB }}@github.com".insteadOf https://github.com
29+
30+
# Install the latest version of Kubernetes CLI and configure the Kubernetes CLI configuration file with a Kubernetes Cloud user API token
31+
- name: Azure Login
32+
uses: Azure/login@v2
33+
with:
34+
client-id: ${{ secrets.AZURE_CLIENT_ID }}
35+
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
36+
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
37+
38+
# Use kubelogin to configure your kubeconfig for Azure auth
39+
- name: Set up kubelogin for non-interactive login
40+
uses: azure/use-kubelogin@v1
41+
with:
42+
kubelogin-version: 'v0.0.25'
43+
44+
- uses: azure/aks-set-context@v3
45+
with:
46+
resource-group: ${{ env.AZURE_RESOURCE_GROUP }}
47+
cluster-name: ${{ env.KUBERNETES_CLUSTER_NAME }}
48+
admin: 'false'
49+
use-kubelogin: 'true'
50+
51+
# Deploys application based on given manifest file
52+
# - name: Deploys application
53+
# uses: Azure/k8s-deploy@v4
54+
# with:
55+
# action: deploy
56+
# manifests: ${{ env.KUBERNETES_MANIFEST_PATH }}
57+
# namespace: ${{ env.KUBERNETES_NAMESPACE }}
58+
59+
- name: Destroy Deployments
60+
shell: bash
61+
run: |
62+
git clone https://github.com/code-kern-ai/cicd-deployment-scripts.git
63+
bash cicd-deployment-scripts/k8-cluster/k8_destroy.sh

k8-cluster/destroy.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# !/bin/bash
2+
set -e
3+
4+
KUBERNETES_DEPLOYMENTS=$(kubectl get deployment --output json | jq -r '.items[].metadata.name')
5+
KUBERNETES_SERVICES=$(kubectl get service --output json | jq -r '.items[].metadata.name')
6+
7+
for deploy in $KUBERNETES_DEPLOYMENTS; do
8+
kubectl delete deployment $deploy
9+
done
10+
11+
for svc in $KUBERNETES_SERVICES; do
12+
kubectl delete service $svc
13+
done

k8-cluster/install_kustomize.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set -e
33

44
KUSTOMIZE_VERSION="5.3.0"
55

6-
while getopts v flag
6+
while getopts v: flag
77
do
88
case "${flag}" in
99
v) KUSTOMIZE_VERSION=${OPTARG};;

0 commit comments

Comments
 (0)