Skip to content

Commit 13b3951

Browse files
committed
Refactor GitHub Actions workflows and Terraform configurations for improved deployment and management
- Update Kubernetes deployment workflow to streamline steps and enhance error handling - Modify destroy workflow to include variable verification and cleanup of GitHub variables - Enhance Docker build workflow to push images to the correct repositories - Revise main pipeline to clarify comments and improve readability - Update Terraform configurations to introduce new variables for namespaces and application name - Add outputs for application and monitoring namespaces in Terraform - Create terraform.tfvars for infrastructure configuration - Adjust ArgoCD application manifests to use dynamic namespace and application name variables - Update Helm templates to utilize dynamic namespace values
1 parent d2f7954 commit 13b3951

File tree

12 files changed

+284
-90
lines changed

12 files changed

+284
-90
lines changed

.github/workflows/deploy.yml

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
name: Kubernetes Deployment
2-
32
on:
43
workflow_dispatch:
5-
inputs:
6-
image-tag:
7-
description: 'Docker image tag to deploy'
8-
required: false
9-
default: 'latest'
10-
type: string
114
workflow_call:
125
inputs:
136
image-tag:
@@ -26,62 +19,53 @@ jobs:
2619
name: Deploy to Kubernetes
2720
runs-on: ubuntu-latest
2821
environment: production
22+
2923
steps:
3024
- name: Checkout Repository
3125
uses: actions/checkout@v5
32-
26+
3327
- name: Login to AWS
3428
uses: aws-actions/[email protected]
3529
with:
3630
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
3731
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
3832
aws-region: us-east-1
39-
33+
4034
- name: Update kubeconfig
4135
run: |
42-
aws eks update-kubeconfig --name otel-cluster --region us-east-1
43-
44-
- name: Configure kubectl
45-
uses: statsig-io/kubectl-via-eksctl@main
46-
env:
47-
aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }}
48-
aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
49-
region: us-east-1
50-
cluster: otel-cluster
51-
52-
# ✅ Deploy Helm Chart with secrets & image tag
53-
- name: Deploy Helm Chart via ArgoCD
36+
aws eks update-kubeconfig --name ${{ vars.CLUSTER_NAME }} --region us-east-1
37+
38+
- name: Create Application Namespace
39+
run: |
40+
kubectl create namespace ${{ vars.APP_NAMESPACE }} --dry-run=client -o yaml | kubectl apply -f -
41+
42+
- name: Deploy Helm Chart
5443
run: |
55-
IMAGE_TAG=${{ inputs.image-tag || github.sha }}
56-
helm upgrade --install my-app ./helm \
57-
--namespace my-app-namespace \
58-
--create-namespace \
44+
helm upgrade --install ${{ vars.APP_NAME }} ./helm \
45+
--namespace ${{ vars.APP_NAMESPACE }} \
46+
--set namespace=${{ vars.APP_NAMESPACE }} \
5947
--set mongo.uri="${{ secrets.MONGO_URI }}" \
6048
--set mongo.username="${{ secrets.MONGO_USERNAME }}" \
6149
--set mongo.password="${{ secrets.MONGO_PASSWORD }}" \
62-
--set image.tag="$IMAGE_TAG"
63-
- name: Create Application Namespace
64-
run: |
65-
kubectl create namespace my-app-namespace --dry-run=client -o yaml | kubectl apply -f -
66-
67-
# ✅ Deploy ArgoCD Application (optional if you want ArgoCD to track)
50+
--set image.tag="${{ inputs.image-tag || github.sha }}"
51+
6852
- name: Deploy ArgoCD Applications
6953
run: |
70-
kubectl apply -f ./argocd/application.yml
54+
export APP_NAME=${{ vars.APP_NAME }}
55+
export APP_NAMESPACE=${{ vars.APP_NAMESPACE }}
56+
export ARGOCD_NAMESPACE=${{ vars.ARGOCD_NAMESPACE }}
57+
envsubst < ./argocd/application.yml | kubectl apply -f -
7158
7259
- name: Print Service Endpoints
7360
run: |
74-
75-
GRAFANA_PASSWORD=${{secrets.GRAFANA_PASSWORD}}
7661
echo "================= SERVICE ENDPOINTS ================="
77-
echo "ArgoCD: http://$(kubectl get svc argocd-server -n argocd -o jsonpath='{.status.loadBalancer.ingress[0].hostname}')"
78-
echo "Prometheus: http://$(kubectl get svc kube-prometheus-stack-prometheus -n monitoring -o jsonpath='{.status.loadBalancer.ingress[0].hostname}'):9090"
79-
echo "Grafana: http://$(kubectl get svc kube-prometheus-stack-grafana -n monitoring -o jsonpath='{.status.loadBalancer.ingress[0].hostname}')"
80-
echo "App: http://$(kubectl get svc my-app-svc -n my-app-namespace -o jsonpath='{.status.loadBalancer.ingress[0].hostname}')"
81-
62+
echo "ArgoCD: http://$(kubectl get svc argocd-server -n argocd -o jsonpath='{.status.loadBalancer.ingress[0].hostname}' 2>/dev/null || echo 'Not found')"
63+
echo "Prometheus: http://$(kubectl get svc kube-prometheus-stack-prometheus -n monitoring -o jsonpath='{.status.loadBalancer.ingress[0].hostname}' 2>/dev/null || echo 'Not found'):9090"
64+
echo "Grafana: http://$(kubectl get svc kube-prometheus-stack-grafana -n monitoring -o jsonpath='{.status.loadBalancer.ingress[0].hostname}' 2>/dev/null || echo 'Not found')"
65+
echo "App: http://$(kubectl get svc my-app-svc -n ${{ vars.APP_NAMESPACE }} -o jsonpath='{.status.loadBalancer.ingress[0].hostname}' 2>/dev/null || echo 'Not found')"
8266
echo "================= DEFAULT CREDENTIALS ================="
8367
echo "ArgoCD -> Username: admin"
84-
echo "ArgoCD -> Password: $(kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath='{.data.password}' | base64 -d)"
68+
echo "ArgoCD -> Password: $(kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath='{.data.password}' 2>/dev/null | base64 -d || echo 'Not found')"
8569
echo "Grafana -> Username: admin"
86-
echo "Grafana -> Password: $GRAFANA_PASSWORD"
87-
echo "Prometheus -> No login needed (anonymous access by default)"
70+
echo "Grafana -> Password: ${{ secrets.GRAFANA_PASSWORD }}"
71+
echo "Prometheus -> No login needed (anonymous access by default)"s

.github/workflows/destroy.yml

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,53 @@
11
name: Terraform Destroy Workflow
2-
32
on:
43
workflow_dispatch:
54

65
jobs:
76
terraform-destroy:
87
name: Terraform Destroy
98
runs-on: ubuntu-latest
10-
9+
environment: production
10+
1111
steps:
1212
- name: Checkout Repository
1313
uses: actions/checkout@v5
14-
14+
15+
- name: Verify Variables Available
16+
run: |
17+
echo "CLUSTER_NAME: ${{ vars.CLUSTER_NAME }}"
18+
echo "NAMESPACE: ${{ vars.APP_NAMESPACE }}"
19+
echo "MONITORING_NAMESPACE: ${{ vars.MONITORING_NAMESPACE }}"
20+
echo "ARGOCD_NAMESPACE: ${{ vars.ARGOCD_NAMESPACE }}"
21+
if [[ -z "${{ vars.CLUSTER_NAME }}" ]]; then
22+
echo "ERROR: CLUSTER_NAME variable not found. Infrastructure may not be deployed."
23+
exit 1
24+
fi
25+
if [[ -z "${{ vars.APP_NAMESPACE }}" ]]; then
26+
echo "ERROR: APP_NAMESPACE variable not found. Infrastructure may not be deployed."
27+
exit 1
28+
fi
29+
1530
- name: Login to AWS
1631
uses: aws-actions/[email protected]
1732
with:
1833
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
1934
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
2035
aws-region: us-east-1
21-
36+
2237
- name: Setup Terraform
2338
uses: hashicorp/[email protected]
2439
with:
2540
terraform_version: 1.5.7
26-
41+
2742
- name: Update kubeconfig
28-
run: aws eks update-kubeconfig --name otel-cluster --region us-east-1
43+
run: aws eks update-kubeconfig --name ${{ vars.CLUSTER_NAME }} --region us-east-1
44+
continue-on-error: true
45+
46+
- name: Install Helm
47+
uses: azure/[email protected]
48+
with:
49+
version: v3.14.0
50+
continue-on-error: true
2951

3052
# ---------------------------
3153
# Delete ArgoCD Applications
@@ -34,31 +56,35 @@ jobs:
3456
run: |
3557
kubectl delete application my-app -n argocd --ignore-not-found
3658
kubectl delete application kube-prometheus-stack -n argocd --ignore-not-found
59+
continue-on-error: true
3760

3861
# ---------------------------
3962
# Uninstall Helm Releases
4063
# ---------------------------
4164
- name: Uninstall Helm Releases
4265
run: |
43-
helm uninstall my-app -n my-app-namespace || true
44-
helm uninstall kube-prometheus-stack -n monitoring || true
45-
helm uninstall argocd -n argocd || true
66+
helm uninstall my-app -n ${{ vars.APP_NAMESPACE }} || true
67+
helm uninstall kube-prometheus-stack -n ${{ vars.MONITORING_NAMESPACE }} || true
68+
helm uninstall argocd -n ${{ vars.ARGOCD_NAMESPACE }} || true
69+
continue-on-error: true
4670

4771
# ---------------------------
4872
# Delete Namespaces
4973
# ---------------------------
5074
- name: Delete Namespaces
5175
run: |
52-
kubectl delete namespace my-app-namespace --ignore-not-found
53-
kubectl delete namespace monitoring --ignore-not-found
54-
kubectl delete namespace argocd --ignore-not-found
76+
kubectl delete namespace ${{ vars.APP_NAMESPACE }} --ignore-not-found
77+
kubectl delete namespace ${{ vars.MONITORING_NAMESPACE }} --ignore-not-found
78+
kubectl delete namespace ${{ vars.ARGOCD_NAMESPACE }} --ignore-not-found
79+
continue-on-error: true
5580

5681
# ---------------------------
5782
# Delete CRDs (Prometheus & Grafana)
5883
# ---------------------------
5984
- name: Delete CRDs
6085
run: |
6186
kubectl get crd -o name | grep -E 'prometheus|grafana|alertmanager|servicemonitor|prometheusrule' | xargs -r kubectl delete || true
87+
continue-on-error: true
6288

6389
# ---------------------------
6490
# Cleanup PVCs & PVs
@@ -67,14 +93,38 @@ jobs:
6793
run: |
6894
kubectl delete pvc --all -A || true
6995
kubectl delete pv --all || true
96+
continue-on-error: true
97+
98+
# ---------------------------
99+
# Wait for cleanup to complete
100+
# ---------------------------
101+
- name: Wait for cleanup
102+
run: sleep 30
70103

71104
# ---------------------------
72105
# Terraform Destroy
73106
# ---------------------------
74107
- name: Terraform Init
75108
run: terraform init
76109
working-directory: ./Terraform
77-
110+
111+
- name: Terraform Destroy Plan
112+
run: terraform plan -destroy
113+
working-directory: ./Terraform
114+
78115
- name: Terraform Destroy
79116
run: terraform destroy -auto-approve
80117
working-directory: ./Terraform
118+
119+
# ---------------------------
120+
# Clean up GitHub Variables
121+
# ---------------------------
122+
- name: Remove GitHub repository variables
123+
run: |
124+
gh variable delete CLUSTER_NAME --repo $GITHUB_REPOSITORY || true
125+
gh variable delete APP_NAMESPACE --repo $GITHUB_REPOSITORY || true
126+
gh variable delete MONITORING_NAMESPACE --repo $GITHUB_REPOSITORY || true
127+
gh variable delete ARGOCD_NAMESPACE --repo $GITHUB_REPOSITORY || true
128+
env:
129+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
130+
continue-on-error: true

.github/workflows/docker.yml

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ jobs:
4444
username: ${{ github.actor }}
4545
password: ${{ secrets.GITHUB_TOKEN }}
4646

47-
- name: Build Docker Image
47+
- name: Build and push Docker image
4848
uses: docker/build-push-action@v6
4949
with:
50-
push: false
50+
context: .
51+
push: true
5152
tags: |
52-
docker.io/${{ secrets.DOCKER_USERNAME }}/graduation-project-devops:${{ github.sha }}
53-
ghcr.io/${{ secrets.DOCKER_USERNAME }}/graduation-project-devops:${{ github.sha }}
53+
docker.io/${{ secrets.DOCKER_USERNAME }}/${{ github.event.repository.name }}:${{ github.sha }}
54+
ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}:${{ github.sha }}
55+
5456
5557
- name: Test Docker Image
5658
run: |
@@ -67,7 +69,7 @@ jobs:
6769
-e MONGO_URI="$MONGO_URI" \
6870
-e MONGO_USERNAME="$MONGO_USERNAME" \
6971
-e MONGO_PASSWORD="$MONGO_PASSWORD" \
70-
ghcr.io/${{ secrets.DOCKER_USERNAME }}/graduation-project-devops:${{ github.sha }}
72+
ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}:${{ github.sha }}
7173
7274
# Wait for container to start
7375
sleep 15
@@ -99,14 +101,16 @@ jobs:
99101
if: ${{ inputs.push-image != false }}
100102
uses: docker/build-push-action@v6
101103
with:
104+
context: .
102105
push: true
103106
tags: |
104-
docker.io/${{ secrets.DOCKER_USERNAME }}/graduation-project-devops:${{ github.sha }}
105-
ghcr.io/${{ secrets.DOCKER_USERNAME }}/graduation-project-devops:${{ github.sha }}
107+
docker.io/${{ secrets.DOCKER_USERNAME }}/${{ github.event.repository.name }}:${{ github.sha }}
108+
ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}:${{ github.sha }}
109+
106110
- name: Update Helm values with new image tag
107111
run: |
108-
sed -i "s|tag: \".*\"|tag: \"${{ github.sha }}\"|g" ./helm/values.yaml
109-
112+
sed -i "s|repository: .*|repository: docker.io/${{ secrets.DOCKER_USERNAME }}/${{ github.event.repository.name }}|g" ./helm/values.yaml
113+
110114
- name: Commit updated image tag
111115
run: |
112116
git config --local user.email "[email protected]"

.github/workflows/main-pipeline.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Solar System - Main Pipeline
22
on:
3-
push: # ← ADDED: Auto-trigger on push
3+
push: # Auto-trigger on push
44
branches: [main]
55
workflow_dispatch:
66
inputs:
@@ -24,7 +24,7 @@ on:
2424
required: false
2525
default: false
2626
type: boolean
27-
force-all: # ← ADDED: Force all workflows
27+
force-all: # Force all workflows
2828
description: 'Force run all workflows (ignore path detection)'
2929
required: false
3030
default: false
@@ -37,7 +37,7 @@ permissions:
3737
actions: read
3838

3939
jobs:
40-
# ← NEW: Detect what changed
40+
# Detect what changed
4141
detect-changes:
4242
name: Detect Changes
4343
runs-on: ubuntu-latest

0 commit comments

Comments
 (0)