239 lines
8.4 KiB
YAML
239 lines
8.4 KiB
YAML
name: CI and deployment
|
|
|
|
on:
|
|
pull_request:
|
|
push:
|
|
branches: [production]
|
|
workflow_dispatch:
|
|
|
|
permissions:
|
|
contents: read
|
|
|
|
jobs:
|
|
backend:
|
|
runs-on: ubuntu-latest
|
|
defaults:
|
|
run:
|
|
working-directory: api
|
|
env:
|
|
APP_ENV: testing
|
|
SQLALCHEMY_DATABASE_URI: sqlite:///:memory:
|
|
SECRET_KEY: ci-test-key
|
|
REGISTRATION_MODE: open
|
|
EMAIL_DELIVERY_ENABLED: "false"
|
|
RATELIMIT_ENABLED: "false"
|
|
steps:
|
|
- uses: actions/checkout@v5
|
|
- uses: actions/setup-python@v6
|
|
with:
|
|
python-version: "3.12"
|
|
- run: python -m pip install -r config/requirements-dev.txt
|
|
- run: pytest test_contact_independent.py
|
|
- run: pytest --cov --cov-report=term-missing
|
|
|
|
backend-mysql:
|
|
runs-on: ubuntu-latest
|
|
defaults:
|
|
run:
|
|
working-directory: api
|
|
services:
|
|
mysql:
|
|
image: mysql:8.4
|
|
env:
|
|
MYSQL_DATABASE: app_test
|
|
MYSQL_USER: app_test
|
|
MYSQL_PASSWORD: test-password
|
|
MYSQL_ROOT_PASSWORD: root-test-password
|
|
ports: ["3306:3306"]
|
|
options: >-
|
|
--health-cmd="mysqladmin ping -h localhost -uroot -proot-test-password --silent"
|
|
--health-interval=10s
|
|
--health-timeout=5s
|
|
--health-retries=10
|
|
env:
|
|
APP_ENV: testing
|
|
SQLALCHEMY_DATABASE_URI: mysql+pymysql://app_test:test-password@127.0.0.1:3306/app_test
|
|
SECRET_KEY: ci-test-key
|
|
REGISTRATION_MODE: open
|
|
EMAIL_DELIVERY_ENABLED: "false"
|
|
RATELIMIT_ENABLED: "false"
|
|
steps:
|
|
- uses: actions/checkout@v5
|
|
- uses: actions/setup-python@v6
|
|
with:
|
|
python-version: "3.12"
|
|
- run: python -m pip install -r config/requirements-dev.txt
|
|
- run: flask --app api.py db upgrade
|
|
- run: pytest
|
|
|
|
frontend:
|
|
runs-on: ubuntu-latest
|
|
defaults:
|
|
run:
|
|
working-directory: react
|
|
steps:
|
|
- uses: actions/checkout@v5
|
|
- uses: actions/setup-node@v5
|
|
with:
|
|
node-version: "22"
|
|
cache: npm
|
|
cache-dependency-path: react/package-lock.json
|
|
- run: npm ci
|
|
- run: npm run lint
|
|
- run: npm test
|
|
- run: npm run build
|
|
- run: npx playwright install --with-deps chromium
|
|
- run: npm run test:e2e
|
|
|
|
publish-images:
|
|
if: github.ref == 'refs/heads/production' && (github.event_name == 'push' || github.event_name == 'workflow_dispatch')
|
|
needs: [backend, backend-mysql, frontend]
|
|
runs-on: ubuntu-latest
|
|
permissions:
|
|
contents: read
|
|
packages: write
|
|
outputs:
|
|
api_image: ${{ steps.images.outputs.api }}
|
|
nginx_image: ${{ steps.images.outputs.nginx }}
|
|
steps:
|
|
- uses: actions/checkout@v5
|
|
- uses: docker/setup-buildx-action@v4
|
|
- id: images
|
|
name: Resolve immutable image names
|
|
shell: bash
|
|
run: |
|
|
owner="$(printf '%s' "$GITHUB_REPOSITORY_OWNER" | tr '[:upper:]' '[:lower:]')"
|
|
repository="${GITHUB_REPOSITORY#*/}"
|
|
printf 'api=ghcr.io/%s/%s-api:%s\n' "$owner" "$repository" "$GITHUB_SHA" >> "$GITHUB_OUTPUT"
|
|
printf 'nginx=ghcr.io/%s/%s-nginx:%s\n' "$owner" "$repository" "$GITHUB_SHA" >> "$GITHUB_OUTPUT"
|
|
- uses: docker/login-action@v4
|
|
with:
|
|
registry: ghcr.io
|
|
username: ${{ github.actor }}
|
|
password: ${{ secrets.GITHUB_TOKEN }}
|
|
- uses: docker/build-push-action@v7
|
|
with:
|
|
context: ./api
|
|
file: ./api/Dockerfile
|
|
push: true
|
|
tags: ${{ steps.images.outputs.api }}
|
|
labels: |
|
|
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
|
|
org.opencontainers.image.revision=${{ github.sha }}
|
|
cache-from: type=gha,scope=api
|
|
cache-to: type=gha,mode=max,scope=api
|
|
- uses: docker/build-push-action@v7
|
|
with:
|
|
context: .
|
|
file: ./nginx/Dockerfile
|
|
push: true
|
|
tags: ${{ steps.images.outputs.nginx }}
|
|
labels: |
|
|
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
|
|
org.opencontainers.image.revision=${{ github.sha }}
|
|
cache-from: type=gha,scope=nginx
|
|
cache-to: type=gha,mode=max,scope=nginx
|
|
- name: Scan API image
|
|
uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0
|
|
with:
|
|
image-ref: ${{ steps.images.outputs.api }}
|
|
format: table
|
|
exit-code: "1"
|
|
ignore-unfixed: true
|
|
severity: CRITICAL,HIGH
|
|
- name: Scan nginx image
|
|
uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0
|
|
with:
|
|
image-ref: ${{ steps.images.outputs.nginx }}
|
|
format: table
|
|
exit-code: "1"
|
|
ignore-unfixed: true
|
|
severity: CRITICAL,HIGH
|
|
|
|
deploy-production:
|
|
if: github.ref == 'refs/heads/production' && (github.event_name == 'push' || github.event_name == 'workflow_dispatch')
|
|
needs: [publish-images]
|
|
runs-on: ubuntu-latest
|
|
environment: production
|
|
concurrency:
|
|
group: production
|
|
cancel-in-progress: false
|
|
timeout-minutes: 20
|
|
steps:
|
|
- name: Validate deployment configuration
|
|
env:
|
|
SSH_HOST: ${{ secrets.PRODUCTION_SSH_HOST }}
|
|
SSH_USER: ${{ secrets.PRODUCTION_SSH_USER }}
|
|
SSH_PRIVATE_KEY: ${{ secrets.PRODUCTION_SSH_PRIVATE_KEY }}
|
|
SSH_KNOWN_HOSTS: ${{ secrets.PRODUCTION_SSH_KNOWN_HOSTS }}
|
|
DEPLOY_PATH: ${{ vars.PRODUCTION_DEPLOY_PATH }}
|
|
ENV_PATH: ${{ vars.PRODUCTION_ENV_PATH }}
|
|
PUBLIC_URL: ${{ vars.PRODUCTION_PUBLIC_URL }}
|
|
run: |
|
|
test -n "$SSH_HOST"
|
|
test -n "$SSH_USER"
|
|
test -n "$SSH_PRIVATE_KEY"
|
|
test -n "$SSH_KNOWN_HOSTS"
|
|
test -n "$DEPLOY_PATH"
|
|
test -n "$ENV_PATH"
|
|
test -n "$PUBLIC_URL"
|
|
- name: Configure SSH
|
|
env:
|
|
SSH_PRIVATE_KEY: ${{ secrets.PRODUCTION_SSH_PRIVATE_KEY }}
|
|
SSH_KNOWN_HOSTS: ${{ secrets.PRODUCTION_SSH_KNOWN_HOSTS }}
|
|
run: |
|
|
install -m 700 -d ~/.ssh
|
|
printf '%s\n' "$SSH_PRIVATE_KEY" > ~/.ssh/production
|
|
chmod 600 ~/.ssh/production
|
|
printf '%s\n' "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
|
|
chmod 600 ~/.ssh/known_hosts
|
|
- name: Deploy tested commit
|
|
env:
|
|
SSH_HOST: ${{ secrets.PRODUCTION_SSH_HOST }}
|
|
SSH_USER: ${{ secrets.PRODUCTION_SSH_USER }}
|
|
TARGET_COMMIT: ${{ github.sha }}
|
|
API_IMAGE: ${{ needs.publish-images.outputs.api_image }}
|
|
NGINX_IMAGE: ${{ needs.publish-images.outputs.nginx_image }}
|
|
DEPLOY_PATH: ${{ vars.PRODUCTION_DEPLOY_PATH }}
|
|
ENV_PATH: ${{ vars.PRODUCTION_ENV_PATH }}
|
|
PUBLIC_URL: ${{ vars.PRODUCTION_PUBLIC_URL }}
|
|
run: |
|
|
ssh -i ~/.ssh/production -o BatchMode=yes -o IdentitiesOnly=yes \
|
|
"$SSH_USER@$SSH_HOST" bash -se -- \
|
|
"$TARGET_COMMIT" "$API_IMAGE" "$NGINX_IMAGE" \
|
|
"$DEPLOY_PATH" "$ENV_PATH" "$PUBLIC_URL" <<'DEPLOY'
|
|
set -Eeuo pipefail
|
|
target_commit="$1"
|
|
api_image="$2"
|
|
nginx_image="$3"
|
|
deploy_path="$4"
|
|
env_path="$5"
|
|
public_url="${6%/}"
|
|
|
|
cd "$deploy_path"
|
|
test "$(git branch --show-current)" = production
|
|
test -z "$(git status --porcelain)"
|
|
test -r "$env_path"
|
|
git fetch --quiet origin production
|
|
git cat-file -e "${target_commit}^{commit}"
|
|
git merge-base --is-ancestor "$target_commit" origin/production
|
|
git merge --ff-only "$target_commit"
|
|
test "$(git rev-parse HEAD)" = "$target_commit"
|
|
|
|
export APP_ENV_FILE="$env_path"
|
|
export API_IMAGE="$api_image"
|
|
export NGINX_IMAGE="$nginx_image"
|
|
compose=(docker compose --env-file "$env_path")
|
|
"${compose[@]}" config --quiet
|
|
"${compose[@]}" pull api nginx
|
|
"${compose[@]}" up -d mysql
|
|
"${compose[@]}" run --rm --no-deps api flask --app api.py db upgrade
|
|
"${compose[@]}" up -d --no-build api nginx
|
|
"${compose[@]}" ps
|
|
|
|
curl --fail --silent --show-error --retry 12 --retry-all-errors \
|
|
--retry-delay 5 --output /dev/null http://127.0.0.1:8081/
|
|
curl --fail --silent --show-error --retry 12 --retry-all-errors \
|
|
--retry-delay 5 --output /dev/null "$public_url/api/health/ready"
|
|
DEPLOY
|