diff --git a/.gitea/workflows/dockerK3s.yaml b/.gitea/workflows/dockerK3s.yaml index 0c16d5b..111933d 100644 --- a/.gitea/workflows/dockerK3s.yaml +++ b/.gitea/workflows/dockerK3s.yaml @@ -20,7 +20,7 @@ jobs: echo "exists=true" >> $GITHUB_ENV else echo "exists=false" >> $GITHUB_ENV - fi + fi - name: Set repository name as image name if: env.exists == 'true' @@ -29,17 +29,17 @@ jobs: - 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 + echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login git.ionas999.at -u ${{ secrets.REGISTRY_USERNAME }} --password-stdin - name: Build Docker image if: env.exists == 'true' 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 if: env.exists == 'true' run: | - docker push git.ionas999.at/${{ env.IMAGE_NAME }}:latest + docker push git.ionas999.at/${{ env.IMAGE_NAME }}:latest deploy_to_k8s: runs-on: ubuntu-latest @@ -49,81 +49,125 @@ jobs: uses: actions/checkout@v4 - name: Install kubectl + env: + KUBE_CONFIG: ${{ secrets.K3S_CONFIG }} + shell: bash run: | # Fetch the latest stable version of kubectl 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 sudo mv kubectl /usr/local/bin/ + + # Verify kubectl installation kubectl version --client - - name: Configure kubectl using K3s config - run: | + # Install kubelogin + 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 - 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: | - cat > k3s_deployment.yaml < k8s-manifests.yaml + apiVersion: apps/v1 + kind: Deployment + metadata: + name: ${REPO_NAME} + namespace: ${NAMESPACE} + labels: + app: ${REPO_NAME} + spec: + replicas: 1 + selector: + matchLabels: + app: ${REPO_NAME} + template: + metadata: + labels: + app: ${REPO_NAME} + spec: + containers: + - name: ${REPO_NAME} + image: git.ionas999.at/${FULL_REPO}:latest + ports: + - containerPort: 80 + --- + apiVersion: v1 + kind: Service + metadata: + name: ${REPO_NAME} + namespace: ${NAMESPACE} + spec: + type: ClusterIP + ports: + - port: 80 + targetPort: 80 + selector: + app: ${REPO_NAME} + --- + apiVersion: networking.k8s.io/v1 + kind: Ingress + metadata: + name: ${REPO_NAME}-ingress + namespace: ${NAMESPACE} + 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: + - ${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 \ No newline at end of file