Skip to content

Commit 9660bc0

Browse files
committed
Merge branch 'develop'
2 parents 51a4304 + 3913817 commit 9660bc0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2948
-1223
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ deploy/
2020
.github
2121
nc/notebooks
2222
venv
23+
.venv
24+
bin/

.github/workflows/deploy.yaml

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,75 @@ name: deploy
22

33
on:
44
push:
5-
branches: [main, develop]
5+
branches: [main, develop, CU-868em67g4_Upgrade-to-Django-52]
66

77
jobs:
88
deploy:
99
runs-on: ubuntu-latest
10+
permissions:
11+
# Allow GitHub's OIDC provider to create a JSON Web Token for every run
12+
# https://github.com/aws-actions/configure-aws-credentials?tab=readme-ov-file#oidc-configuration
13+
id-token: write
14+
# This is required for actions/checkout
15+
contents: read
16+
# This is needed for bobheadxi/deployments
17+
actions: read
18+
deployments: write
1019
env:
1120
ENV: staging
1221
GITHUB_ENV: https://staging.nccopwatch.org/
13-
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
14-
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
22+
DOCKER_REPO: 606178775542.dkr.ecr.us-east-2.amazonaws.com/traff-appli-gvyudgfsjhrz
1523
AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }}
1624
steps:
17-
- uses: actions/checkout@v3
25+
- uses: actions/checkout@v5
1826
with:
1927
fetch-depth: 0
28+
- name: Set up Docker Buildx
29+
uses: docker/setup-buildx-action@v3
2030
- name: Set env vars (production)
2131
if: endsWith(github.ref, '/main')
2232
run: |
2333
echo "ENV=production" >> $GITHUB_ENV
2434
echo "ENV_URL=https://nccopwatch.org/" >> $GITHUB_ENV
25-
- uses: actions/setup-python@v4
35+
- uses: astral-sh/setup-uv@v6
2636
with:
27-
python-version: '3.12'
28-
cache: 'pip'
29-
cache-dependency-path: 'requirements/*/*.txt'
37+
enable-cache: true
3038
- name: Install dependencies
3139
run: |
32-
python -m pip install pip-tools
33-
pip-sync requirements/base/base.txt requirements/dev/dev.txt
40+
uv sync --locked
3441
- name: Start deployment
3542
uses: bobheadxi/deployments@v1
3643
id: deployment
3744
with:
3845
step: start
3946
token: ${{ secrets.GITHUB_TOKEN }}
4047
env: ${{ env.ENV }}
48+
- name: Set DOCKER_TAG variable
49+
run: |
50+
echo DOCKER_TAG="$ENV-$(git rev-parse --short HEAD)" >> "$GITHUB_ENV"
51+
- name: Configure AWS Credentials
52+
uses: aws-actions/configure-aws-credentials@v4
53+
with:
54+
aws-region: us-east-2
55+
role-to-assume: arn:aws:iam::606178775542:role/GitHubActionsDeploy
56+
role-session-name: GitHubActionsDeploy
4157
- name: Login to Docker
4258
id: docker-login
4359
run: |
4460
echo "env is $ENV"
45-
inv $ENV aws.docker-login
46-
- name: Build, tag, push, and deploy image
47-
id: build-tag-push-deploy
61+
uv run inv $ENV aws.docker-login
62+
- name: Build image
63+
uses: docker/build-push-action@v6
64+
with:
65+
context: .
66+
pull: true
67+
push: true
68+
tags: "${{ env.DOCKER_REPO }}:${{ env.DOCKER_TAG }}"
69+
target: "deploy"
70+
- name: Deploy image
4871
run: |
4972
echo "env is $ENV"
50-
inv $ENV image deploy --verbosity=0
73+
uv run inv $ENV deploy --tag=${{ env.DOCKER_TAG }} --verbosity=0
5174
- name: Update deployment status
5275
uses: bobheadxi/deployments@v1
5376
if: always()

.github/workflows/test.yaml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: test
33
on:
44
pull_request:
55
push:
6-
branches: [main, develop, actions-cd]
6+
branches: [main, develop]
77

88
jobs:
99
tests:
@@ -25,30 +25,28 @@ jobs:
2525
ports:
2626
- 5432:5432
2727
steps:
28-
- uses: actions/checkout@v3
29-
- uses: actions/setup-python@v4
28+
- uses: actions/checkout@v5
29+
- uses: astral-sh/setup-uv@v6
3030
with:
31-
python-version: '3.12'
32-
cache: 'pip'
33-
cache-dependency-path: 'requirements/*/*.txt'
31+
enable-cache: true
3432
- name: Install dependencies
3533
run: |
3634
sudo apt update
3735
sudo apt install -y --no-install-recommends postgresql-client
38-
python -m pip install pip-tools
39-
pip-sync requirements/base/base.txt requirements/dev/dev.txt requirements/test/test.txt
36+
uv sync --locked
37+
- run: uv run pre-commit run --show-diff-on-failure --color=always --all-files
4038
- name: Create NC database
4139
run: |
4240
psql $DATABASE_URL -c 'CREATE DATABASE traffic_stops_nc;'
4341
env:
4442
DATABASE_URL: postgres://postgres:postgres@localhost:5432/traffic_stops
4543
- name: Run tests
4644
run: |
47-
pytest
45+
uv run pytest
4846
env:
4947
DJANGO_SETTINGS_MODULE: traffic_stops.settings.dev
5048
DATABASE_URL: postgres://postgres:postgres@localhost:5432/traffic_stops
5149
DATABASE_URL_NC: postgres://postgres:postgres@localhost:5432/traffic_stops_nc
5250
- name: Test deploy image build
5351
run: |
54-
inv image.build
52+
uv run inv image.build

.pre-commit-config.yaml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
repos:
2+
- repo: https://github.com/astral-sh/uv-pre-commit
3+
# uv version.
4+
rev: 0.8.9
5+
hooks:
6+
- id: uv-lock
27
- repo: https://github.com/psf/black
38
rev: 24.10.0
49
hooks:
510
- id: black
6-
language_version: python3.12
11+
language_version: python3.13
712
exclude: migrations
813
- repo: https://github.com/PyCQA/flake8
914
rev: 7.1.1
@@ -30,3 +35,12 @@ repos:
3035
- id: check-merge-conflict
3136
- id: debug-statements
3237
- id: detect-private-key
38+
- repo: https://github.com/asottile/pyupgrade
39+
rev: v3.20.0
40+
hooks:
41+
- id: pyupgrade
42+
- repo: https://github.com/adamchainz/django-upgrade
43+
rev: 1.25.0
44+
hooks:
45+
- id: django-upgrade
46+
args: [--target-version, "5.2"]

Dockerfile

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:18.17.0-bullseye-slim AS static_files
1+
FROM node:22.18.0-bullseye-slim AS static_files
22

33
WORKDIR /code
44
ENV PATH=/code/node_modules/.bin:$PATH
@@ -7,7 +7,17 @@ RUN npm install --silent
77
COPY frontend/ /code/
88
RUN npm run build
99

10-
FROM python:3.12-slim-bullseye AS base
10+
FROM ghcr.io/astral-sh/uv:python3.13-bookworm AS base
11+
12+
# Enable bytecode compilation
13+
ENV UV_COMPILE_BYTECODE=1
14+
15+
# Copy from the cache instead of linking since it's a mounted volume
16+
ENV UV_LINK_MODE=copy
17+
18+
# Use a custom VIRTUAL_ENV with uv to avoid conflicts with local developer's
19+
# .venv/ while running tests in Docker
20+
ENV VIRTUAL_ENV=/venv
1121

1222
# Create a group and user to run our app
1323
ARG APP_USER=appuser
@@ -32,27 +42,28 @@ RUN set -ex \
3242
&& apt-get update && apt-get install -y --no-install-recommends $RUN_DEPS \
3343
&& rm -rf /var/lib/apt/lists/*
3444

35-
# Copy in your requirements file
36-
# ADD requirements.txt /requirements.txt
37-
38-
# OR, if you're using a directory for your requirements, copy everything (comment out the above and uncomment this if so):
39-
ADD requirements /requirements
40-
4145
# Install build deps, then run `pip install`, then remove unneeded build deps all in a single step.
4246
# Correct the path to your production requirements file, if needed.
43-
RUN set -ex \
47+
ARG UV_OPTS="--no-dev --group deploy"
48+
RUN --mount=type=cache,target=/root/.cache/uv \
49+
--mount=type=bind,source=uv.lock,target=uv.lock \
50+
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
51+
set -ex \
4452
&& BUILD_DEPS=" \
4553
build-essential \
4654
libpcre3-dev \
4755
libpq-dev \
4856
git-core \
4957
" \
5058
&& apt-get update && apt-get install -y --no-install-recommends $BUILD_DEPS \
51-
&& pip install -U -q pip-tools \
52-
&& pip-sync requirements/base/base.txt requirements/deploy/deploy.txt \
59+
&& uv venv $VIRTUAL_ENV \
60+
&& uv sync --active --locked --no-install-project $UV_OPTS \
5361
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false $BUILD_DEPS \
5462
&& rm -rf /var/lib/apt/lists/*
5563

64+
# Add uv venv to PATH
65+
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
66+
5667
FROM base AS deploy
5768

5869
# Copy your application code to the container (make sure you create a .dockerignore file if any large files or directories should be excluded)
@@ -98,7 +109,7 @@ ENTRYPOINT ["/code/docker-entrypoint.sh"]
98109
# Start uWSGI
99110
CMD ["newrelic-admin", "run-program", "uwsgi", "--single-interpreter", "--enable-threads", "--show-config"]
100111

101-
FROM python:3.12-slim-bullseye AS dev
112+
FROM ghcr.io/astral-sh/uv:python3.13-bookworm AS dev
102113

103114
ARG USERNAME=appuser
104115
ARG USER_UID=1000
@@ -153,8 +164,10 @@ RUN --mount=type=cache,target=/var/cache/apt --mount=type=cache,target=/var/lib/
153164
# docker
154165
&& curl https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor | tee /etc/apt/trusted.gpg.d/docker.gpg >/dev/null \
155166
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/trusted.gpg.d/docker.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null \
156-
# nodejs
157-
&& sh -c 'echo "deb https://deb.nodesource.com/node_18.x $(lsb_release -cs) main" > /etc/apt/sources.list.d/nodesource.list' \
167+
# nodejs (https://github.com/nodesource/distributions/wiki/Repository-Manual-Installation#debian-systems)
168+
&& mkdir -p /etc/apt/keyrings \
169+
&& curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \
170+
&& sh -c 'echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_22.x nodistro main" > /etc/apt/sources.list.d/nodesource.list' \
158171
&& wget --quiet -O- https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add - \
159172
# PostgreSQL
160173
&& sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' \

deploy/ansible.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ error_on_undefined_vars = true
88

99
# Make errors more readable
1010
# human-readable stdout/stderr results display
11-
stdout_callback = yaml
11+
result_format=yaml

deploy/group_vars/all.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ k8s_aws_load_balancer_type: nlb
9797
# ----------------------------------------------------------------------------
9898

9999
# New Relic Account: [email protected]
100-
k8s_newrelic_chart_version: "6.0.1"
100+
k8s_newrelic_chart_version: "6.0.10"
101101
k8s_newrelic_logging_enabled: true
102102
k8s_newrelic_license_key: !vault |
103103
$ANSIBLE_VAULT;1.1;AES256

docs/deploy.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,13 @@ Create PostgreSQL database
6767

6868
3. Create environment-specific databases, e.g.::
6969

70-
```sql
7170
CREATE ROLE trafficstops_staging WITH LOGIN NOSUPERUSER INHERIT CREATEDB NOCREATEROLE NOREPLICATION PASSWORD '<password>';
7271
CREATE DATABASE trafficstops_staging;
7372
GRANT CONNECT ON DATABASE trafficstops_staging TO trafficstops_staging;
7473
GRANT ALL PRIVILEGES ON DATABASE trafficstops_staging TO trafficstops_staging;
7574
CREATE DATABASE trafficstops_staging_nc;
7675
GRANT CONNECT ON DATABASE trafficstops_staging_nc TO trafficstops_staging;
7776
GRANT ALL PRIVILEGES ON DATABASE trafficstops_staging_nc TO trafficstops_staging;
78-
```
7977

8078

8179
Configure cluster for deploying web applications

docs/dev-setup.rst

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Below you will find basic setup and deployment instructions for the NC Traffic
55
Stops project. To begin you should have the following applications installed on
66
your local development system:
77

8-
- Python 3.12
9-
- NodeJS >= 12.6.0
10-
- `pip >= 8 or so <http://www.pip-installer.org/>`_
8+
- Python 3.13
9+
- NodeJS >= 22
10+
- `Install uv <https://docs.astral.sh/uv/getting-started/installation/>`_
1111
- Postgres >= 16
1212

1313
Getting Started (Docker 🐳)
@@ -22,7 +22,8 @@ pack for VS Code.
2222
2. **Install Python requirements:** Create virtual environment and install Python requirements::
2323

2424
python3 -m venv /code/venv
25-
make setup
25+
uv python install
26+
uv sync --locked
2627
(cd frontend; npm install)
2728

2829
3. **Setup pre-commit:** Install pre-commit to enforce a variety of community standards::
@@ -85,10 +86,14 @@ To use ``psql`` locally, make sure you have the following env variables loaded
8586
To setup your local environment you should create a virtualenv and install the
8687
necessary requirements::
8788

88-
$ which python3.12 # make sure you have Python 3.10 installed
89-
$ mkvirtualenv --python=`which python3.10` traffic-stops
90-
(traffic-stops)$ pip install -U pip
91-
(traffic-stops)$ make setup
89+
$ uv python install
90+
$ uv sync --locked
91+
92+
Set up ``uv`` and ``direnv``::
93+
94+
echo "PATH_add .venv/bin/" >> .envrc
95+
echo "uv sync --locked" >> .envrc
96+
direnv allow
9297

9398
Next, we'll set up our local environment variables. We use `django-dotenv
9499
<https://github.com/jpadilla/django-dotenv>`_ to help with this. It reads environment variables

docs/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Contents:
2828
dev-setup
2929
data-import
3030
api
31+
census
32+
deploy
3133

3234

3335
Indices and tables

0 commit comments

Comments
 (0)