Add github action

This commit is contained in:
2024-09-08 22:02:35 +10:00
parent ecbef12eef
commit b12f614d7f
3 changed files with 91 additions and 0 deletions
+58
View File
@@ -0,0 +1,58 @@
name: Build and Deploy to Kubernetes
on:
push:
branches: [ main ] # Trigger the action on push to main branch
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
K8S_NAMESPACE: default # Replace with your Kubernetes namespace
jobs:
build-and-deploy:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Build and push Docker image
uses: docker/build-push-action@v3
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Set up kubectl
uses: wahyd4/kubectl-helm-action@master
env:
KUBE_CONFIG_DATA: ${{ secrets.KUBE_CONFIG_DATA }}
- name: Deploy to Kubernetes
run: |
# Update the deployment.yaml with the new image tag
sed -i 's|image: .*|image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }}|' k8s/deployment.yaml
# Apply Kubernetes manifests
kubectl apply -f k8s/deployment.yaml
kubectl apply -f k8s/service.yaml
env:
KUBE_CONFIG_DATA: ${{ secrets.KUBE_CONFIG_DATA }}
+20
View File
@@ -0,0 +1,20 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: url-manager
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: url-manager
template:
metadata:
labels:
app: url-manager
spec:
containers:
- name: url-manager
image: ghcr.io/yourgithubusername/url-manager:latest
ports:
- containerPort: 8000
+13
View File
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Service
metadata:
name: url-manager
namespace: default
spec:
selector:
app: url-manager
ports:
- protocol: TCP
port: 80
targetPort: 8000
type: ClusterIP