forked from mtusi2026/mtusi2026lab7
232 lines
11 KiB
YAML
232 lines
11 KiB
YAML
name: Deploy production
|
|
|
|
on:
|
|
workflow_dispatch:
|
|
|
|
concurrency:
|
|
group: production
|
|
cancel-in-progress: false
|
|
|
|
jobs:
|
|
verify:
|
|
name: Verify selected commit
|
|
if: github.ref == 'refs/heads/main'
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 60
|
|
outputs:
|
|
api_image: ${{ steps.publish.outputs.api_image }}
|
|
nginx_image: ${{ steps.publish.outputs.nginx_image }}
|
|
swagger_image: ${{ steps.publish.outputs.swagger_image }}
|
|
prometheus_image: ${{ steps.publish.outputs.prometheus_image }}
|
|
grafana_image: ${{ steps.publish.outputs.grafana_image }}
|
|
s3_image: ${{ steps.publish.outputs.s3_image }}
|
|
s3_bootstrap_image: ${{ steps.publish.outputs.s3_bootstrap_image }}
|
|
env:
|
|
COMPOSE_PROJECT_NAME: mtusi2026lab7-deploy-${{ github.run_id }}
|
|
COMPOSE_FILE: compose.yaml:deploy/compose.images.yaml
|
|
API_BIND_ADDRESS: 127.0.0.1:0
|
|
SWAGGER_BIND_ADDRESS: 127.0.0.1:0
|
|
PROMETHEUS_BIND_ADDRESS: 127.0.0.1:0
|
|
GRAFANA_BIND_ADDRESS: 127.0.0.1:0
|
|
LOKI_BIND_ADDRESS: 127.0.0.1:0
|
|
TEMPO_BIND_ADDRESS: 127.0.0.1:0
|
|
OPENBAO_BIND_ADDRESS: 127.0.0.1:0
|
|
BAO_ADDR: https://openbao:8200
|
|
DOCKER_WORKSPACE_MIRROR: "1"
|
|
CI_WORKSPACE: /tmp/mtusi2026lab7-${{ github.run_id }}
|
|
API_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab7-api:${{ github.sha }}
|
|
NGINX_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab7-nginx:${{ github.sha }}
|
|
SWAGGER_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab7-swagger:${{ github.sha }}
|
|
PROMETHEUS_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab7-prometheus:${{ github.sha }}
|
|
GRAFANA_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab7-grafana:${{ github.sha }}
|
|
S3_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab7-minio:${{ github.sha }}
|
|
S3_BOOTSTRAP_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab7-minio-client:${{ github.sha }}
|
|
steps:
|
|
- name: Checkout exact commit
|
|
run: |
|
|
git init .
|
|
git remote add origin https://gitea.privcloud.tech/${{ github.repository }}.git
|
|
git fetch --depth=1 origin ${{ github.sha }}
|
|
git checkout --detach FETCH_HEAD
|
|
|
|
- uses: actions/setup-go@v5
|
|
with:
|
|
go-version: "1.26.10"
|
|
cache: false
|
|
|
|
- name: Check contract
|
|
run: make check
|
|
|
|
- name: Validate OpenAPI
|
|
run: |
|
|
python3 -m venv .venv
|
|
.venv/bin/python -m pip install -r requirements-dev.txt
|
|
make validate PYTHON=.venv/bin/python
|
|
|
|
- name: Check formatting and shell scripts
|
|
run: |
|
|
test -z "$(gofmt -l cmd internal migrations)"
|
|
for file in infra/minio/*.sh infra/pki/*.sh infra/postgres/*.sh infra/redis/*.sh infra/security/*.sh infra/openbao/*.sh scripts/*.sh; do sh -n "$file"; done
|
|
python3 -m py_compile scripts/lab6-traffic.py scripts/lab6-observe.py scripts/lab6-alert.py scripts/lab6-correlation.py scripts/validate-lab6-evidence.py scripts/test-login-rate-limit.py
|
|
|
|
- name: Lint
|
|
uses: golangci/golangci-lint-action@v7
|
|
with:
|
|
version: v2.13.2
|
|
args: --timeout=5m
|
|
skip-cache: true
|
|
|
|
- name: Unit tests and build
|
|
run: |
|
|
go test -race ./...
|
|
go build ./cmd/...
|
|
|
|
- name: Prepare local stack
|
|
run: |
|
|
mkdir -p "$CI_WORKSPACE"
|
|
git archive HEAD | tar -x -C "$CI_WORKSPACE"
|
|
cd "$CI_WORKSPACE"
|
|
cp lab7.env.example .env
|
|
docker compose config --quiet
|
|
./infra/pki/bootstrap.sh
|
|
./scripts/openbao-seed.sh .env
|
|
./scripts/sync-docker-workspace.sh
|
|
docker compose up -d security-bootstrap openbao-permissions openbao
|
|
docker network connect "${COMPOSE_PROJECT_NAME}_secrets" "$HOSTNAME"
|
|
./scripts/bootstrap-local.sh .env
|
|
docker network connect "${COMPOSE_PROJECT_NAME}_application" "$HOSTNAME"
|
|
docker network connect "${COMPOSE_PROJECT_NAME}_data" "$HOSTNAME"
|
|
docker network connect "${COMPOSE_PROJECT_NAME}_data_backend" "$HOSTNAME"
|
|
docker network connect "${COMPOSE_PROJECT_NAME}_monitoring" "$HOSTNAME"
|
|
docker network connect "${COMPOSE_PROJECT_NAME}_secure_clients" "$HOSTNAME"
|
|
docker network connect "${COMPOSE_PROJECT_NAME}_telemetry_storage" "$HOSTNAME"
|
|
|
|
- name: Integration checks
|
|
run: |
|
|
cd "$CI_WORKSPACE"
|
|
test "$(curl --noproxy '*' --cacert .runtime/security/internal-ca/ca.crt -s -o /dev/null -w '%{http_code}' "https://s3:9000/messenger-attachments")" = "403"
|
|
make test BASE_URL=https://nginx:8443 CA_FILE=.runtime/security/public-ca/ca.crt
|
|
make test-security BASE_URL=https://nginx:8443 CA_FILE=.runtime/security/public-ca/ca.crt
|
|
make test-tls-matrix ENV_FILE=.env
|
|
ENV_FILE=.env BASE_URL=https://nginx:8443 ./scripts/test-leaf-rotation.sh
|
|
docker compose --profile security run --rm dependency-tests
|
|
ENV_FILE=.env BAO_ADDR=https://openbao:8200 BAO_CACERT="$PWD/.runtime/security/openbao/ca.crt" ./scripts/openbao-test-policy.sh
|
|
ENV_FILE=.env BAO_ADDR=https://openbao:8200 BAO_CACERT="$PWD/.runtime/security/openbao/ca.crt" ./scripts/test-openbao-lifecycle.sh
|
|
make test-login-rate-limit BASE_URL=https://nginx:8443 CA_FILE=.runtime/security/public-ca/ca.crt
|
|
docker compose --profile tests run --rm --build tests
|
|
go run ./cmd/dependencycheck -base-url=https://nginx:8443 -ca-file=.runtime/security/public-ca/ca.crt
|
|
make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=https://nginx:8443 CA_FILE=.runtime/security/public-ca/ca.crt
|
|
make test-failover ACTION_SCRIPT=scripts/stop-one.sh BASE_URL=https://nginx:8443 CA_FILE=.runtime/security/public-ca/ca.crt
|
|
make test-lab5-acceptance
|
|
make test-worker-compromise ENV_FILE=.env
|
|
ENV_FILE=.env CA_FILE=.runtime/security/public-ca/ca.crt PROMETHEUS_URL=http://prometheus:9090 TEMPO_URL=http://tempo:3200 LOKI_URL=http://loki:3100 make test-lab6-smoke BASE_URL=https://nginx:8443
|
|
mkdir -p evidence/runtime
|
|
api_restart_counts=$(docker inspect --format '{{.RestartCount}}' $(docker compose ps -q api))
|
|
go run ./cmd/loginburst -base-url=https://nginx:8443 -ca-file=.runtime/security/public-ca/ca.crt > evidence/runtime/login-burst.json
|
|
test "$(docker inspect --format '{{.RestartCount}}' $(docker compose ps -q api))" = "$api_restart_counts"
|
|
|
|
- name: Publish verified images
|
|
id: publish
|
|
env:
|
|
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
|
|
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
|
|
run: |
|
|
printf '%s' "$REGISTRY_PASSWORD" | docker login gitea.privcloud.tech --username "$REGISTRY_USERNAME" --password-stdin
|
|
|
|
publish() {
|
|
output=$1
|
|
image=$2
|
|
push_output=$(docker push "$image")
|
|
printf '%s\n' "$push_output"
|
|
digest=$(printf '%s\n' "$push_output" | sed -n 's/^.*digest: \(sha256:[0-9a-f]*\).*$/\1/p' | tail -n 1)
|
|
test -n "$digest"
|
|
repository=${image%:*}
|
|
printf '%s=%s@%s\n' "$output" "$repository" "$digest" >> "$GITHUB_OUTPUT"
|
|
}
|
|
|
|
publish api_image "$API_IMAGE"
|
|
publish nginx_image "$NGINX_IMAGE"
|
|
publish swagger_image "$SWAGGER_IMAGE"
|
|
publish prometheus_image "$PROMETHEUS_IMAGE"
|
|
publish grafana_image "$GRAFANA_IMAGE"
|
|
publish s3_image "$S3_IMAGE"
|
|
publish s3_bootstrap_image "$S3_BOOTSTRAP_IMAGE"
|
|
|
|
- name: Container logs
|
|
if: failure()
|
|
run: if [ -f "$CI_WORKSPACE/.env" ]; then cd "$CI_WORKSPACE"; docker compose logs --no-color --tail=200; fi
|
|
|
|
- name: Stop local stack
|
|
if: always()
|
|
run: |
|
|
test -d "$CI_WORKSPACE" || exit 0
|
|
cd "$CI_WORKSPACE"
|
|
trap './scripts/sync-docker-workspace.sh --clean' EXIT
|
|
docker network disconnect "${COMPOSE_PROJECT_NAME}_secrets" "$HOSTNAME" 2>/dev/null || true
|
|
docker network disconnect "${COMPOSE_PROJECT_NAME}_application" "$HOSTNAME" 2>/dev/null || true
|
|
docker network disconnect "${COMPOSE_PROJECT_NAME}_data" "$HOSTNAME" 2>/dev/null || true
|
|
docker network disconnect "${COMPOSE_PROJECT_NAME}_data_backend" "$HOSTNAME" 2>/dev/null || true
|
|
docker network disconnect "${COMPOSE_PROJECT_NAME}_monitoring" "$HOSTNAME" 2>/dev/null || true
|
|
docker network disconnect "${COMPOSE_PROJECT_NAME}_secure_clients" "$HOSTNAME" 2>/dev/null || true
|
|
docker network disconnect "${COMPOSE_PROJECT_NAME}_telemetry_storage" "$HOSTNAME" 2>/dev/null || true
|
|
if [ -f .env ]; then docker compose --profile tests down -v --remove-orphans; fi
|
|
|
|
deploy:
|
|
name: Deploy selected commit
|
|
needs: verify
|
|
runs-on: ubuntu-latest
|
|
environment: production
|
|
env:
|
|
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
|
|
DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
|
|
DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
|
|
DEPLOY_KNOWN_HOSTS: ${{ secrets.DEPLOY_KNOWN_HOSTS }}
|
|
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
|
|
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
|
|
DEPLOY_SHA: ${{ github.sha }}
|
|
steps:
|
|
- name: Configure SSH
|
|
run: |
|
|
umask 077
|
|
mkdir -p ~/.ssh
|
|
printf '%s\n' "$DEPLOY_SSH_KEY" > ~/.ssh/id_ed25519
|
|
printf '%s\n' "$DEPLOY_KNOWN_HOSTS" > ~/.ssh/known_hosts
|
|
|
|
- name: Prepare immutable image manifest
|
|
run: |
|
|
cat > deploy-images.env <<EOF
|
|
API_IMAGE=${{ needs.verify.outputs.api_image }}
|
|
NGINX_IMAGE=${{ needs.verify.outputs.nginx_image }}
|
|
SWAGGER_IMAGE=${{ needs.verify.outputs.swagger_image }}
|
|
PROMETHEUS_IMAGE=${{ needs.verify.outputs.prometheus_image }}
|
|
GRAFANA_IMAGE=${{ needs.verify.outputs.grafana_image }}
|
|
S3_IMAGE=${{ needs.verify.outputs.s3_image }}
|
|
S3_BOOTSTRAP_IMAGE=${{ needs.verify.outputs.s3_bootstrap_image }}
|
|
EOF
|
|
|
|
- name: Authenticate registry on deployment host
|
|
run: printf '%s' "$REGISTRY_PASSWORD" | ssh "$DEPLOY_USER@$DEPLOY_HOST" "docker login gitea.privcloud.tech --username '$REGISTRY_USERNAME' --password-stdin"
|
|
|
|
- name: Upload image manifest
|
|
run: scp deploy-images.env "$DEPLOY_USER@$DEPLOY_HOST:/tmp/mtusi2026lab7-${{ github.run_id }}.env"
|
|
|
|
- name: Deploy and verify
|
|
run: |
|
|
ssh "$DEPLOY_USER@$DEPLOY_HOST" "
|
|
set -eu
|
|
cd ~/mtusi2026lab7
|
|
git fetch origin main
|
|
git cat-file -e '$DEPLOY_SHA^{commit}'
|
|
git reset --hard '$DEPLOY_SHA'
|
|
manifest='/tmp/mtusi2026lab7-${{ github.run_id }}.env'
|
|
if DEPLOY_GENERATION='${{ github.run_id }}' scripts/rolling-update.sh \"\$manifest\"; then
|
|
rm -f \"\$manifest\"
|
|
else
|
|
status=\$?
|
|
rm -f \"\$manifest\"
|
|
exit \"\$status\"
|
|
fi
|
|
test \"\$(git rev-parse HEAD)\" = '$DEPLOY_SHA'
|
|
docker compose ps
|
|
"
|