From b12f614d7f933257be34484456bf15e63cbb53ac Mon Sep 17 00:00:00 2001 From: Junwei Zhao Date: Sun, 8 Sep 2024 22:02:35 +1000 Subject: [PATCH] Add github action --- .github/workflows/build-and-deploy.yml | 58 ++++++++++++++++++++++++++ k8s/deployment.yaml | 20 +++++++++ k8s/service.yaml | 13 ++++++ 3 files changed, 91 insertions(+) create mode 100644 .github/workflows/build-and-deploy.yml create mode 100644 k8s/deployment.yaml create mode 100644 k8s/service.yaml diff --git a/.github/workflows/build-and-deploy.yml b/.github/workflows/build-and-deploy.yml new file mode 100644 index 0000000..4b4560c --- /dev/null +++ b/.github/workflows/build-and-deploy.yml @@ -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 }} diff --git a/k8s/deployment.yaml b/k8s/deployment.yaml new file mode 100644 index 0000000..af7ea7e --- /dev/null +++ b/k8s/deployment.yaml @@ -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 diff --git a/k8s/service.yaml b/k8s/service.yaml new file mode 100644 index 0000000..802e50f --- /dev/null +++ b/k8s/service.yaml @@ -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