Update .gitea/workflows/dockerK3s.yaml
All checks were successful
Build, Publish Docker Image, and Deploy to Kubernetes / build_and_push (push) Successful in 2s
Build, Publish Docker Image, and Deploy to Kubernetes / deploy_to_k8s (push) Successful in 10s

This commit is contained in:
jonas 2025-03-13 11:53:08 +00:00
parent 61cb7b5c7b
commit 9006382b27

View File

@ -20,7 +20,7 @@ jobs:
echo "exists=true" >> $GITHUB_ENV echo "exists=true" >> $GITHUB_ENV
else else
echo "exists=false" >> $GITHUB_ENV echo "exists=false" >> $GITHUB_ENV
fi fi
- name: Set repository name as image name - name: Set repository name as image name
if: env.exists == 'true' if: env.exists == 'true'
@ -29,17 +29,17 @@ jobs:
- name: Log in to Docker registry - name: Log in to Docker registry
if: env.exists == 'true' if: env.exists == 'true'
run: | run: |
echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login git.ionas999.at -u ${{ secrets.REGISTRY_USERNAME }} --password-stdin echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login git.ionas999.at -u ${{ secrets.REGISTRY_USERNAME }} --password-stdin
- name: Build Docker image - name: Build Docker image
if: env.exists == 'true' if: env.exists == 'true'
run: | run: |
docker build -t git.ionas999.at/${{ env.IMAGE_NAME }}:latest . docker build -t git.ionas999.at/${{ env.IMAGE_NAME }}:latest .
- name: Push Docker image - name: Push Docker image
if: env.exists == 'true' if: env.exists == 'true'
run: | run: |
docker push git.ionas999.at/${{ env.IMAGE_NAME }}:latest docker push git.ionas999.at/${{ env.IMAGE_NAME }}:latest
deploy_to_k8s: deploy_to_k8s:
runs-on: ubuntu-latest runs-on: ubuntu-latest
@ -49,81 +49,125 @@ jobs:
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Install kubectl - name: Install kubectl
env:
KUBE_CONFIG: ${{ secrets.K3S_CONFIG }}
shell: bash
run: | run: |
# Fetch the latest stable version of kubectl # Fetch the latest stable version of kubectl
KUBECTL_VERSION=$(curl -s https://dl.k8s.io/release/stable.txt) KUBECTL_VERSION=$(curl -s https://dl.k8s.io/release/stable.txt)
curl -LO "https://dl.k8s.io/release/$KUBECTL_VERSION/bin/linux/amd64/kubectl" if [ -z "$KUBECTL_VERSION" ]; then
echo "Failed to fetch the latest kubectl version. Exiting."
exit 1
fi
# Download kubectl binary
curl -LO "https://dl.k8s.io/release/v1.31.0/bin/linux/amd64/kubectl"
if [ $? -ne 0 ]; then
echo "Failed to download kubectl binary. Exiting."
exit 1
fi
# Make kubectl executable and move to PATH
chmod +x kubectl chmod +x kubectl
sudo mv kubectl /usr/local/bin/ sudo mv kubectl /usr/local/bin/
# Verify kubectl installation
kubectl version --client kubectl version --client
- name: Configure kubectl using K3s config # Install kubelogin
run: | curl -fsSLO https://github.com/int128/kubelogin/releases/download/v1.25.4/kubelogin_linux_amd64.zip
unzip kubelogin_linux_amd64.zip kubelogin -d kubelogin
mv kubelogin/kubelogin kubelogin/kubectl-oidc_login
export PATH=$PATH:$(realpath kubelogin)
# Configure kubeconfig
mkdir -p ~/.kube mkdir -p ~/.kube
echo "${{ secrets.K3S_CONFIG }}" > ~/.kube/config echo "$KUBE_CONFIG" > ~/.kube/config
- name: Generate and Apply K3s Deployment and Ingress - name: Generate and Apply K8s Manifests
run: | run: |
cat > k3s_deployment.yaml <<EOF # Get repository name and convert to lowercase
apiVersion: apps/v1 REPO_NAME=$(echo "${GITHUB_REPOSITORY##*/}" | tr '[:upper:]' '[:lower:]')
kind: Deployment FULL_REPO=$(echo "${GITHUB_REPOSITORY}" | tr '[:upper:]' '[:lower:]')
metadata:
name: ${GITHUB_REPOSITORY} # Set namespace consistently
labels: NAMESPACE="gitea-deployment"
app: ${GITHUB_REPOSITORY}
spec: echo "Using repository name: $REPO_NAME"
replicas: 1 echo "Using full repository path: $FULL_REPO"
selector: echo "Using namespace: $NAMESPACE"
matchLabels:
app: ${GITHUB_REPOSITORY} # Create namespace if it doesn't exist
template: kubectl get namespace $NAMESPACE || kubectl create namespace $NAMESPACE
metadata:
labels: cat <<EOF > k8s-manifests.yaml
app: ${GITHUB_REPOSITORY} apiVersion: apps/v1
spec: kind: Deployment
containers: metadata:
- name: ${GITHUB_REPOSITORY} name: ${REPO_NAME}
image: git.ionas999.at/${GITHUB_REPOSITORY}:latest namespace: ${NAMESPACE}
ports: labels:
- containerPort: 80 app: ${REPO_NAME}
--- spec:
apiVersion: v1 replicas: 1
kind: Service selector:
metadata: matchLabels:
name: ${GITHUB_REPOSITORY} app: ${REPO_NAME}
spec: template:
type: ClusterIP metadata:
ports: labels:
- port: 80 app: ${REPO_NAME}
targetPort: 80 spec:
selector: containers:
app: ${GITHUB_REPOSITORY} - name: ${REPO_NAME}
--- image: git.ionas999.at/${FULL_REPO}:latest
apiVersion: networking.k8s.io/v1 ports:
kind: Ingress - containerPort: 80
metadata: ---
name: ${GITHUB_REPOSITORY}-ingress apiVersion: v1
namespace: gitea-deployment kind: Service
annotations: metadata:
traefik.ingress.kubernetes.io/router.entrypoints: websecure name: ${REPO_NAME}
traefik.ingress.kubernetes.io/router.tls: "true" namespace: ${NAMESPACE}
traefik.ingress.kubernetes.io/router.tls.certresolver: le spec:
spec: type: ClusterIP
ingressClassName: traefik ports:
tls: - port: 80
- hosts: targetPort: 80
- ${GITHUB_REPOSITORY}.git.ionas999.at selector:
rules: app: ${REPO_NAME}
- host: ${GITHUB_REPOSITORY}.git.ionas999.at ---
http: apiVersion: networking.k8s.io/v1
paths: kind: Ingress
- path: / metadata:
pathType: Prefix name: ${REPO_NAME}-ingress
backend: namespace: ${NAMESPACE}
service: annotations:
name: ${GITHUB_REPOSITORY} traefik.ingress.kubernetes.io/router.entrypoints: websecure
port: traefik.ingress.kubernetes.io/router.tls: "true"
number: 80 traefik.ingress.kubernetes.io/router.tls.certresolver: le
EOF spec:
ingressClassName: traefik
kubectl apply -f k3s_deployment.yaml tls:
- hosts:
- ${REPO_NAME}.git.ionas999.at
rules:
- host: ${REPO_NAME}.git.ionas999.at
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: ${REPO_NAME}
port:
number: 80
EOF
# Display the manifest for debugging
echo "--- Generated manifest: ---"
cat k8s-manifests.yaml
echo "-------------------------"
# Apply the manifests
kubectl apply -f k8s-manifests.yaml