Skip to content

Commit 9f31431

Browse files
Merge pull request #24 from python-discord/Python-3.11
Modernise the project
2 parents 37de243 + d1161b7 commit 9f31431

33 files changed

+3012
-1399
lines changed

.github/workflows/build.yaml

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,32 @@
11
name: Build
22

33
on:
4-
workflow_run:
5-
workflows: ["Lint & Test"]
6-
branches:
7-
- main
8-
types:
9-
- completed
10-
11-
concurrency:
12-
group: ${{ github.workflow }}-${{ github.ref }}
13-
cancel-in-progress: true
4+
workflow_call:
5+
inputs:
6+
sha-tag:
7+
description: "A short-form SHA tag for the commit that triggered this flow"
8+
required: true
9+
type: string
1410

1511
jobs:
1612
build:
17-
if: github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'push'
1813
name: Build & Push
1914
runs-on: ubuntu-latest
2015

2116
steps:
22-
# Create a commit SHA-based tag for the container repositories
23-
- name: Create SHA Container Tag
24-
id: sha_tag
25-
run: |
26-
tag=$(cut -c 1-7 <<< $GITHUB_SHA)
27-
echo "::set-output name=tag::$tag"
2817
- name: Checkout code
2918
uses: actions/checkout@v3
30-
3119
# The current version (v2) of Docker's build-push action uses
3220
# buildx, which comes with BuildKit features that help us speed
3321
# up our builds using additional cache features. Buildx also
3422
# has a lot of other features that are not as relevant to us.
3523
#
3624
# See https://github.com/docker/build-push-action
3725
- name: Set up Docker Buildx
38-
uses: docker/setup-buildx-action@v1
26+
uses: docker/setup-buildx-action@v2
3927

4028
- name: Login to Github Container Registry
41-
uses: docker/login-action@v1
29+
uses: docker/login-action@v2
4230
with:
4331
registry: ghcr.io
4432
username: ${{ github.repository_owner }}
@@ -48,7 +36,7 @@ jobs:
4836
# Repository. The container will be tagged as "latest"
4937
# and with the short SHA of the commit.
5038
- name: Build and push
51-
uses: docker/build-push-action@v2
39+
uses: docker/build-push-action@v3
5240
with:
5341
context: .
5442
file: ./Dockerfile
@@ -57,7 +45,7 @@ jobs:
5745
cache-to: type=inline
5846
tags: |
5947
ghcr.io/python-discord/code-jam-management:latest
60-
ghcr.io/python-discord/code-jam-management:${{ steps.sha_tag.outputs.tag }}
48+
ghcr.io/python-discord/code-jam-management:${{ inputs.sha-tag }}
6149
build-args: |
6250
git_sha=${{ github.sha }}
6351
@@ -67,12 +55,6 @@ jobs:
6755
runs-on: ubuntu-latest
6856

6957
steps:
70-
# Create a commit SHA-based tag for the container repositories
71-
- name: Create SHA Container Tag
72-
id: sha_tag
73-
run: |
74-
tag=$(cut -c 1-7 <<< $GITHUB_SHA)
75-
echo "::set-output name=tag::$tag"
7658

7759
# Check out the private "kubernetes" repository in the `kubernetes`
7860
# subdirectory using a GitHub Personal Access Token
@@ -82,16 +64,17 @@ jobs:
8264
repository: python-discord/kubernetes
8365
path: kubernetes
8466

67+
- uses: azure/setup-kubectl@v3
68+
8569
- name: Authenticate with Kubernetes
86-
uses: azure/k8s-set-context@v1
70+
uses: azure/k8s-set-context@v3
8771
with:
8872
method: kubeconfig
8973
kubeconfig: ${{ secrets.KUBECONFIG }}
9074

9175
- name: Deploy to Kubernetes
92-
uses: Azure/k8s-deploy@v1
76+
uses: Azure/k8s-deploy@v4
9377
with:
9478
manifests: |
9579
kubernetes/namespaces/default/code-jam-management/deployment.yaml
96-
images: 'ghcr.io/python-discord/code-jam-management:${{ steps.sha_tag.outputs.tag }}'
97-
kubectl-version: 'latest'
80+
images: 'ghcr.io/python-discord/code-jam-management:${{ inputs.sha-tag }}'

.github/workflows/lint-test.yaml

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
name: Lint & Test
22

33
on:
4-
push:
5-
branches:
6-
- main
7-
pull_request:
4+
workflow_call
85

9-
concurrency:
10-
group: ${{ github.workflow }}-${{ github.ref }}
11-
cancel-in-progress: true
126

137
jobs:
148
lint:
@@ -35,17 +29,15 @@ jobs:
3529
uses: actions/checkout@v3
3630

3731
- name: Install Python Dependencies
38-
uses: HassanAbouelela/actions/setup-python@setup-python_v1.3.2
32+
uses: HassanAbouelela/actions/setup-python@setup-python_v1.4.0
3933
with:
40-
# Set dev=true to install flake8 extensions, which are dev dependencies
41-
dev: true
42-
python_version: '3.9'
34+
python_version: "3.11"
4335

4436
# We will not run `flake8` here, as we will use a separate flake8
4537
# action. As pre-commit does not support user installs, we set
4638
# PIP_USER=0 to not do a user install.
4739
- name: Run pre-commit hooks
48-
run: export PIP_USER=0; SKIP=flake8 pre-commit run --all-files
40+
run: SKIP=flake8 pre-commit run --all-files
4941

5042
# Run flake8 and have it format the linting errors in the format of
5143
# the GitHub Workflow command to register error annotations. This
@@ -58,6 +50,7 @@ jobs:
5850
- name: Run flake8
5951
run: "flake8 \
6052
--format='::error file=%(path)s,line=%(row)d,col=%(col)d::[flake8] %(code)s: %(text)s'"
53+
6154
# We run `coverage` using the `python` command so we can suppress
6255
# irrelevant warnings in our CI output.
6356
- name: Run tests and generate coverage report
@@ -73,7 +66,7 @@ jobs:
7366
# print a "job" link in the output of the GitHub Action
7467
- name: Publish coverage report to coveralls.io
7568
env:
76-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
69+
GITHUB_TOKEN: ${{ github.token }}
7770
run: coveralls --service=github
7871

7972
# Prepare the Pull Request Payload artifact. If this fails, we

.github/workflows/main.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: main
7+
8+
concurrency:
9+
group: ${{ github.workflow }}-${{ github.ref }}
10+
cancel-in-progress: true
11+
12+
jobs:
13+
14+
lint-test:
15+
uses: ./.github/workflows/lint-test.yaml
16+
secrets: inherit
17+
18+
generate-inputs:
19+
if: github.ref == 'refs/heads/main'
20+
runs-on: ubuntu-latest
21+
outputs:
22+
sha-tag: ${{ steps.sha-tag.outputs.sha-tag }}
23+
steps:
24+
- name: Create SHA Container Tag
25+
id: sha-tag
26+
run: |
27+
tag=$(cut -c 1-7 <<< $GITHUB_SHA)
28+
echo "sha-tag=$tag" >> $GITHUB_OUTPUT
29+
30+
build-deploy:
31+
if: github.ref == 'refs/heads/main'
32+
uses: ./.github/workflows/build.yaml
33+
needs:
34+
- lint-test
35+
- generate-inputs
36+
with:
37+
sha-tag: ${{ needs.generate-inputs.outputs.sha-tag }}
38+
secrets: inherit

.pre-commit-config.yaml

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v2.5.0
3+
rev: v4.3.0
44
hooks:
55
- id: check-merge-conflict
66
- id: check-toml
77
- id: check-yaml
8+
args: [--unsafe] # Needed for << lines in docker-compose.yml
89
- id: end-of-file-fixer
910
- id: trailing-whitespace
1011
args: [--markdown-linebreak-ext=md]
12+
1113
- repo: https://github.com/pre-commit/pygrep-hooks
12-
rev: v1.5.1
14+
rev: v1.9.0
1315
hooks:
1416
- id: python-check-blanket-noqa
17+
18+
- repo: https://github.com/pycqa/isort
19+
rev: 5.10.1
20+
hooks:
21+
- id: isort
22+
name: isort (python)
23+
24+
- repo: https://github.com/python-poetry/poetry
25+
rev: '1.2.2'
26+
hooks:
27+
- id: poetry-check
28+
- id: poetry-export
29+
args: ["-f", "requirements.txt", "-o", "main-requirements.txt"]
30+
1531
- repo: local
1632
hooks:
1733
- id: flake8
@@ -21,3 +37,21 @@ repos:
2137
language: system
2238
types: [python]
2339
require_serial: true
40+
- id: black
41+
name: black
42+
description: This hook runs black within our project's environment.
43+
entry: poetry run task black
44+
language: system
45+
types: [python]
46+
require_serial: true
47+
48+
ci:
49+
autofix_commit_msg: |
50+
[pre-commit.ci] auto fixes from pre-commit.com hooks
51+
for more information, see https://pre-commit.ci
52+
autofix_prs: true
53+
autoupdate_branch: ''
54+
autoupdate_commit_msg: '[pre-commit.ci] pre-commit autoupdate'
55+
autoupdate_schedule: daily
56+
skip: [flake8, black] # pre-commit.ci doesn't support running under poetry right now.
57+
submodules: false

Dockerfile

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,23 @@
1-
FROM --platform=linux/amd64 python:3.9-slim as base
1+
FROM --platform=linux/amd64 python:3.11-slim as base
22

3-
# Set pip to have no saved cache
4-
ENV PIP_NO_CACHE_DIR=false \
5-
POETRY_VIRTUALENVS_IN_PROJECT=true \
6-
PYTHONUNBUFFERED=1 \
7-
PIP_DISABLE_PIP_VERSION_CHECK=on \
8-
POETRY_HOME="/opt/poetry" \
9-
INSTALL_DIR="/opt/dependencies" \
10-
APP_DIR="/app" \
11-
POETRY_NO_INTERACTION=1
12-
13-
ENV PATH="$POETRY_HOME/bin:$INSTALL_DIR/.venv/bin:$PATH"
3+
# Define Git SHA build argument for sentry
4+
ARG git_sha="development"
5+
ENV GIT_SHA=$git_sha
146

157
RUN groupadd -g 61000 codejam_management \
16-
&& useradd -g 61000 -l -r -u 61000 codejam_management
17-
18-
FROM base as builder
19-
RUN apt-get update \
20-
&& apt-get -y upgrade \
21-
&& apt-get install --no-install-recommends -y \
22-
curl \
23-
&& apt-get clean && rm -rf /var/lib/apt/lists/*
24-
25-
RUN curl -sSL https://install.python-poetry.org | python -
26-
27-
WORKDIR $INSTALL_DIR
28-
COPY "pyproject.toml" "poetry.lock" ./
29-
RUN poetry install --no-dev
30-
8+
&& useradd -g 61000 -l -r -u 61000 codejam_management
319

32-
FROM base as development
33-
34-
# Create the working directory
35-
WORKDIR $APP_DIR
36-
COPY --from=builder $INSTALL_DIR $INSTALL_DIR
37-
38-
# Copy the source code in last to optimize rebuilding the image
39-
COPY . .
10+
# Install project dependencies
11+
WORKDIR /app
12+
COPY main-requirements.txt ./
13+
RUN pip install -r main-requirements.txt
4014

15+
EXPOSE 8000
4116
USER codejam_management
42-
# Run a single uvicorn worker
43-
# Multiple workers are managed by kubernetes, rather than something like gunicorn
44-
CMD ["sh", "-c", "alembic upgrade head && uvicorn api.main:app --host 0.0.0.0 --port 8000 --reload"]
45-
17+
# Pull the uvicorn_extra build arg and ave it as an env var.
18+
# The CMD instruction is ran at execution time, so it also needs to be an env var, so that it is available at that time.
19+
ARG uvicorn_extras=""
20+
ENV uvicorn_extras=$uvicorn_extras
4621

47-
FROM base as production
48-
COPY --from=builder $INSTALL_DIR $INSTALL_DIR
49-
WORKDIR $APP_DIR
50-
COPY . .
51-
RUN python -m compileall api/
52-
53-
USER codejam_management
54-
CMD ["sh", "-c", "alembic upgrade head && uvicorn api.main:app --host 0.0.0.0 --port 8000"]
22+
ENTRYPOINT ["/bin/bash", "-c"]
23+
CMD ["alembic upgrade head && uvicorn api.main:app --host 0.0.0.0 --port 80 $uvicorn_extras"]

0 commit comments

Comments
 (0)