ci: deploy Hugo to k3s via Gitea Actions
Some checks failed
Deploy Hugo Blog to k3s / build-and-deploy (push) Failing after 21m54s
Some checks failed
Deploy Hugo Blog to k3s / build-and-deploy (push) Failing after 21m54s
This commit is contained in:
@@ -8,6 +8,13 @@ on:
|
|||||||
env:
|
env:
|
||||||
REGISTRY: git.luochen570.cn
|
REGISTRY: git.luochen570.cn
|
||||||
IMAGE: luochen570/hugo-blog
|
IMAGE: luochen570/hugo-blog
|
||||||
|
K8S_NAMESPACE: hugo
|
||||||
|
K8S_DEPLOYMENT: hugo-blog
|
||||||
|
K8S_CONTAINER: nginx
|
||||||
|
K8S_INGRESS: hugo-blog
|
||||||
|
TEST_HOST: hugo.test.luochen570.cn
|
||||||
|
PROD_HOST: hugo.luochen570.cn
|
||||||
|
DOCKER_HOST: unix:///run/user/1000/docker.sock
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-and-deploy:
|
build-and-deploy:
|
||||||
@@ -32,22 +39,68 @@ jobs:
|
|||||||
env:
|
env:
|
||||||
REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }}
|
REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }}
|
||||||
run: |
|
run: |
|
||||||
# Find the Docker daemon (dind is on the gateway)
|
set -euo pipefail
|
||||||
DOCKER_HOST_IP=$(ip route | grep default | awk '{print $3}')
|
|
||||||
export DOCKER_HOST="tcp://${DOCKER_HOST_IP}:2375"
|
|
||||||
echo "DOCKER_HOST=$DOCKER_HOST"
|
|
||||||
|
|
||||||
# Login
|
# k3s dind-rootless runner: use rootless Docker Unix socket.
|
||||||
echo "$REGISTRY_TOKEN" | docker login $REGISTRY -u luochen570 --password-stdin
|
# Avoid the unverified TCP Docker API path here.
|
||||||
|
export DOCKER_HOST="${DOCKER_HOST}"
|
||||||
|
unset DOCKER_TLS_VERIFY DOCKER_CERT_PATH DOCKER_CONTEXT
|
||||||
|
|
||||||
# Build
|
# Avoid proxy pollution in the container/rootless Docker path; proxy envs
|
||||||
docker build -t $REGISTRY/$IMAGE:${{ github.sha }} .
|
# can break image pulls through the slirp4netns namespace.
|
||||||
docker tag $REGISTRY/$IMAGE:${{ github.sha }} $REGISTRY/$IMAGE:latest
|
unset HTTP_PROXY HTTPS_PROXY ALL_PROXY http_proxy https_proxy all_proxy
|
||||||
|
|
||||||
# Push
|
if [ -z "${REGISTRY_TOKEN:-}" ]; then
|
||||||
docker push $REGISTRY/$IMAGE:${{ github.sha }}
|
echo "REGISTRY_TOKEN secret is required" >&2
|
||||||
docker push $REGISTRY/$IMAGE:latest
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
docker version
|
||||||
|
echo "$REGISTRY_TOKEN" | docker login "$REGISTRY" -u luochen570 --password-stdin
|
||||||
|
docker build \
|
||||||
|
-t "$REGISTRY/$IMAGE:${{ github.sha }}" \
|
||||||
|
-t "$REGISTRY/$IMAGE:latest" \
|
||||||
|
.
|
||||||
|
docker push "$REGISTRY/$IMAGE:${{ github.sha }}"
|
||||||
|
docker push "$REGISTRY/$IMAGE:latest"
|
||||||
|
|
||||||
- name: Deploy to k3s
|
- name: Deploy to k3s
|
||||||
|
env:
|
||||||
|
KUBECONFIG_B64: ${{ secrets.KUBECONFIG_B64 }}
|
||||||
run: |
|
run: |
|
||||||
echo "Deploy step - skipped until runner kubectl is configured"
|
set -euo pipefail
|
||||||
|
|
||||||
|
if [ -z "${KUBECONFIG_B64:-}" ]; then
|
||||||
|
echo "KUBECONFIG_B64 secret is required" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
KUBECONFIG_FILE="$(mktemp)"
|
||||||
|
trap 'rm -f "$KUBECONFIG_FILE" /tmp/kubectl' EXIT
|
||||||
|
echo "$KUBECONFIG_B64" | base64 -d > "$KUBECONFIG_FILE"
|
||||||
|
chmod 600 "$KUBECONFIG_FILE"
|
||||||
|
|
||||||
|
KUBECTL_VERSION="v1.32.0"
|
||||||
|
curl -fsSLo /tmp/kubectl "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl"
|
||||||
|
chmod +x /tmp/kubectl
|
||||||
|
|
||||||
|
if /tmp/kubectl --kubeconfig "$KUBECONFIG_FILE" \
|
||||||
|
auth can-i get ingresses -n "$K8S_NAMESPACE" >/dev/null 2>&1; then
|
||||||
|
ingress_hosts="$(/tmp/kubectl --kubeconfig "$KUBECONFIG_FILE" \
|
||||||
|
get ingress "$K8S_INGRESS" -n "$K8S_NAMESPACE" \
|
||||||
|
--ignore-not-found \
|
||||||
|
-o jsonpath='{range .spec.rules[*]}{.host}{"\n"}{end}{range .spec.tls[*].hosts[*]}{.}{"\n"}{end}')"
|
||||||
|
printf '%s\n' "$ingress_hosts" | grep -Fx "$TEST_HOST" >/dev/null
|
||||||
|
if printf '%s\n' "$ingress_hosts" | grep -Fx "$PROD_HOST" >/dev/null; then
|
||||||
|
echo "Refusing deploy: ingress contains production host $PROD_HOST" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "Kubeconfig cannot read ingresses; relying on K8S_NAMESPACE/K8S_DEPLOYMENT scope: $K8S_NAMESPACE/$K8S_DEPLOYMENT"
|
||||||
|
fi
|
||||||
|
|
||||||
|
/tmp/kubectl --kubeconfig "$KUBECONFIG_FILE" \
|
||||||
|
set image "deployment/$K8S_DEPLOYMENT" -n "$K8S_NAMESPACE" \
|
||||||
|
"$K8S_CONTAINER=$REGISTRY/$IMAGE:${{ github.sha }}"
|
||||||
|
/tmp/kubectl --kubeconfig "$KUBECONFIG_FILE" \
|
||||||
|
rollout status "deployment/$K8S_DEPLOYMENT" -n "$K8S_NAMESPACE" --timeout=120s
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ spec:
|
|||||||
labels:
|
labels:
|
||||||
app: hugo-blog
|
app: hugo-blog
|
||||||
spec:
|
spec:
|
||||||
|
imagePullSecrets:
|
||||||
|
- name: gitea-registry
|
||||||
containers:
|
containers:
|
||||||
- name: nginx
|
- name: nginx
|
||||||
image: git.luochen570.cn/luochen570/hugo-blog:latest
|
image: git.luochen570.cn/luochen570/hugo-blog:latest
|
||||||
|
|||||||
Reference in New Issue
Block a user