Skip to content

Commit

Permalink
chore: Add devcontainer prebuild with GitHub Actions and GitLab CI/CD. (
Browse files Browse the repository at this point in the history
  • Loading branch information
huxuan authored Jan 10, 2024
1 parent 61cf06b commit 61c281d
Show file tree
Hide file tree
Showing 14 changed files with 291 additions and 4 deletions.
54 changes: 54 additions & 0 deletions .devcontainer/prebuild/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
ARG PYTHON_VERSION=3.12

########################################################################################
# Dev image is used for development and cicd.
########################################################################################

FROM python:${PYTHON_VERSION} as dev

RUN apt-get update && apt-get install -y --no-install-recommends \
# To install Python applications.
pipx \
&& apt-get clean -y && rm -rf /var/lib/apt/lists/*

# Config pipx
ENV PIPX_HOME=/usr/local/pipx
ENV PIPX_BIN_DIR=/usr/local/bin

# Install pdm
RUN pipx install pdm

########################################################################################
# Build image is an intermediate image used for building the project.
########################################################################################

FROM dev as build

# Copy necessary files for build.
COPY pyproject.toml pdm.lock README.md /workspace/
COPY src/ /workspace/src
COPY .git/ /workspace/.git

# Install dependencies and project into the local packages directory.
WORKDIR /workspace
RUN mkdir __pypackages__ && pdm sync --prod --no-editable

########################################################################################
# Prod image is used for deployment and distribution.
########################################################################################

FROM python:${PYTHON_VERSION}-slim as prod

# NOTE: python docker image has env `PYTHON_VERSION` but with patch version.
# ARG is used here for temporary override without changing the original env.
ARG PYTHON_VERSION=3.12

# Retrieve packages from build stage.
ENV PYTHONPATH=/workspace/pkgs
COPY --from=build /workspace/__pypackages__/${PYTHON_VERSION}/lib /workspace/pkgs

# Retrieve executables from build stage.
COPY --from=build /workspace/__pypackages__/${PYTHON_VERSION}/bin/* /usr/local/bin/

# Set command to run the cli by default.
CMD ["ss-python-cli"]
11 changes: 11 additions & 0 deletions .devcontainer/prebuild/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"build": {
"args": {
"PYTHON_VERSION": "${localEnv:PYTHON_VERSION}"
},
"cacheFrom": "ghcr.io/serious-scaffold/ss-python:dev-py${localEnv:PYTHON_VERSION}",
"context": "../../..",
"dockerfile": "Dockerfile",
"target": "dev"
}
}
41 changes: 41 additions & 0 deletions .github/workflows/devcontainer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: DevContainer Prebuild
concurrency:
cancel-in-progress: true
group: ${{ github.workflow }}-${{ github.ref }}
jobs:
devcontainer_prebuild:
permissions:
contents: read
packages: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: docker/login-action@v3
with:
password: ${{ secrets.GITHUB_TOKEN }}
registry: ghcr.io
username: ${{ github.actor }}
- env:
PYTHON_VERSION: ${{ matrix.python-version }}
uses: devcontainers/[email protected]
with:
imageName: ghcr.io/${{ github.repository }}
imageTag: dev-py${{ matrix.python-version }}
push: always
subFolder: .devcontainer/prebuild
strategy:
matrix:
python-version:
- '3.8'
- '3.9'
- '3.10'
- '3.11'
- '3.12'
on:
push:
branches:
- main
paths:
- .devcontainer/prebuild/**
3 changes: 2 additions & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
stages:
- build
- ci
- release
default:
image: python:3.12
image: ${CI_REGISTRY_IMAGE}:dev-py3.12
include: .gitlab/ci/**.yml
2 changes: 1 addition & 1 deletion .gitlab/ci/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ci:
coverage_format: cobertura
path: coverage.xml
coverage: /(?i)total.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/
image: python:$PYTHON_VERSION
image: ${CI_REGISTRY_IMAGE}:dev-py${PYTHON_VERSION}
interruptible: true
only:
- main
Expand Down
28 changes: 28 additions & 0 deletions .gitlab/ci/devcontainer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
devcontainer-prebuild:
image: docker:latest
interruptible: true
only:
- main
parallel:
matrix:
- PYTHON_VERSION:
- '3.8'
- '3.9'
- '3.10'
- '3.11'
- '3.12'
script:
- apk add --update nodejs npm python3 make g++
- npm install -g @devcontainers/cli
- docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
- |
devcontainer build \
--image-name ${CI_REGISTRY_IMAGE}:dev-py${PYTHON_VERSION} \
--push true \
--workspace-folder .devcontainer/prebuild/
services:
- docker:dind
stage: build
variables:
DOCKER_TLS_CERTDIR: /certs
PYTHON_VERSION: ${PYTHON_VERSION}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"cobertura",
"deepclean",
"deflist",
"devcontainer",
"devcontainers",
"elif",
"endmacro",
"epub",
Expand All @@ -36,6 +38,7 @@
"pathjoin",
"pipenv",
"pipx",
"prebuild",
"pycache",
"pydantic",
"pyproject",
Expand Down
54 changes: 54 additions & 0 deletions template/.devcontainer/prebuild/.devcontainer/Dockerfile.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
ARG PYTHON_VERSION={{ default_py }}

########################################################################################
# Dev image is used for development and cicd.
########################################################################################

FROM python:${PYTHON_VERSION} as dev

RUN apt-get update && apt-get install -y --no-install-recommends \
# To install Python applications.
pipx \
&& apt-get clean -y && rm -rf /var/lib/apt/lists/*

# Config pipx
ENV PIPX_HOME=/usr/local/pipx
ENV PIPX_BIN_DIR=/usr/local/bin

# Install pdm
RUN pipx install pdm

########################################################################################
# Build image is an intermediate image used for building the project.
########################################################################################

FROM dev as build

# Copy necessary files for build.
COPY pyproject.toml pdm.lock README.md /workspace/
COPY src/ /workspace/src
COPY .git/ /workspace/.git

# Install dependencies and project into the local packages directory.
WORKDIR /workspace
RUN mkdir __pypackages__ && pdm sync --prod --no-editable

########################################################################################
# Prod image is used for deployment and distribution.
########################################################################################

FROM python:${PYTHON_VERSION}-slim as prod

# NOTE: python docker image has env `PYTHON_VERSION` but with patch version.
# ARG is used here for temporary override without changing the original env.
ARG PYTHON_VERSION={{ default_py }}

# Retrieve packages from build stage.
ENV PYTHONPATH=/workspace/pkgs
COPY --from=build /workspace/__pypackages__/${PYTHON_VERSION}/lib /workspace/pkgs

# Retrieve executables from build stage.
COPY --from=build /workspace/__pypackages__/${PYTHON_VERSION}/bin/* /usr/local/bin/

# Set command to run the cli by default.
CMD ["ss-python-cli"]
11 changes: 11 additions & 0 deletions template/.devcontainer/prebuild/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"build": {
"args": {
"PYTHON_VERSION": "${localEnv:PYTHON_VERSION}"
},
"cacheFrom": "ghcr.io/serious-scaffold/ss-python:dev-py${localEnv:PYTHON_VERSION}",
"context": "../../..",
"dockerfile": "Dockerfile",
"target": "dev"
}
}
3 changes: 3 additions & 0 deletions template/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"cobertura",
"deepclean",
"deflist",
"devcontainer",
"devcontainers",
"elif",
"endmacro",
"epub",
Expand All @@ -36,6 +38,7 @@
"pathjoin",
"pipenv",
"pipx",
"prebuild",
"pycache",
"pydantic",
"pyproject",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[% from pathjoin("includes", "version_compare.jinja") import version_between -%]
name: DevContainer Prebuild
concurrency:
cancel-in-progress: true
group: {{ '${{ github.workflow }}-${{ github.ref }}' }}
jobs:
devcontainer_prebuild:
permissions:
contents: read
packages: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: docker/login-action@v3
with:
password: {{ '${{ secrets.GITHUB_TOKEN }}' }}
registry: ghcr.io
username: {{ '${{ github.actor }}' }}
- env:
PYTHON_VERSION: {{ '${{ matrix.python-version }}' }}
uses: devcontainers/[email protected]
with:
imageName: ghcr.io/{{ '${{ github.repository }}' }}
imageTag: dev-py{{ '${{ matrix.python-version }}' }}
push: always
subFolder: .devcontainer/prebuild
strategy:
matrix:
python-version:
[%- if version_between("3.8", min_py, max_py) %]
- '3.8'
[%- endif %]
[%- if version_between("3.9", min_py, max_py) %]
- '3.9'
[%- endif %]
[%- if version_between("3.10", min_py, max_py) %]
- '3.10'
[%- endif %]
[%- if version_between("3.11", min_py, max_py) %]
- '3.11'
[%- endif %]
[%- if version_between("3.12", min_py, max_py) %]
- '3.12'
[%- endif %]
on:
push:
branches:
- main
paths:
- .devcontainer/prebuild/**
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
stages:
- build
- ci
- release
default:
image: python:{{ default_py }}
image: ${CI_REGISTRY_IMAGE}:dev-py{{ default_py }}
include: .gitlab/ci/**.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ci:
coverage_format: cobertura
path: coverage.xml
coverage: /(?i)total.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/
image: python:$PYTHON_VERSION
image: ${CI_REGISTRY_IMAGE}:dev-py${PYTHON_VERSION}
interruptible: true
only:
- main
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
devcontainer-prebuild:
image: docker:latest
interruptible: true
only:
- main
parallel:
matrix:
- PYTHON_VERSION:
- '3.8'
- '3.9'
- '3.10'
- '3.11'
- '3.12'
script:
- apk add --update nodejs npm python3 make g++
- npm install -g @devcontainers/cli
- docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
- |
devcontainer build \
--image-name ${CI_REGISTRY_IMAGE}:dev-py${PYTHON_VERSION} \
--push true \
--workspace-folder .devcontainer/prebuild/
services:
- docker:dind
stage: build
variables:
DOCKER_TLS_CERTDIR: /certs
PYTHON_VERSION: ${PYTHON_VERSION}

0 comments on commit 61c281d

Please sign in to comment.