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 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ada42b75..e00f4620 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,39 @@ 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 }} + 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 + 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 }} 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"] 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