Files
ins-landing-page/.gitea/workflows/ci.yml
T
ngqileandClaude Opus 5.5 91b6b31644 chore: k8s manifests and Gitea CI for ins-landing.containerverse.dev
Modelled on dynamic-qr: Traefik Gateway with Let's Encrypt, Longhorn PVC,
1 replica with Recreate, Calico network policies, and a rate limit on
POST /api/leads. CI runs the tests in a Dockerfile stage before pushing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfmHGGiYUjNcvJoyzdf
2026-12-01 11:24:33 +08:00

191 lines
8.7 KiB
YAML

name: CI
# Image build for ins-landing-page, following dynamic-qr's pipeline (build container, DinD, one
# moving tag per profile, per-branch run queue, guarded microk8s rollout, notifications). The k8s
# manifests live in k8s/ and are applied by hand (README "Kubernetes"); CI only rolls the
# Deployment so it pulls the new image, and only from main. Tests run inside the Dockerfile's
# `test` stage, so a failing test stops the image from being pushed.
#
# Tags: main -> ins-landing-page-prd-latest; dev -> ins-landing-page-dev-latest.
on:
push:
branches:
- main
- dev
# Do NOT add workflow `concurrency`: Gitea keeps one waiting run per group and cancels the rest.
# Runs queue per branch in the "Wait for older runs" step instead (scripts/waitForOlderRuns.sh).
jobs:
build:
runs-on: ubuntu-latest
container:
image: gitea.containerverse.dev/solvway/maven-custom-21:latest
credentials:
username: ${{ secrets.PACKAGE_USER }}
password: ${{ secrets.PACKAGE_RW_KEY }}
env:
_REGISTRY: gitea.containerverse.dev
_REGISTRY_PREFIX: solvway
_REPO_NAME: ins-landing-page
_IMAGE_NAME: ins-landing-page
# Images are built against the DinD sidecar (no host docker socket), without TLS.
DOCKER_HOST: tcp://docker:2375
DOCKER_TLS_VERIFY: ""
# Push a single manifest, not a manifest list with a provenance attestation.
BUILDX_NO_DEFAULT_ATTESTATIONS: "1"
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Wait for older runs of this branch
timeout-minutes: 125
env:
GITEA_TOKEN: ${{ github.token }}
GITEA_API: ${{ github.server_url }}/api/v1
GITEA_REPO: ${{ github.repository }}
GITEA_BRANCH: ${{ github.ref_name }}
RUN_NUMBER: ${{ github.run_number }}
# Values reach scripts through env:, never as ${{ }} inside run:.
run: |
"$GITHUB_WORKSPACE/scripts/waitForOlderRuns.sh" "$RUN_NUMBER"
- name: Docker build & push
env:
REGISTRY_USER: ${{ secrets.PACKAGE_USER }}
REGISTRY_PASSWORD: ${{ secrets.PACKAGE_RW_KEY }}
BRANCH: ${{ github.ref_name }}
run: |
case "$BRANCH" in
main) PROFILE=prd ;;
dev) PROFILE=dev ;;
*) echo "Refusing unexpected branch name"; exit 1 ;;
esac
IMAGE_REF="$_REGISTRY/$_REGISTRY_PREFIX/$_IMAGE_NAME:$_REPO_NAME-$PROFILE-latest"
printf '%s' "$REGISTRY_PASSWORD" | docker login "$_REGISTRY" -u "$REGISTRY_USER" --password-stdin
docker build --pull -f Dockerfile --target test .
docker build -f Dockerfile -t "$IMAGE_REF" .
docker push "$IMAGE_REF"
echo "Pushed $IMAGE_REF"
- name: Rollout to microk8s
if: ${{ github.ref_name == 'main' }}
env:
DEPLOY_USER: ${{ secrets.DRONECICD_SSH_USERNAME }}
DEPLOY_HOST: ${{ secrets.DRONECICD_SSH_HOST }}
DEPLOY_SSH_KEY: ${{ secrets.DRONECICD_SSH_KEY }}
SHA: ${{ github.sha }}
# Same guard as babyboo's emitGuardedRollout.sh: annotate first, and skip when the
# Deployment already runs a newer commit (by committer time), so an older run never rolls
# back a newer image. A missing Deployment (manifests not applied yet) is skipped, not failed.
run: |
case "$SHA" in *[!0-9a-f]*|'') echo "Refusing unexpected sha"; exit 1 ;; esac
COMMIT_TIME="$(git log -1 --format=%ct "$SHA")"
case "$COMMIT_TIME" in ''|*[!0-9]*) echo "cannot resolve the commit time of $SHA"; exit 1 ;; esac
mkdir -p /root/.ssh && chmod 700 /root/.ssh
printf '%s' "$DEPLOY_SSH_KEY" > /root/.ssh/id_rsa
chmod 600 /root/.ssh/id_rsa
ssh -i /root/.ssh/id_rsa -o StrictHostKeyChecking=no "${DEPLOY_USER}@${DEPLOY_HOST}" 'bash -s' <<EOF
set -e
k() { PATH="\$PATH:/snap/bin" microk8s kubectl "\$@"; }
if ! k -n ins-landing-page get deployment/ins-landing-page >/dev/null 2>&1; then
echo "ins-landing-page/ins-landing-page not found: apply k8s/ first (README). Skipping rollout."
exit 0
fi
cur=\$(k -n ins-landing-page get deployment/ins-landing-page -o 'go-template={{with .metadata.annotations}}{{index . "ins-landing-page/deployed-commit-time"}}{{end}}')
case "\$cur" in ''|*[!0-9]*) cur=0 ;; esac
if [ "\$cur" -gt $COMMIT_TIME ]; then
echo "Skip: ins-landing-page runs a newer commit (commit time \$cur > $COMMIT_TIME)"
else
k -n ins-landing-page annotate --overwrite deployment/ins-landing-page ins-landing-page/deployed-commit-time=$COMMIT_TIME ins-landing-page/deployed-commit=$SHA
./krrd.sh ins-landing-page ins-landing-page
fi
EOF
- name: Telegram notification
if: always()
# Uses github.actor (the authenticated pusher) — always populated, so the
# "Commit by <blank>" problem from the Drone setup cannot happen here.
env:
TG_TOKEN: ${{ secrets.TELEGRAM_TOKEN }}
TG_TO: ${{ secrets.TELEGRAM_TO }}
BUILD_STATUS: ${{ job.status }}
ACTOR: ${{ github.actor }}
REPO: ${{ github.repository }}
BRANCH: ${{ github.ref_name }}
RUN_NUMBER: ${{ github.run_number }}
COMMIT_MSG: ${{ github.event.head_commit.message }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
if [ "$BUILD_STATUS" = "success" ]; then
HEAD="✅ Build #${RUN_NUMBER} of \`${REPO}\` succeeded."
else
HEAD="❌ Build #${RUN_NUMBER} of \`${REPO}\` failed."
fi
TEXT="$(printf '%s\n\n📝 Pushed by %s on `%s`:\n\n```\n%s\n```\n\n🌐 %s' \
"$HEAD" "$ACTOR" "$BRANCH" "$COMMIT_MSG" "$RUN_URL")"
curl -sS "https://api.telegram.org/bot${TG_TOKEN}/sendMessage" \
--data-urlencode "chat_id=${TG_TO}" \
--data-urlencode "parse_mode=Markdown" \
--data-urlencode "text=${TEXT}" >/dev/null
- name: Microsoft Teams notification
if: always()
# Independent of the Telegram step: both run on every build regardless of
# the other's outcome. Posts an Adaptive Card, which both Teams Workflows
# webhooks and legacy incoming webhooks accept.
env:
TEAMS_WEBHOOK_URL: ${{ secrets.MICROSOFT_TEAMS_WEBHOOK_URL }}
BUILD_STATUS: ${{ job.status }}
ACTOR: ${{ github.actor }}
REPO: ${{ github.repository }}
BRANCH: ${{ github.ref_name }}
RUN_NUMBER: ${{ github.run_number }}
COMMIT_MSG: ${{ github.event.head_commit.message }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
if [ -z "$TEAMS_WEBHOOK_URL" ]; then
echo "MICROSOFT_TEAMS_WEBHOOK_URL not set — skipping."
exit 0
fi
if [ "$BUILD_STATUS" = "success" ]; then
HEAD="✅ Build #${RUN_NUMBER} of ${REPO} succeeded."
COLOR="Good"
else
HEAD="❌ Build #${RUN_NUMBER} of ${REPO} failed."
COLOR="Attention"
fi
# JSON-escape without relying on jq being in the build image.
json_escape() {
printf '%s' "$1" | tr -d '\r' \
| sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e 's/\t/\\t/g' \
| awk 'NR > 1 { printf "\\n" } { printf "%s", $0 }'
}
BODY="$(cat <<JSON
{
"type": "message",
"attachments": [{
"contentType": "application/vnd.microsoft.card.adaptive",
"content": {
"\$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.4",
"body": [
{ "type": "TextBlock", "text": "$(json_escape "$HEAD")", "weight": "Bolder", "size": "Medium", "color": "$COLOR", "wrap": true },
{ "type": "TextBlock", "text": "📝 Pushed by $(json_escape "$ACTOR") on $(json_escape "$BRANCH"):", "wrap": true },
{ "type": "TextBlock", "text": "$(json_escape "$COMMIT_MSG")", "fontType": "Monospace", "wrap": true }
],
"actions": [
{ "type": "Action.OpenUrl", "title": "🌐 View run", "url": "$(json_escape "$RUN_URL")" }
]
}
}]
}
JSON
)"
HTTP_CODE="$(curl -sS -o /dev/null -w '%{http_code}' -X POST \
-H 'Content-Type: application/json' \
--data-binary "$BODY" "$TEAMS_WEBHOOK_URL")" || true
echo "Teams webhook responded HTTP ${HTTP_CODE}."