From b002691d1ec4cbd4527590165caecdc13c6740de Mon Sep 17 00:00:00 2001 From: Luis Alvergue Date: Tue, 3 Dec 2024 19:25:22 +0000 Subject: [PATCH] feat: create devcontainer note that .dockerignore is required for the step "install pre-commit environments in throwaway Git repository" in the Dockerfile --- .devcontainer/Dockerfile | 39 +++++++++++++++++++++++++++++++++ .devcontainer/devcontainer.json | 27 +++++++++++++++++++++++ .devcontainer/postAttach.sh | 6 +++++ .devcontainer/requirements.txt | 1 + .dockerignore | 1 + .pre-commit-config.yaml | 26 ++++++++++++++++++++++ .vscode/settings.json | 9 ++++++++ compose.yml | 10 +++++++++ 8 files changed, 119 insertions(+) create mode 100644 .devcontainer/Dockerfile create mode 100644 .devcontainer/devcontainer.json create mode 100644 .devcontainer/postAttach.sh create mode 100644 .devcontainer/requirements.txt create mode 100644 .dockerignore create mode 100644 .pre-commit-config.yaml create mode 100644 .vscode/settings.json create mode 100644 compose.yml diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000..50f8a2a --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,39 @@ +ARG PYTHON_VERSION=3.12 + +FROM python:${PYTHON_VERSION} + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + USER=caltrans + +# create non-root $USER and home directory +RUN useradd --create-home --shell /bin/bash $USER && \ + chown -R $USER /home/$USER + +# switch to $USER +USER $USER + +# enter src directory +WORKDIR /home/$USER/src + +# update PATH for local pip installs +ENV PATH="$PATH:/home/$USER/.local/bin" + +# upgrade pip +RUN python -m pip install --upgrade pip + +# copy assets +COPY . . + +# install devcontainer requirements +RUN pip install --no-cache-dir -r .devcontainer/requirements.txt + +# install pre-commit environments in throwaway Git repository +# https://stackoverflow.com/a/68758943 +RUN git init . && \ + pre-commit install-hooks && \ + rm -rf .git + +CMD sleep infinity + +ENTRYPOINT [] diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..4b65b98 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,27 @@ +{ + "name": "caltrans/pems", + "dockerComposeFile": ["../compose.yml"], + "service": "dev", + "runServices": ["dev"], + "workspaceFolder": "/home/caltrans/src", + "postAttachCommand": ["/bin/bash", ".devcontainer/postAttach.sh"], + "customizations": { + "vscode": { + // Set *default* container specific settings.json values on container create. + "settings": { + "terminal.integrated.defaultProfile.linux": "bash", + "terminal.integrated.profiles.linux": { + "bash": { + "path": "/bin/bash" + } + } + }, + // Add the IDs of extensions you want installed when the container is created. + "extensions": [ + "eamodio.gitlens", + "esbenp.prettier-vscode", + "mhutchie.git-graph" + ] + } + } +} diff --git a/.devcontainer/postAttach.sh b/.devcontainer/postAttach.sh new file mode 100644 index 0000000..2e748e6 --- /dev/null +++ b/.devcontainer/postAttach.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +set -eux + +# initialize pre-commit +git config --global --add safe.directory /home/$USER/src +pre-commit install --overwrite diff --git a/.devcontainer/requirements.txt b/.devcontainer/requirements.txt new file mode 100644 index 0000000..416634f --- /dev/null +++ b/.devcontainer/requirements.txt @@ -0,0 +1 @@ +pre-commit diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..2d2ecd6 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +.git/ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..e0c581c --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,26 @@ +ci: + autofix_commit_msg: "chore(pre-commit): autofix run" + autoupdate_commit_msg: "chore(pre-commit): autoupdate hooks" + +default_install_hook_types: + - pre-commit + - commit-msg + +repos: + - repo: https://github.com/compilerla/conventional-pre-commit + rev: v3.6.0 + hooks: + - id: conventional-pre-commit + stages: [commit-msg] + + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v5.0.0 + hooks: + - id: check-added-large-files + - id: check-merge-conflict + - id: check-yaml + args: [--unsafe] + - id: end-of-file-fixer + - id: fix-byte-order-marker + - id: mixed-line-ending + - id: trailing-whitespace diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..cfa546a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,9 @@ +{ + "editor.formatOnSave": true, + "editor.defaultFormatter": "esbenp.prettier-vscode", + "files.encoding": "utf8", + "files.eol": "\n", + "files.insertFinalNewline": true, + "files.trimFinalNewlines": true, + "files.trimTrailingWhitespace": true +} diff --git a/compose.yml b/compose.yml new file mode 100644 index 0000000..1dc42e6 --- /dev/null +++ b/compose.yml @@ -0,0 +1,10 @@ +name: pems + +services: + dev: + build: + context: . + dockerfile: .devcontainer/Dockerfile + image: caltrans/pems:main + volumes: + - ./:/home/caltrans/src