From 4d34ea6cfe3917c1f1c19374597aac2674ac7907 Mon Sep 17 00:00:00 2001 From: bengt Date: Wed, 8 Oct 2025 01:19:18 +0200 Subject: [PATCH 1/5] Package isort as a minimal, staged Docker image based on Python Alpine --- Dockerfile | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5de32d58..36e2ac8a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,25 +1,20 @@ -FROM python:3.13 +# Build stage +FROM python:3.13-alpine AS builder WORKDIR /isort -COPY pyproject.toml uv.lock /isort/ +COPY . . -# Install uv -COPY --from=ghcr.io/astral-sh/uv:0.6.0 /uv /uvx /bin/ +RUN pip install --no-cache-dir uv -# Setup as minimal a stub project as possible, simply to allow caching base dependencies -# between builds. -# -# If error is encountered in these steps, can safely be removed locally. -RUN mkdir -p /isort/isort -RUN mkdir -p /isort/tests -RUN touch /isort/isort/__init__.py -RUN touch /isort/tests/__init__.py -RUN touch /isort/README.md -COPY . /isort -RUN SETUPTOOLS_SCM_PRETEND_VERSION=0.0.0 uv sync --all-extras --frozen +RUN SETUPTOOLS_SCM_PRETEND_VERSION=0.0.0 uv build . -# Install latest code for actual project -RUN rm -rf /isort +# Release stage +FROM python:3.13-alpine -# Run full test suite -CMD /isort/scripts/test.sh +# Install the wheel from the build stage +COPY --from=builder /isort/dist/ /tmp/ +RUN \ + pip install --no-cache-dir /tmp/*.whl && \ + rm /tmp/*.whl + +ENTRYPOINT ["isort"] From c2e9c2faf94af9b862a6605729e20ee1b69ee426 Mon Sep 17 00:00:00 2001 From: bengt Date: Wed, 8 Oct 2025 01:20:31 +0200 Subject: [PATCH 2/5] Simplify and rebuild Docker ignores for the new Docker image --- .dockerignore | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/.dockerignore b/.dockerignore index 54671a6b..6c103fae 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,17 +1,7 @@ -# project build medadata that can't be used in docker -Dockerfile -.appveyor.yml -.dockerignore -.pre-commit-hooks.yaml -.travis.yml -.git* +# Ignore everything ... +* -# documentation not needed -CHANGELOG.md -LICENSE -MANIFEST.in -.undertake -example.gif -logo.png -art/ -docs/ +# ... except: +!isort +!pyproject.toml +!README.md From fea2a988589d8214ea68f07da9571853aaf64b7d Mon Sep 17 00:00:00 2001 From: bengt Date: Wed, 8 Oct 2025 01:21:06 +0200 Subject: [PATCH 3/5] Extend the release workflow to building and pushing a Docker image --- .github/workflows/release.yml | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ada42b75..b0b747ee 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -10,6 +10,9 @@ jobs: release: if: github.repository_owner == 'PyCQA' name: Release + permissions: + contents: read + packages: write runs-on: ubuntu-latest steps: - name: Check out the repository @@ -55,3 +58,34 @@ jobs: tag: ${{ steps.check-version.outputs.tag }} env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + # Build a new Docker image and push it to the GitHub Container Registry + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to the GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata for Docker + id: meta + uses: docker/metadata-action@v5 + with: + images: ghcr.io/${{ github.repository }} + + - name: Build and push Docker image + uses: docker/build-push-action@v6 + with: + cache-from: type=gha + cache-to: type=gha,mode=max + context: . + labels: ${{ steps.meta.outputs.labels }} + platforms: linux/amd64,linux/arm64 + push: true + tags: ${{ steps.meta.outputs.tags }} From 44cb6f6726b1318ba787ffc4adeb10b3d4c9f45f Mon Sep 17 00:00:00 2001 From: bengt Date: Wed, 8 Oct 2025 01:57:55 +0200 Subject: [PATCH 4/5] Document Docker image usage on the command line and in GitLab CICD --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/README.md b/README.md index 050533a2..69cd5f82 100644 --- a/README.md +++ b/README.md @@ -275,6 +275,44 @@ pre-commit script to check Python code before committing. [More info here.](https://pycqa.github.io/isort/docs/configuration/git_hook.html) +## Docker + +isort is available as a Docker image on the GitHub Container Registry. + +To sort imports and modify files: + +```bash +docker run -it --rm \ + -v $(pwd):/code \ + -v $(pwd)/pyproject.toml:/code/pyproject.toml \ + -w /code \ + ghcr.io/pycqa/isort:latest \ + isort +``` + +To check imports without modifying files: + +```bash +docker run -it --rm \ + -v $(pwd):/code \ + -v $(pwd)/pyproject.toml:/code/pyproject.toml \ + -w /code ghcr.io/pycqa/isort:latest \ + --check-only --diff isort +``` + +To check files in GitLab Continuous Integration, add the following job to your `.gitlab-ci.yml`: + +```yaml +stages: + - lint + +isort: + stage: lint + image: ghcr.io/pycqa/isort:latest + script: + - isort --check-only --diff . +``` + ## Setuptools integration Upon installation, isort enables a `setuptools` command that checks From 6e55dded011844eceae734a559f6ef68cc2c7c89 Mon Sep 17 00:00:00 2001 From: bengt Date: Wed, 8 Oct 2025 01:59:28 +0200 Subject: [PATCH 5/5] Tag the latest image on various degrees of specicifity --- .github/workflows/release.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b0b747ee..e00f4620 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -78,6 +78,11 @@ jobs: uses: docker/metadata-action@v5 with: images: ghcr.io/${{ github.repository }} + tags: | + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} + type=raw,value=latest - name: Build and push Docker image uses: docker/build-push-action@v6