Repository navigation
150 lines (145 loc) · 5.62 KB
/
Copy pathproduction.yml
File metadata and controls
150 lines (145 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
name: Production
on:
pull_request:
push:
branches: [master]
workflow_dispatch:
permissions:
contents: read
jobs:
checks:
runs-on: ubuntu-latest
env:
SECRET_KEY: ci-only-not-a-production-secret
# The existing Django tests use HTTP clients and development static storage.
DEBUG: "True"
SITE_URL: https://example.com
ALLOWED_HOSTS: localhost,127.0.0.1,testserver
DATABASE_URL: postgresql://postgres:postgres@127.0.0.1:5432/moscowpythonpro
services:
postgres:
image: postgres:17-alpine
env:
POSTGRES_DB: moscowpythonpro
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
options: >-
--health-cmd "pg_isready -U postgres -d moscowpythonpro"
--health-interval 5s --health-timeout 5s --health-retries 10
steps:
- uses: actions/checkout@v4
- uses: astral-sh/setup-uv@v6
with:
version: "0.9.3"
python-version: "3.12"
enable-cache: true
- run: uv sync --frozen
- run: uv run ruff check .
- run: uv run python manage.py test
env:
# Existing photo tests close a streaming response inside a transaction,
# which closes the PostgreSQL test connection. Keep the full suite on SQLite.
DATABASE_URL: sqlite:///ci.sqlite3
- name: Apply migrations on PostgreSQL
run: uv run python manage.py migrate --noinput
- run: uv run python manage.py makemigrations --check --dry-run
- run: bash -n deploy/deploy.sh
- run: python3 -m unittest discover -s tests -p 'test_deploy.py'
- name: Validate production Compose
env:
APP_IMAGE: ghcr.io/moscowpython/moscowpythonpro:ci
POSTGRES_PASSWORD: ci-only
CSRF_TRUSTED_ORIGINS: https://example.com
run: docker compose -f docker-compose.prod.yml config --quiet
publish:
needs: checks
if: github.event_name != 'pull_request' && github.ref == 'refs/heads/master'
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
image: ${{ steps.image.outputs.name }}@${{ steps.build.outputs.digest }}
steps:
- uses: actions/checkout@v4
- name: Set image name
id: image
env:
REPOSITORY: ${{ github.repository }}
run: echo "name=ghcr.io/${REPOSITORY,,}" >> "$GITHUB_OUTPUT"
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v6
id: build
with:
context: .
push: true
platforms: linux/amd64
tags: ${{ steps.image.outputs.name }}:${{ github.sha }}
labels: |
org.opencontainers.image.source=https://github.com/${{ github.repository }}
org.opencontainers.image.revision=${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
deploy:
needs: publish
if: github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/master'
runs-on: ubuntu-latest
environment: production
concurrency:
group: production-deploy
cancel-in-progress: false
steps:
- uses: actions/checkout@v4
- name: Reject outdated deployment
env:
GH_TOKEN: ${{ github.token }}
run: |
current="$(gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/master" --jq '.object.sha')"
if [[ "$current" != "$GITHUB_SHA" ]]; then
echo "master has changed. Run the workflow again for its current commit." >&2
exit 1
fi
- name: Deploy over SSH
shell: bash
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 }}
DJANGO_SECRET_KEY: ${{ secrets.SECRET_KEY }}
DEPLOY_PORT: ${{ vars.DEPLOY_PORT || '22' }}
APP_IMAGE: ${{ needs.publish.outputs.image }}
run: |
set -euo pipefail
[[ "$DEPLOY_HOST" =~ ^[a-zA-Z0-9][a-zA-Z0-9.-]*$ ]]
[[ "$DEPLOY_USER" =~ ^[a-zA-Z_][a-zA-Z0-9_-]*$ ]]
[[ "$DEPLOY_PORT" =~ ^[0-9]+$ ]]
[[ -n "$DEPLOY_SSH_KEY" && -n "$DEPLOY_KNOWN_HOSTS" ]]
if [[ -z "$DJANGO_SECRET_KEY" ]]; then
echo "Set SECRET_KEY in the production GitHub environment." >&2
exit 1
fi
ssh_dir="$(mktemp -d)"
trap 'rm -rf "$ssh_dir"' EXIT
printf '%s\n' "$DEPLOY_SSH_KEY" > "$ssh_dir/key"
printf '%s\n' "$DEPLOY_KNOWN_HOSTS" > "$ssh_dir/known_hosts"
chmod 600 "$ssh_dir/key" "$ssh_dir/known_hosts"
options=(-i "$ssh_dir/key" -o BatchMode=yes -o IdentitiesOnly=yes
-o StrictHostKeyChecking=yes -o UserKnownHostsFile="$ssh_dir/known_hosts"
-o ConnectTimeout=15 -o ServerAliveInterval=15 -o ServerAliveCountMax=4)
release="/opt/moscowpythonpro/releases/${GITHUB_SHA}-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}"
ssh "${options[@]}" -p "$DEPLOY_PORT" -l "$DEPLOY_USER" "$DEPLOY_HOST" \
"mkdir -p '$release'"
scp "${options[@]}" -P "$DEPLOY_PORT" \
docker-compose.prod.yml deploy/deploy.sh \
"$DEPLOY_USER@$DEPLOY_HOST:$release/"
printf '%s' "$DJANGO_SECRET_KEY" |
ssh "${options[@]}" -p "$DEPLOY_PORT" -l "$DEPLOY_USER" "$DEPLOY_HOST" \
"bash '$release/deploy.sh' '$APP_IMAGE'"