Update .gitea/workflows/dockerK3s.yaml
This commit is contained in:
parent
61cb7b5c7b
commit
9006382b27
@ -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 <<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
|
||||
|
||||
kubectl apply -f k3s_deployment.yaml
|
||||
# Get repository name and convert to lowercase
|
||||
REPO_NAME=$(echo "${GITHUB_REPOSITORY##*/}" | tr '[:upper:]' '[:lower:]')
|
||||
FULL_REPO=$(echo "${GITHUB_REPOSITORY}" | tr '[:upper:]' '[:lower:]')
|
||||
|
||||
# Set namespace consistently
|
||||
NAMESPACE="gitea-deployment"
|
||||
|
||||
echo "Using repository name: $REPO_NAME"
|
||||
echo "Using full repository path: $FULL_REPO"
|
||||
echo "Using namespace: $NAMESPACE"
|
||||
|
||||
# Create namespace if it doesn't exist
|
||||
kubectl get namespace $NAMESPACE || kubectl create namespace $NAMESPACE
|
||||
|
||||
cat <<EOF > 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
|
||||
Loading…
Reference in New Issue
Block a user