forked from mtusi2026/mtusi2026lab6
195 lines
7.7 KiB
YAML
195 lines
7.7 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: 45
|
|
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: mtusi2026lab5-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
|
|
API_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab5-api:${{ github.sha }}
|
|
NGINX_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab5-nginx:${{ github.sha }}
|
|
SWAGGER_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab5-swagger:${{ github.sha }}
|
|
PROMETHEUS_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab5-prometheus:${{ github.sha }}
|
|
GRAFANA_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab5-grafana:${{ github.sha }}
|
|
S3_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab5-minio:${{ github.sha }}
|
|
S3_BOOTSTRAP_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab5-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-file: go.mod
|
|
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)"
|
|
sh -n infra/minio/bootstrap.sh scripts/restart.sh scripts/rolling-update.sh scripts/stop-one.sh scripts/test-lab5.sh scripts/test-lab5-acceptance.sh
|
|
|
|
- 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: |
|
|
cp lab5.env.example .env
|
|
docker compose config --quiet
|
|
docker compose up -d --build --scale api=2 --wait --wait-timeout 150
|
|
docker network connect "${COMPOSE_PROJECT_NAME}_application" "$HOSTNAME"
|
|
docker network connect "${COMPOSE_PROJECT_NAME}_data" "$HOSTNAME"
|
|
|
|
- name: Integration checks
|
|
run: |
|
|
set -a
|
|
. ./.env
|
|
set +a
|
|
test "$(curl --noproxy '*' -s -o /dev/null -w '%{http_code}' "http://s3:9000/$S3_BUCKET")" = "403"
|
|
make test BASE_URL=http://nginx:8080
|
|
docker compose --profile tests run --rm --build tests
|
|
go run ./cmd/dependencycheck -base-url=http://nginx:8080
|
|
make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=http://nginx:8080
|
|
make test-failover ACTION_SCRIPT=scripts/stop-one.sh BASE_URL=http://nginx:8080
|
|
make test-lab5-acceptance
|
|
make test-lab5
|
|
|
|
- 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 .env ]; then docker compose logs --no-color --tail=200; fi
|
|
|
|
- name: Stop local stack
|
|
if: always()
|
|
run: |
|
|
docker network disconnect "${COMPOSE_PROJECT_NAME}_application" "$HOSTNAME" 2>/dev/null || true
|
|
docker network disconnect "${COMPOSE_PROJECT_NAME}_data" "$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/mtusi2026lab5-${{ github.run_id }}.env"
|
|
|
|
- name: Deploy and verify
|
|
run: |
|
|
ssh "$DEPLOY_USER@$DEPLOY_HOST" "
|
|
set -eu
|
|
cd ~/mtusi2026lab5
|
|
git fetch origin main
|
|
git cat-file -e '$DEPLOY_SHA^{commit}'
|
|
git reset --hard '$DEPLOY_SHA'
|
|
manifest='/tmp/mtusi2026lab5-${{ 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
|
|
"
|