Skip to content

Commit

Permalink
Workspace 145 (#146)
Browse files Browse the repository at this point in the history
* include postgres in dev container

* install postgres

* postgres settings

* ports

* add env vars

* use new test python job

* postgres 17

* install psql

* remove args

* reference example

* set to main

Co-Authored-By: --global <--global>
  • Loading branch information
SKairinos authored Sep 27, 2024
1 parent 959ff0e commit c498ecc
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM mcr.microsoft.com/devcontainers/base:ubuntu-22.04

# Install PostgreSQL (psql) client.
RUN apt-get update && \
export DEBIAN_FRONTEND=noninteractive && \
apt-get -y install --no-install-recommends postgresql-client
24 changes: 17 additions & 7 deletions .devcontainer.json → .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"image": "mcr.microsoft.com/devcontainers/base:ubuntu-22.04",
"customizations": {
"vscode": {
"extensions": [
// general
"visualstudioexptteam.vscodeintellicode",
"github.vscode-pull-request-github",
"redhat.vscode-yaml",
"davidanson.vscode-markdownlint",
"bierner.markdown-mermaid",
"streetsidesoftware.code-spell-checker",
"tamasfe.even-better-toml",
"github.vscode-github-actions",
"codecov.codecov",
"ritwickdey.liveserver",
"eamodio.gitlens",
"rangav.vscode-thunder-client",
"jock.svg",
"tyriar.luna-paint",
// git
"github.vscode-pull-request-github",
"github.vscode-github-actions",
"eamodio.gitlens",
// javascript
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
Expand All @@ -29,11 +29,17 @@
"ms-python.vscode-pylance",
"ms-python.mypy-type-checker",
"ms-python.black-formatter",
"qwtel.sqlite-viewer",
"njpwerner.autodocstring"
"njpwerner.autodocstring",
// database
"mtxr.sqltools",
"mtxr.sqltools-driver-sqlite",
"mtxr.sqltools-driver-pg"
]
}
},
"dockerComposeFile": [
"./docker-compose.yml"
],
"features": {
"ghcr.io/devcontainers/features/github-cli:1": {
"version": "2.52.0"
Expand All @@ -46,9 +52,13 @@
"version": "3.12"
}
},
"forwardPorts": [
8000, // Django
5432 // PostgreSQL
],
"name": "workspace",
"postCreateCommand": "sudo chmod u+x .submodules/setup/run && .submodules/setup/run",
"remoteUser": "root",
"workspaceMount": "source=${localWorkspaceFolder},target=/codeforlife-workspace,type=bind",
"service": "app",
"workspaceFolder": "/codeforlife-workspace"
}
25 changes: 25 additions & 0 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: "3"

services:
app:
build:
context: ..
dockerfile: .devcontainer/Dockerfile
volumes:
- ..:/codeforlife-workspace:cached
# Overrides default so things don't shut down after the process ends
command: sleep infinity
# Runs app on the same network as the database container,
# allows "forwardPorts" in devcontainer.json function
network_mode: service:db

db:
image: postgres:17
restart: unless-stopped
volumes:
- ../scripts/database:/docker-entrypoint-initdb.d
- ../.postgres-data:/var/lib/postgresql/data
environment:
POSTGRES_MULTIPLE_DATABASES: contributor,portal,template
POSTGRES_USER: root
POSTGRES_PASSWORD: password
6 changes: 6 additions & 0 deletions .github/workflows/backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ on:
type: string
required: false
default: "api"
postgres-db:
description: "The postgres database to test against."
type: string
required: false
default: "db"
secrets:
CODECOV_TOKEN:
description: "The token used to gain access to Codecov."
Expand All @@ -29,6 +34,7 @@ jobs:
with:
python-version: ${{ inputs.python-version }}
source-path: ${{ inputs.source-path }}
postgres-db: ${{ inputs.postgres-db }}

deploy:
permissions:
Expand Down
39 changes: 39 additions & 0 deletions .github/workflows/test-python-code.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ on:
type: string
required: false
default: "./codecov.yml"
postgres-version:
description: "The postgres version to set up."
type: number
required: false
default: 17
postgres-db:
description: "The postgres database to test against."
type: string
required: false
default: "db"
postgres-user:
description: "The postgres user to test with."
type: string
required: false
default: "root"
postgres-password:
description: "The postgres user's password."
type: string
required: false
default: "password"
secrets:
CODECOV_TOKEN:
description: "The token used to gain access to Codecov."
Expand All @@ -50,6 +70,25 @@ jobs:
PYPROJECT_TOML: ${{ inputs.pyproject-toml-directory }}/pyproject.toml
COVERAGE_REPORT: coverage.xml # NOTE: COVERAGE_FILE is reserved - do not use.
OCADO_TECH_ORG_ID: 2088731
DB_NAME: ${{ inputs.postgres-db }}
DB_HOST: localhost
DB_USER: ${{ inputs.postgres-user }}
DB_PASSWORD: ${{ inputs.postgres-password }}
services:
postgres: # https://docs.github.com/en/actions/use-cases-and-examples/using-containerized-services/creating-postgresql-service-containers
image: postgres:${{ inputs.postgres-version }}
env:
POSTGRES_DB: ${{ env.DB_NAME }}
POSTGRES_HOST: ${{ env.DB_HOST }}
POSTGRES_USER: ${{ env.DB_USER }}
POSTGRES_PASSWORD: ${{ env.DB_PASSWORD }}
options: >- # Set health checks to wait until postgres has started
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
steps:
- name: 🛫 Checkout
uses: actions/checkout@v4
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,6 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/

# Database
/.postgres-data
39 changes: 38 additions & 1 deletion codeforlife.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,43 @@
"autoDocstring.customTemplatePath": "python-docstring.mustache",
"workbench.colorCustomizations": {
"editorRuler.foreground": "#008000"
}
},
"sqltools.connections": [
{
"previewLimit": 50,
"server": "localhost",
"port": 5432,
"driver": "PostgreSQL",
"name": "contributor",
"database": "contributor",
"username": "root",
"password": "password"
},
{
"previewLimit": 50,
"server": "localhost",
"port": 5432,
"driver": "PostgreSQL",
"name": "portal",
"database": "portal",
"username": "root",
"password": "password"
},
{
"previewLimit": 50,
"server": "localhost",
"port": 5432,
"driver": "PostgreSQL",
"name": "template",
"database": "template",
"username": "root",
"password": "password"
}
]
},
"extensions": {
"unwantedRecommendations": [
"ms-azuretools.vscode-docker"
]
}
}
25 changes: 25 additions & 0 deletions scripts/database/create-multiple-postgresql-databases.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

# This file was taken from:
# https://github.com/mrts/docker-postgresql-multiple-databases/tree/dfc6df914b031c0b1de017248e50914013734b38

set -e
set -u

function create_user_and_database() {
local database=$1
echo " Creating user and database '$database'"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE USER $database;
CREATE DATABASE $database;
GRANT ALL PRIVILEGES ON DATABASE $database TO $database;
EOSQL
}

if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then
echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES"
for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do
create_user_and_database $db
done
echo "Multiple databases created"
fi

0 comments on commit c498ecc

Please sign in to comment.