Some checks failed
Build, Publish Docker Image, and Deploy to Kubernetes / build_and_push_and_deploy (push) Failing after 3s
134 lines
3.5 KiB
YAML
134 lines
3.5 KiB
YAML
name: Build, Publish Docker Image, and Deploy to Kubernetes
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- main
|
|
workflow_dispatch:
|
|
|
|
jobs:
|
|
build_and_push_and_deploy:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
# Step 1: Checkout repository
|
|
- name: Checkout repository
|
|
uses: actions/checkout@v4
|
|
|
|
# Step 2: Check if Dockerfile exists
|
|
- name: Check if Dockerfile exists
|
|
id: check_dockerfile
|
|
run: |
|
|
if [ -f "Dockerfile" ]; then
|
|
echo "exists=true" >> $GITHUB_ENV
|
|
else
|
|
echo "exists=false" >> $GITHUB_ENV
|
|
fi
|
|
|
|
# Step 3: Set repository name as image name
|
|
- name: Set repository name as image name
|
|
if: env.exists == 'true'
|
|
run: echo "IMAGE_NAME=$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
|
|
|
|
# Step 4: Log in to Docker registry
|
|
- name: Log in to Docker registry
|
|
if: env.exists == 'true'
|
|
run: |
|
|
echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login git.ionas999.at -u ${{ secrets.REGISTRY_USERNAME }} --password-stdin
|
|
|
|
# Step 5: Build Docker image
|
|
- name: Build Docker image
|
|
if: env.exists == 'true'
|
|
run: |
|
|
docker build -t git.ionas999.at/${{ env.IMAGE_NAME }}:latest .
|
|
|
|
# Step 6: Push Docker image
|
|
- name: Push Docker image
|
|
if: env.exists == 'true'
|
|
run: |
|
|
docker push git.ionas999.at/${{ env.IMAGE_NAME }}:latest
|
|
|
|
# Step 7: Install kubectl
|
|
- name: Install kubectl
|
|
run: |
|
|
KUBECTL_VERSION=$(curl -s https://dl.k8s.io/release/stable.txt)
|
|
curl -LO "https://dl.k8s.io/release/$KUBECTL_VERSION/bin/linux/amd64/kubectl"
|
|
chmod +x kubectl
|
|
sudo mv kubectl /usr/local/bin/
|
|
kubectl version --client
|
|
|
|
# Step 8: Configure kubectl using K3s config
|
|
- name: Configure kubectl
|
|
run: |
|
|
mkdir -p ~/.kube
|
|
echo "${{ secrets.K3S_CONFIG }}" > ~/.kube/config
|
|
|
|
# Step 9: Generate Kubernetes Deployment and Ingress configuration
|
|
- name: Create Kubernetes Deployment and Ingress
|
|
run: |
|
|
cat > k3s_deployment.yaml <<EOF
|
|
apiVersion: apps/v1
|
|
kind: Deployment
|
|
metadata:
|
|
name: ${GITHUB_REPOSITORY}
|
|
labels:
|
|
app: ${GITHUB_REPOSITORY}
|
|
spec:
|
|
replicas: 1
|
|
selector:
|
|
matchLabels:
|
|
app: ${GITHUB_REPOSITORY}
|
|
template:
|
|
metadata:
|
|
labels:
|
|
app: ${GITHUB_REPOSITORY}
|
|
spec:
|
|
containers:
|
|
- name: ${GITHUB_REPOSITORY}
|
|
image: git.ionas999.at/${GITHUB_REPOSITORY}:latest
|
|
ports:
|
|
- containerPort: 80
|
|
---
|
|
apiVersion: v1
|
|
kind: Service
|
|
metadata:
|
|
name: ${GITHUB_REPOSITORY}
|
|
spec:
|
|
type: ClusterIP
|
|
ports:
|
|
- port: 80
|
|
targetPort: 80
|
|
selector:
|
|
app: ${GITHUB_REPOSITORY}
|
|
---
|
|
apiVersion: networking.k8s.io/v1
|
|
kind: Ingress
|
|
metadata:
|
|
name: ${GITHUB_REPOSITORY}-ingress
|
|
namespace: gitea-deployment
|
|
annotations:
|
|
traefik.ingress.kubernetes.io/router.entrypoints: websecure
|
|
traefik.ingress.kubernetes.io/router.tls: "true"
|
|
traefik.ingress.kubernetes.io/router.tls.certresolver: le
|
|
spec:
|
|
ingressClassName: traefik
|
|
tls:
|
|
- hosts:
|
|
- ${GITHUB_REPOSITORY}.git.ionas999.at
|
|
rules:
|
|
- host: ${GITHUB_REPOSITORY}.git.ionas999.at
|
|
http:
|
|
paths:
|
|
- path: /
|
|
pathType: Prefix
|
|
backend:
|
|
service:
|
|
name: ${GITHUB_REPOSITORY}
|
|
port:
|
|
number: 80
|
|
EOF
|
|
|
|
# Step 10: Apply Kubernetes resources
|
|
- name: Apply Kubernetes configuration
|
|
run: |
|
|
kubectl apply -f k3s_deployment.yaml
|