-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add devcontainer prebuild with GitHub Actions and GitLab CI/CD. (
#263)
- Loading branch information
Showing
14 changed files
with
291 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
template/.devcontainer/prebuild/.devcontainer/Dockerfile.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
template/.devcontainer/prebuild/.devcontainer/devcontainer.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...% if repo_host_type == 'github.com' %].github[% endif %]/workflows/devcontainer.yml.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/** |
3 changes: 2 additions & 1 deletion
3
...'gitlab.com' or repo_host_type == 'gitlab-self-managed' %].gitlab-ci.yml[% endif %].jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
....com' or repo_host_type == 'gitlab-self-managed' %].gitlab[% endif %]/ci/devcontainer.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |