forked from mtusi2026/mtusi2026lab2
ci: verify and deploy lab2
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches: [main]
|
||||
push:
|
||||
branches: [main, solution/bvt2303-plastinin]
|
||||
workflow_dispatch:
|
||||
|
||||
concurrency:
|
||||
group: ci-${{ github.repository }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
verify:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 20
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version-file: go.mod
|
||||
cache: true
|
||||
|
||||
- uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: "3.13"
|
||||
|
||||
- name: Check contract
|
||||
run: make check
|
||||
|
||||
- name: Validate OpenAPI
|
||||
run: |
|
||||
python -m venv .venv
|
||||
.venv/bin/python -m pip install -r requirements-dev.txt
|
||||
make validate PYTHON=.venv/bin/python
|
||||
|
||||
- name: Check formatting
|
||||
run: test -z "$(gofmt -l cmd internal migrations)"
|
||||
|
||||
- name: Lint
|
||||
uses: golangci/golangci-lint-action@v6.5.2
|
||||
with:
|
||||
version: v2.13.2
|
||||
args: --timeout=5m
|
||||
|
||||
- name: Unit tests and build
|
||||
run: |
|
||||
go test -race ./...
|
||||
go build ./cmd/messenger
|
||||
|
||||
- name: Prepare local stack
|
||||
run: |
|
||||
cp lab2.env.example .env
|
||||
./scripts/init-local-secrets.sh
|
||||
docker compose config --quiet
|
||||
docker compose up -d --build --wait --wait-timeout 120
|
||||
|
||||
- name: Public smoke
|
||||
run: make test
|
||||
|
||||
- name: PostgreSQL and Valkey integration tests
|
||||
run: docker compose --profile tests run --rm --build tests
|
||||
|
||||
- name: Dependency outage recovery
|
||||
run: go run ./cmd/dependencycheck
|
||||
|
||||
- name: Persistence test
|
||||
run: make test-persistence ACTION_SCRIPT=scripts/restart.sh
|
||||
|
||||
- name: Container logs
|
||||
if: failure()
|
||||
run: docker compose logs --no-color --tail=200
|
||||
|
||||
- name: Stop local stack
|
||||
if: always()
|
||||
run: docker compose --profile tests down -v --remove-orphans
|
||||
@@ -0,0 +1,123 @@
|
||||
name: Deploy production
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
confirmation:
|
||||
description: Type DEPLOY to release main
|
||||
required: true
|
||||
|
||||
concurrency:
|
||||
group: production
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
if: github.ref == 'refs/heads/main' && inputs.confirmation == 'DEPLOY'
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 20
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version-file: go.mod
|
||||
cache: true
|
||||
|
||||
- uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: "3.13"
|
||||
|
||||
- name: Verify release
|
||||
run: |
|
||||
make check
|
||||
python -m venv .venv
|
||||
.venv/bin/python -m pip install -r requirements-dev.txt
|
||||
make validate PYTHON=.venv/bin/python
|
||||
test -z "$(gofmt -l cmd internal migrations)"
|
||||
go test -race ./...
|
||||
go build ./cmd/messenger
|
||||
|
||||
- name: Lint
|
||||
uses: golangci/golangci-lint-action@v6.5.2
|
||||
with:
|
||||
version: v2.13.2
|
||||
args: --timeout=5m
|
||||
|
||||
- name: Verify release stack
|
||||
run: |
|
||||
cp lab2.env.example .env
|
||||
./scripts/init-local-secrets.sh
|
||||
docker compose up -d --build --wait --wait-timeout 120
|
||||
make test
|
||||
docker compose --profile tests run --rm --build tests
|
||||
go run ./cmd/dependencycheck
|
||||
make test-persistence ACTION_SCRIPT=scripts/restart.sh
|
||||
|
||||
- name: Stop release stack
|
||||
if: always()
|
||||
run: docker compose --profile tests down -v --remove-orphans
|
||||
|
||||
- name: Configure SSH
|
||||
run: |
|
||||
install -d -m 700 ~/.ssh
|
||||
printf '%s\n' '${{ secrets.DEPLOY_SSH_KEY }}' > ~/.ssh/id_ed25519
|
||||
chmod 600 ~/.ssh/id_ed25519
|
||||
printf '%s\n' '${{ secrets.DEPLOY_KNOWN_HOSTS }}' > ~/.ssh/known_hosts
|
||||
|
||||
- name: Pack release
|
||||
run: tar --exclude=.git --exclude=.env --exclude=secrets --exclude=.venv -czf /tmp/mtusi-lab2-${{ github.sha }}.tar.gz .
|
||||
|
||||
- name: Upload release
|
||||
run: scp /tmp/mtusi-lab2-${{ github.sha }}.tar.gz '${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}:/tmp/'
|
||||
|
||||
- name: Deploy exact commit
|
||||
env:
|
||||
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
|
||||
DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
|
||||
RELEASE_SHA: ${{ github.sha }}
|
||||
RELEASE_ATTEMPT: ${{ github.run_attempt }}
|
||||
run: |
|
||||
ssh "$DEPLOY_USER@$DEPLOY_HOST" sh -s -- "$RELEASE_SHA" "$RELEASE_ATTEMPT" <<'REMOTE'
|
||||
set -eu
|
||||
|
||||
release_sha=$1
|
||||
release_attempt=$2
|
||||
deploy_root=/opt/mtusi-lab2
|
||||
release_dir=$deploy_root/releases/$release_sha-$release_attempt
|
||||
archive=/tmp/mtusi-lab2-$release_sha.tar.gz
|
||||
|
||||
mkdir -p "$deploy_root/releases" "$deploy_root/shared/backups"
|
||||
exec 9>"$deploy_root/deploy.lock"
|
||||
flock -x 9
|
||||
|
||||
previous=
|
||||
previous_image=
|
||||
if [ -L "$deploy_root/current" ]; then
|
||||
previous=$(basename "$(readlink "$deploy_root/current")")
|
||||
previous_image=$(cat "$deploy_root/current/.release-sha")
|
||||
cd "$deploy_root/current"
|
||||
docker compose -p mtusi-lab2 exec -T postgres sh -eu -c '
|
||||
export PGPASSWORD="$(cat /run/secrets/postgres_password)"
|
||||
pg_dump --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" --format custom
|
||||
' > "$deploy_root/shared/backups/pre-$release_sha.dump"
|
||||
fi
|
||||
|
||||
mkdir -p "$release_dir"
|
||||
tar -xzf "$archive" -C "$release_dir"
|
||||
printf '%s\n' "$release_sha" > "$release_dir/.release-sha"
|
||||
ln -s "$deploy_root/shared/.env" "$release_dir/.env"
|
||||
ln -s "$deploy_root/shared/secrets" "$release_dir/secrets"
|
||||
|
||||
cd "$release_dir"
|
||||
if ! API_IMAGE="mtusi-messenger:$release_sha" docker compose -p mtusi-lab2 up -d --build --wait --wait-timeout 120; then
|
||||
if [ -n "$previous" ]; then
|
||||
cd "$deploy_root/releases/$previous"
|
||||
API_IMAGE="mtusi-messenger:$previous_image" docker compose -p mtusi-lab2 up -d --wait --wait-timeout 120
|
||||
fi
|
||||
exit 1
|
||||
fi
|
||||
|
||||
ln -sfn "$release_dir" "$deploy_root/current"
|
||||
rm -f "$archive"
|
||||
REMOTE
|
||||
Reference in New Issue
Block a user