Skip to content

Commit 0e3d330

Browse files
authored
Merge pull request #94 from LandRegistry/docker
Docker support
2 parents ed40347 + 86a1086 commit 0e3d330

13 files changed

+152
-89
lines changed

.flaskenv

-12
This file was deleted.

.github/workflows/codeql.yml

+22-12
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,23 @@ name: "CodeQL"
1313

1414
on:
1515
push:
16-
branches: [ "main" ]
16+
branches: [ "main", "develop" ]
1717
pull_request:
1818
# The branches below must be a subset of the branches above
1919
branches: [ "main" ]
2020
schedule:
21-
- cron: '17 2 * * 4'
21+
- cron: '40 16 * * 6'
2222

2323
jobs:
2424
analyze:
2525
name: Analyze
26-
runs-on: ubuntu-latest
26+
# Runner size impacts CodeQL analysis time. To learn more, please see:
27+
# - https://gh.io/recommended-hardware-resources-for-running-codeql
28+
# - https://gh.io/supported-runners-and-hardware-resources
29+
# - https://gh.io/using-larger-runners
30+
# Consider using larger runners for possible analysis time improvements.
31+
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
32+
timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 360 }}
2733
permissions:
2834
actions: read
2935
contents: read
@@ -32,8 +38,10 @@ jobs:
3238
strategy:
3339
fail-fast: false
3440
matrix:
35-
language: [ 'javascript', 'python' ]
36-
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
41+
language: [ 'javascript-typescript', 'python' ]
42+
# CodeQL supports [ 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'swift' ]
43+
# Use only 'java-kotlin' to analyze code written in Java, Kotlin or both
44+
# Use only 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both
3745
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support
3846

3947
steps:
@@ -48,25 +56,27 @@ jobs:
4856
# If you wish to specify custom queries, you can do so here or in a config file.
4957
# By default, queries listed here will override any specified in a config file.
5058
# Prefix the list here with "+" to use these queries and those in the config file.
51-
52-
# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
59+
60+
# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
5361
# queries: security-extended,security-and-quality
5462

55-
56-
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
63+
64+
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift).
5765
# If this step fails, then you should remove it and run the build manually (see below)
5866
- name: Autobuild
5967
uses: github/codeql-action/autobuild@v2
6068

6169
# ℹ️ Command-line programs to run using the OS shell.
6270
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
6371

64-
# If the Autobuild fails above, remove it and uncomment the following three lines.
72+
# If the Autobuild fails above, remove it and uncomment the following three lines.
6573
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
6674

6775
# - run: |
68-
# echo "Run, Build Application using script"
69-
# ./location_of_script_within_repo/buildscript.sh
76+
# echo "Run, Build Application using script"
77+
# ./location_of_script_within_repo/buildscript.sh
7078

7179
- name: Perform CodeQL Analysis
7280
uses: github/codeql-action/analyze@v2
81+
with:
82+
category: "/language:${{matrix.language}}"

.github/workflows/dependency-review.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ jobs:
1717
- name: 'Checkout Repository'
1818
uses: actions/checkout@v3
1919
- name: 'Dependency Review'
20-
uses: actions/dependency-review-action@v2
20+
uses: actions/dependency-review-action@v3

.github/workflows/python-app.yml

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
1+
# This workflow will install Python dependencies, run tests and lint with a single version of Python
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
3+
14
name: Python application
25

36
on:
47
push:
5-
branches: [main]
8+
branches: [ "main" ]
69
pull_request:
7-
branches: [main]
10+
branches: [ "main" ]
11+
12+
permissions:
13+
contents: read
814

915
jobs:
1016
build:
1117
runs-on: ubuntu-latest
12-
strategy:
13-
matrix:
14-
python-version: ["3.8", "3.9", "3.10", "3.11"]
15-
1618
steps:
1719
- uses: actions/checkout@v3
18-
- name: Set up Python ${{ matrix.python-version }}
20+
- name: Set up Python 3.11
1921
uses: actions/setup-python@v4
2022
with:
21-
python-version: ${{ matrix.python-version }}
23+
python-version: 3.11
2224
- name: Install dependencies
2325
run: |
2426
python -m pip install --upgrade pip
25-
pip install safety flake8 black bandit
27+
pip install -r requirements_dev.txt
2628
pip install -r requirements.txt
2729
- name: Check dependencies for known security vulnerabilities
2830
run: safety check -r requirements.txt
2931
- name: Check code for potential security vulnerabilities
30-
run: bandit -r .
32+
run: bandit -r . -x /tests
3133
- name: Check code formatting
32-
run: black . -l 120 --check
34+
run: |
35+
black . -t py311 -l 120 --check
36+
isort . -c
3337
- name: Lint with flake8
3438
run: |
3539
# stop the build if there are Python syntax errors or undefined names
3640
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
3741
# exit-zero treats all errors as warnings.
3842
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=120 --statistics
43+
# - name: Test with pytest
44+
# run: python -m pytest --cov=app --cov-report=term-missing --cov-branch

.gitignore

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Created by https://www.toptal.com/developers/gitignore/api/windows,macos,osx,linux,jetbrains+all,visualstudio,visualstudiocode,python,flask,git,venv
2-
# Edit at https://www.toptal.com/developers/gitignore?templates=windows,macos,osx,linux,jetbrains+all,visualstudio,visualstudiocode,python,flask,git,venv
1+
# Created by https://www.toptal.com/developers/gitignore/api/git,osx,venv,flask,linux,macos,python,windows,certificates,visualstudio,jetbrains+all,visualstudiocode
2+
# Edit at https://www.toptal.com/developers/gitignore?templates=git,osx,venv,flask,linux,macos,python,windows,certificates,visualstudio,jetbrains+all,visualstudiocode
33

44
### GOV.UK Frontend ###
55
app/static/fonts*
@@ -8,6 +8,14 @@ app/static/images*
88
app/static/VERSION.txt
99
govuk_components*
1010

11+
### certificates ###
12+
*.pem
13+
*.key
14+
*.crt
15+
*.cer
16+
*.der
17+
*.priv
18+
1119
### Flask ###
1220
instance/*
1321
!instance/.gitignore
@@ -872,4 +880,4 @@ FodyWeavers.xsd
872880
### VisualStudio Patch ###
873881
# Additional files built by Visual Studio
874882

875-
# End of https://www.toptal.com/developers/gitignore/api/windows,macos,osx,linux,jetbrains+all,visualstudio,visualstudiocode,python,flask,git,venv
883+
# End of https://www.toptal.com/developers/gitignore/api/git,osx,venv,flask,linux,macos,python,windows,certificates,visualstudio,jetbrains+all,visualstudiocode

Dockerfile

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM python:3.11-slim
2+
3+
RUN useradd containeruser
4+
5+
WORKDIR /home/containeruser
6+
7+
COPY app app
8+
COPY govuk-frontend-flask.py config.py docker-entrypoint.sh requirements.txt ./
9+
RUN pip install -r requirements.txt \
10+
&& chmod +x docker-entrypoint.sh \
11+
&& chown -R containeruser:containeruser ./
12+
13+
# Set environment variables
14+
ENV FLASK_APP=govuk-frontend-flask.py \
15+
PYTHONDONTWRITEBYTECODE=1 \
16+
PYTHONUNBUFFERED=1
17+
18+
USER containeruser
19+
20+
EXPOSE 8000
21+
ENTRYPOINT ["./docker-entrypoint.sh"]

README.md

+17-25
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,17 @@ The app is provided intentionally bare, with just the essential parts that all s
1515

1616
### Required
1717

18-
- Python 3.8.x or higher
19-
20-
### Optional
21-
22-
- Redis 4.0.x or higher (for rate limiting, otherwise in-memory storage is used)
18+
- Docker
2319

2420
## Getting started
2521

2622
### Create a new repository
2723

2824
[Create a new repository](https://github.com/LandRegistry/govuk-frontend-flask/generate) using this template, with the same directory structure and files. Then clone a local copy of your newly created repository.
2925

30-
### Create venv and install requirements
31-
32-
```shell
33-
python3 -m venv venv
34-
source venv/bin/activate
35-
pip3 install -r requirements.txt ; pip3 install -r requirements_dev.txt
36-
```
37-
38-
### Get GOV.UK Frontend assets
39-
40-
For convenience a shell script has been provided to download and extract the GOV.UK Frontend distribution assets
41-
42-
```shell
43-
./build.sh
44-
```
45-
4626
### Set local environment variables
4727

48-
In the `.flaskenv` file you will find a number of environment variables. These are injected as global variables into the app and pre-populated into page templates as appropriate. Enter your specific information for the following:
28+
In the `compose.yml` file you will find a number of environment variables. These are injected as global variables into the app and pre-populated into page templates as appropriate. Enter your specific service information for the following:
4929

5030
- CONTACT_EMAIL
5131
- CONTACT_PHONE
@@ -55,13 +35,25 @@ In the `.flaskenv` file you will find a number of environment variables. These a
5535
- SERVICE_PHASE
5636
- SERVICE_URL
5737

58-
### Run app
38+
You must also set a new unique `SECRET_KEY`, which is used to securely sign the session cookie and CSRF tokens. It should be a long random `bytes` or `str`. You can use the output of this Python comand to generate a new key:
39+
40+
```shell
41+
python -c 'import secrets; print(secrets.token_hex())'
42+
```
43+
44+
### Get the latest GOV.UK Frontend assets
45+
46+
```shell
47+
./build.sh
48+
```
49+
50+
### Run containers
5951

6052
```shell
61-
flask run
53+
docker compose up --build
6254
```
6355

64-
You should now have the app running on <http://localhost:5000/>
56+
You should now have the app running on <https://localhost:8000/>. Accept the browsers security warning due to the self-signed HTTPS certificate to continue.
6557

6658
## Demos
6759

app/demos/forms.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -350,20 +350,29 @@ class ConditionalRevealForm(FlaskForm):
350350
class AutocompleteForm(FlaskForm):
351351
# Manually added list here, but could be dynamically assigned in server route
352352
countries = [
353+
"Argentina",
354+
"Australia",
355+
"Brazil",
353356
"Canada",
354357
"China",
355358
"France",
356359
"Germany",
357360
"India",
361+
"Indonesia",
358362
"Italy",
359363
"Japan",
364+
"Mexico",
365+
"Russia",
366+
"Saudi Arabia",
367+
"South Africa",
360368
"South Korea",
369+
"Turkey",
361370
"United Kingdom",
362371
"United States",
363372
]
364373

365374
country = StringField(
366-
"Country",
375+
"G20 Countries",
367376
widget=GovTextInput(),
368377
validators=[InputRequired(message="Enter a country")],
369378
description="Start typing and select a suggestion",

compose.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
services:
2+
web:
3+
container_name: govuk-frontend-flask
4+
build: .
5+
restart: always
6+
environment:
7+
- CONTACT_EMAIL=[contact email]
8+
- CONTACT_PHONE=[contact phone]
9+
- DEPARTMENT_NAME=[name of department]
10+
- DEPARTMENT_URL=[url of department]
11+
- REDIS_URL=redis://cache:6379
12+
- SECRET_KEY=4f378500459bb58fecf903ea3c113069f11f150b33388f56fc89f7edce0e6a84
13+
- SERVICE_NAME=[name of service]
14+
- SERVICE_PHASE=[phase]
15+
- SERVICE_URL=[url of service]
16+
ports:
17+
- "8000:8000"
18+
volumes:
19+
- .:/home/containeruser
20+
depends_on:
21+
- cache
22+
cache:
23+
container_name: redis
24+
image: redis:7.0-alpine
25+
restart: always
26+
ports:
27+
- 6379:6379
28+

docker-entrypoint.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
openssl req -new -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365 -subj "/C=GB/ST=Devon/L=Plymouth/O=HM Land Registry/OU=DDaT/CN=localhost"
3+
exec gunicorn --reload --certfile cert.pem --keyfile key.pem -b :8000 --access-logfile - --error-logfile - govuk-frontend-flask:app

requirements.in

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
cssmin
22
email_validator
3+
flask
34
flask-assets
45
flask-compress
5-
flask-limiter
6+
flask-limiter[redis]
67
flask-talisman
7-
flask
88
govuk-frontend-jinja
99
govuk-frontend-wtf
1010
gunicorn
1111
jsmin
12-
python-dotenv
13-
redis

0 commit comments

Comments
 (0)